Skip to content

Instantly share code, notes, and snippets.

@rekyuu
Created May 30, 2023 00:36
Show Gist options
  • Save rekyuu/a8b91826cf8e01b1ff626a2694176954 to your computer and use it in GitHub Desktop.
Save rekyuu/a8b91826cf8e01b1ff626a2694176954 to your computer and use it in GitHub Desktop.
screenshot-grim.sh
#! /bin/bash
function get-sway-window-id() {
swaymsg -t get_tree | jq ".. | select(.type?) | select(.pid==$1).id"
}
function slurp-custom() {
slurp -b "#00000075" -c "#FF0000FF"
}
filename=$(date "+%Y-%m-%d_%H-%M-%S-%3N")
foldername=$(date "+%Y-%m")
destination_folder=~/Screenshots/$foldername
output="$destination_folder/$filename"
mkdir -p "$destination_folder"
case $1 in
all) # prtscr
grim "$output.png"
;;
window) # alt + prtscr
window=$(swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true)')
pos_x=$(echo "$window" | jq ".rect.x")
pos_y=$(echo "$window" | jq ".rect.y")
width=$(echo "$window" | jq ".rect.width")
height=$(echo "$window" | jq ".rect.height")
grim -g "${pos_x},${pos_y} ${width}x${height}" "$output.png"
;;
selection) # shift + prtscr
grim -t ppm - | feh - &
FEH_PID=$!
swaymsg "for_window [pid=$FEH_PID] fullscreen enable global"
while [ -z "$(get-sway-window-id $FEH_PID)" ]; do
sleep 0.1
done
grim -g "$(slurp-custom)" "$output.png"
kill "$FEH_PID"
;;
esac
if [[ -f "$output.png" ]]; then
wl-copy < "$output.png"
notify-send "Image copied to clipboard."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment