Skip to content

Instantly share code, notes, and snippets.

@nikto-b
Created December 8, 2021 12:35
Show Gist options
  • Save nikto-b/5f0aeacaeaa08be64c874faa4bad9d5d to your computer and use it in GitHub Desktop.
Save nikto-b/5f0aeacaeaa08be64c874faa4bad9d5d to your computer and use it in GitHub Desktop.
#!/bin/bash
DUNST=`pidof dunst`
FOCUSED=$(swaymsg -t get_tree | jq '.. | (.nodes? // empty)[] | select(.focused and .pid) | .rect | "\(.x),\(.y) \(.width)x\(.height)"')
OUTPUTS=$(swaymsg -t get_outputs | jq -r '.[] | select(.active) | .rect | "\(.x),\(.y) \(.width)x\(.height)"')
WINDOWS=$(swaymsg -t get_tree | jq -r '.. | select(.pid? and .visible?) | .rect | "\(.x),\(.y) \(.width)x\(.height)"')
if [[ "$1" == "" ]]; then
CHOICE=`dmenu -l 10 -p "How to make a screenshot?" << EOF
fullscreen
region
focused
select-output
select-window
-cancel-
record-builtin
record-external
record-region
record-focused
record-stop
EOF`
else
CHOICE=$1
fi
FILENAME="$(xdg-user-dir PICTURES)/screenshots/$(date +'%Y-%m-%d-%H%M%S_screenshot.png')"
RECORDING="$(xdg-user-dir PICTURES)/screenshots/$(date +'%Y-%m-%d-%H%M%S_recording.mp4')"
if [ "$CHOICE" = fullscreen ]
then
grim "$FILENAME"
elif [ "$CHOICE" = region ]
then
slurp | grim -g - "$FILENAME"
elif [ "$CHOICE" = select-output ]
then
echo $OUTPUTS | slurp | grim -g - "$FILENAME"
elif [ "$CHOICE" = select-window ]
then
echo $WINDOWS | slurp | grim -g - "$FILENAME"
elif [ "$CHOICE" = focused ]
then
grim -g "$(eval echo $FOCUSED)" "$FILENAME"
elif [ "$CHOICE" = 'record-builtin' ]
then
wf-recorder -o eDP-1 -f "$RECORDING"
REC=1
elif [ "$CHOICE" = 'record-external' ]
then
wf-recorder -o DP-1 -f "$RECORDING"
REC=1
elif [ "$CHOICE" = 'record-region' ]
then
wf-recorder -g "$(slurp)" -f "$RECORDING"
REC=1
elif [ "$CHOICE" = 'record-focused' ]
then
wf-recorder -g "$(eval echo $FOCUSED)" -f "$RECORDING"
REC=1
elif [ "$CHOICE" = 'record-stop' ]
then
killall -SIGINT wf-recorder
if [ $DUNST ]; then
notify-send "Killing screen recorder" -t 2000
fi
exit
elif [ -z "$CHOICE" ]
then
if [ $DUNST ]; then
notify-send "Screenshot" "Cancelled" -t 1000
fi
exit 0
else
grim -g "$(eval echo $CHOICE)" "$FILENAME"
fi
if [ $REC ]; then
sleep 1
else
if [ $DUNST ]; then
notify-send "Screenshot" "File saved as $FILENAME\nand copied to clipboard" -t 6000 -i $FILENAME
fi
wl-copy < $FILENAME
#feh $FILENAME
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment