Skip to content

Instantly share code, notes, and snippets.

@mary-ext
Last active September 24, 2024 08:31
Show Gist options
  • Save mary-ext/b3bf4c40b402b4dd283bed78f4e6b4e8 to your computer and use it in GitHub Desktop.
Save mary-ext/b3bf4c40b402b4dd283bed78f4e6b4e8 to your computer and use it in GitHub Desktop.
"Proper" area screenshot in Hyprland, uses grim, slurp, imv and imagemagick
#!/usr/bin/env bash
# - Take a screenshot of the active monitor
# - View said screenshot in fullscreen
# - Ask the user for the region to crop
# - Pass the crop geometry for ImageMagick to crop and copy it
# + Grab the active workspace's window geometries for suggested crops
CURRENT_OUTPUT=$(hyprctl monitors -j | jq -r '.[] | select(.focused) | .name')
CURRENT_SCALE=$(hyprctl monitors -j | jq -r '.[] | select(.focused) | .scale')
CURRENT_WORKSPACE=$(hyprctl monitors -j | jq -r 'map(.activeWorkspace.id)')
ACTIVE_WINDOWS=$(hyprctl clients -j | jq -r --argjson workspaces "$CURRENT_WORKSPACE" 'map(select([.workspace.id] | inside($workspaces)))')
WINDOW_GEOMETRIES=$(echo "$ACTIVE_WINDOWS" | jq -r '.[] | "\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"')
TEMP_FILE=$(mktemp --suffix=.png)
grim -l 0 -o $CURRENT_OUTPUT $TEMP_FILE
imv -f $TEMP_FILE &
PID=$!
# Fullscreen won't work without waiting 0.1s for whatever reason
sleep 0.1
PICKED_GEOMETRY=$(echo "$WINDOW_GEOMETRIES" | slurp -d)
kill $PID
if [ "$PICKED_GEOMETRY" ]; then
IM_GEOM=$(echo '"'$PICKED_GEOMETRY'"' | jq -r --argjson scale $CURRENT_SCALE '. | split("[ ,x]"; "") | map(tonumber * $scale | floor) | "\(.[2])x\(.[3])+\(.[0])+\(.[1])"')
echo $IM_GEOM
convert $TEMP_FILE -crop $IM_GEOM png:- | wl-copy -t image/png
fi
rm -f $TEMP_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment