2023-01-29 05:12:51 +00:00
#! /usr/bin/zsh
set -e
PICTURES_DIR = " $( xdg-user-dir PICTURES) "
ZK_NOTEBOOK_DIR = "/home/lafrite/Nextcloud/Documents/zettelkasten/"
monitor = " $( swaymsg -t get_outputs | jq '[.[].focused] | index(true)' ) "
alias myrofi = " rofi -dmenu -matching fuzzy -i -sort -sorting-method fzf -monitor $monitor "
select_window( ) {
declare -A windows
swaymsg -t get_tree |
jq -r '..|.nodes? + .floating_nodes?|arrays|select(length > 0)|.[]|select((.nodes + .floating_nodes | length) == 0)|select(.visible)|(.app_id + ": " + .name + "\n" + (.rect.x | tostring) + "," + (.rect.y | tostring) + " " + (.rect.width | tostring) + "x" + (.rect.height | tostring))' |
while read window_name; read geometry; do
windows[ $window_name ] = " $geometry "
done
echo ${ windows [ $( print -l ${ (@k)windows } | myrofi) ] }
}
main( ) {
actions = " Copy region to clipboard\nSave region to $PICTURES_DIR \nCopy window to clipboard\nSave window to $PICTURES_DIR \nSave region to zk "
if [ [ " $( swaymsg -t get_outputs | jq length) " = = "1" ] ] ; then
actions += " \nCopy screen to clipboard\nSave screen to $PICTURES_DIR "
else
actions += " \nCopy current monitor to clipboard\nSave current monitor to $PICTURES_DIR \nCopy all monitors to clipboard\nSave all monitors to $PICTURES_DIR "
fi
selection = " $( echo " $actions " | myrofi -p "Take a screenshot" ) "
filename = " $( date +'screenshot_%Y%m%d%H%M%s.png' ) "
2023-02-24 08:28:11 +00:00
imagepath = " ${ PICTURES_DIR } / $filename "
2023-01-29 05:12:51 +00:00
zkpath = " ${ ZK_NOTEBOOK_DIR } /fig/ $filename "
case " $selection " in
"Copy region to clipboard" )
grim -g " $( slurp) " - | wl-copy
notify-send "Region copied to clipboard"
; ;
" Save region to $PICTURES_DIR " )
grim -g " $( slurp) " " $imagepath "
reply = $( notify-send -A 'open,Open' -i " $imagepath " "Screenshot saved" )
; ;
"Save region to zk" )
grim -g " $( slurp) " " $zkpath "
sh_path = " ../fig/ ${ zkpath ##*/ } "
wl-copy $sh_path
reply = $( notify-send -A 'open,Open' -i " $imagepath " "Screenshot saved" " $sh_path is copied " )
; ;
"Copy window to clipboard" )
grim -g " $( select_window) " - | wl-copy
notify-send "Window copied to clipboard"
; ;
" Save window to $PICTURES_DIR " )
grim -g " $( select_window) " " $imagepath "
reply = $( notify-send -A 'open,Open' -i " $imagepath " "Screenshot saved" )
; ;
"Copy current monitor to clipboard" )
monitor = " $( swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name' ) "
grim -o $monitor - | wl-copy
notify-send " Monitor $monitor copied to clipboard "
; ;
" Save current monitor to $PICTURES_DIR " )
grim -o " $( swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name' ) " " $imagepath "
reply = $( notify-send -A 'open,Open' -i " $imagepath " "Screenshot saved" )
; ;
"Copy screen to clipboard" | "Copy all monitors to clipboard" )
grim - | wl-copy
notify-send "Screen copied to clipboard"
; ;
" Save screen to $PICTURES_DIR " | " Save all monitors to $PICTURES_DIR " )
grim " $imagepath "
reply = $( notify-send -A 'open,Open' -i " $imagepath " "Screenshot saved" )
; ;
*)
exit 1
; ;
esac
if [ [ " $reply " = = "2" ] ] ; then
nautilus -s $imagepath
fi
}
main " $@ "