Skip to content

Instantly share code, notes, and snippets.

@mhdzli
Last active March 1, 2020 17:36
Show Gist options
  • Save mhdzli/1bed96944b57bfec5232088ab7dcefc4 to your computer and use it in GitHub Desktop.
Save mhdzli/1bed96944b57bfec5232088ab7dcefc4 to your computer and use it in GitHub Desktop.
Easy screenshots in Sway using dmenu
#!/bin/bash
# `list_geometry` returns the geometry of the focused of visible windows. You can also get they title
# by setting a second argument to `with_description`. The geometry and the title are seperated by `\t`.
#
# Arguments:
# $1: `focused` or `visible`
# $2: `with_description` or nothing
#
# Output examples:
# - with the `with_description` option:
# 12,43 100x200\tTermite
# - without the `with_description` option:
# 12,43 100x200
function list_geometry () {
[ "$2" = with_description ] && local append="\t\(.name)" || local append=
swaymsg -t get_tree | jq -r '.. | (.nodes? // empty)[] | select(.'$1' and .pid) | "\(.rect.x),\(.rect.y) \(.rect.width)x\(.rect.height)'$append'"'
}
WINDOWS=`list_geometry visible with_description`
FOCUSED=`list_geometry focused`
CHOICE=`rofi -dmenu -p 'Screenshot' << EOF
fullscreen
region
focused
$WINDOWS
EOF`
SAVEDIR=${SWAY_INTERACTIVE_SCREENSHOT_SAVEDIR:=~}
mkdir -p -- "$SAVEDIR"
FILENAME="$SAVEDIR/$(date +'%Y-%m-%d-%H%M%S_screenshot.png')"
EXPENDED_FILENAME="${FILENAME/#\~/$HOME}"
case $CHOICE in
fullscreen)
grim "$EXPENDED_FILENAME"
;;
region)
grim -g "$(slurp)" "$EXPENDED_FILENAME"
;;
focused)
grim -g "$FOCUSED" "$EXPENDED_FILENAME"
;;
'')
notify-send "Screenshot" "Cancelled"
exit 0
;;
*)
GEOMETRY="`echo \"$CHOICE\" | cut -d$'\t' -f1`"
grim -g "$GEOMETRY" "$EXPENDED_FILENAME"
esac
wl-copy < "$EXPENDED_FILENAME"
notify-send "Screenshot" "File saved as <i>'$FILENAME'</i> and copied to the clipboard." -i "$EXPENDED_FILENAME"
#!/bin/bash
#MENU="dmenu"
MENU="rofi -dmenu"
NOTIFY=$(pidof mako || 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)"')
RECORDER=wf-recorder
CHOICE=`$MENU -l 11 -p "How to make a screenshot?" << EOF
fullscreen
region
focused
select-output
select-window
record-builtin
record-external
record-region
record-focused
record-stop
EOF`
mkdir -p $(xdg-user-dir PICTURES)/screenshots/
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
$RECORDER -o eDP-1 -f "$RECORDING"
REC=1
elif [ "$CHOICE" = 'record-external' ]
then
$RECORDER -o DP-1 -f "$RECORDING"
REC=1
elif [ "$CHOICE" = 'record-region' ]
then
$RECORDER -g "$(slurp)" -f "$RECORDING"
REC=1
elif [ "$CHOICE" = 'record-focused' ]
then
$RECORDER -g "$(eval echo $FOCUSED)" -f "$RECORDING"
REC=1
elif [ "$CHOICE" = 'record-stop' ]
then
killall -SIGINT wf-recorder
if [ $NOTIFY ]; then
notify-send "Killing screen recorder" -t 2000
fi
exit
elif [ -z "$CHOICE" ]
then
if [ $NOTIFY ]; then
notify-send "Screenshot" "Cancelled" -t 1000
fi
exit 0
else
grim -g "$(eval echo $CHOICE)" "$FILENAME"
fi
if [ $REC ]; then
notify-send "Recording" "Recording stopped: $RECORDING" -t 10000
else
if [ $NOTIFY ]; 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