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, June 2024

Hi all! This status update will be shorter than usual because I had a lot less free time for my open-source projects than usual this month. Indeed, I recently joined SNCF Réseau (the company responsible for the French railway infrastructure) to work on OSRD, …

via emersion June 19, 2024

Whose CIDR is it anyway?

A look at CIDR block ownership from a RIR-, country-, and organization level. Originally presented at RIPE88.

via Signs of Triviality June 12, 2024

How and why to make a /now page on your site

Background I used to wonder what my friend Benny Lewis was doing. He has a website and social media accounts, but neither gave an overview of what he’s doing now. Then I realized some people might wonder the same about me. So in 2015, I made a /now page on my…

via Derek Sivers blog May 18, 2024

Generated by openring