Taking Screenshots on Wayland

— 579 words — 3 min


I wanted to have a convenient way to take screenshots on sway with different modes:

I put together a simple script that does exactly that. Depending on the first argument passed to the script, it enters a different mode. That makes the bindings look nice and maintainable in the sway configuration.

On Arch Linux you can install the required packages with

pacman -S jq grim slurp swappy

You can save the following script somewhere, for example in ~/.local/bin/screenshot.sh and make it executable with chmod +x ./screenshot.sh.

#!/usr/bin/env sh
# Take a screenshot on wayland with swaymsg, jq, grim, slurp, and swappy
#
# Make sure the script is executable (chmod +x ./screenshot.sh)
#
# If you don't use sway, replace `swaymsg` with whatever your window manager
# gives you to query window information.
#
# Example sway configuration
#
# bindsym Print             exec ~/.local/bin/screenshot.sh region
# bindsym Shift+Print       exec ~/.local/bin/screenshot.sh window
# bindsym Ctrl+Print        exec ~/.local/bin/screenshot.sh output
# bindsym Ctrl+Shift+Print  exec ~/.local/bin/screenshot.sh all


# region|window|output|all
mode="$1"

case $mode in
    "region")
        grim -g "$(slurp)" - | swappy -f -
        ;;
    "window")
        grim -g "$(swaymsg -t get_tree | jq -j '.. | select(.type?) | select(.focused).rect | "\(.x),\(.y) \(.width)x\(.height)"')" - | swappy -f -
        ;;
    "output")
        grim -o $(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name') - | swappy -f -
        ;;
    "all")
        grim - | swappy -f -
        ;;
    *)
        echo >&2 "unsupported command \"$mode\""
        echo >&2 "Usage:"
        echo >&2 "screenshot.sh <region|window|output|all>"
        exit 1
esac

As you can see, I also included example bindings for sway. How you can change the bindings should be rather obvious even if you are not too familiar with sway: just change the first argument of the script to a different allowed value or change the symbol (key) that this exec command is bound to.

Copy and paste the following bindings into your sway configuration; probably at ~/.config/sway/config.

bindsym Print             exec ~/.local/bin/screenshot.sh region
bindsym Shift+Print       exec ~/.local/bin/screenshot.sh window
bindsym Ctrl+Print        exec ~/.local/bin/screenshot.sh output
bindsym Ctrl+Shift+Print  exec ~/.local/bin/screenshot.sh all

Reload sway and you’re done!

How does it work?🔗

Let’s take a look at the first command that I bound to the Print key.

grim -g "$(slurp)" - | swappy -f -

If you press the Print key, then a white-ish overlay will appear and you can select the region of the screenshot with your mouse. This part is done by the program slurp. It will print the start coordinate (x,y) and selected dimensions (<width>x<height>) to stdout which is fed to grim -g. Grim will go ahead and capture the content at the provided coordinate with the given dimensions and write the result to stdout which in turn is read on stdin by swappy. Swappy displays the image and lets you annotate it by drawing something or adding text or forms. Then you can copy it to your clipboard or save it to disk.

The other commands work in a similar way. The trick is to pass the right coordinate and dimensions to grim. Hence, on sway you can use swaymsg and jq to extract that information when you only want to capture the focused window or output.

Other suggestions? Get in touch!


Articles from blogs I follow around the net

Status update, October 2024

Hi! This month XDC 2024 took place in Montreal. I wasn’t there in-person, but thanks to the organizers I could still ask questions and attend workshops remotely (thanks!). As usual, XDC has been a great reminder of many things I wanted to do but which got bur…

via emersion October 21, 2024

Wealth = Have ÷ Need

Not a new idea, but just another visualization and reminder. Wealth, feeling like you have plenty, is an equation. Wealth = Have ÷ Need If you have nothing, then focus on having some. Once you have some, the easiest way to increase your wealth is to decrease …

via Derek Sivers blog September 27, 2024

Installing NetBSD on Linode

Instructions on how to install NetBSD on a Linode instance. All steps are performed via the command-line and serial console.

via Signs of Triviality September 14, 2024

Generated by openring