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

Hi all! This month I’ve spent a lot of time triaging Sway and wlroots issues following the Sway 1.10 release. There are a few regressions, some of which are already fixed (thanks to all contributors for sending patches!). Kenny has added support for software-…

via emersion November 22, 2024

Dismissed!

Dismissing gives me a quick little lift. “That guy is an idiot!” “That place sucks!” There. Now I feel superior. Now I don’t have to think about it. I don’t even need first-hand experience. I can just echo any complaint I’ve heard. I’ve done this with restaurants…

via Derek Sivers blog November 18, 2024

2024-11-06

Post-election mood.

via Signs of Triviality November 6, 2024

Generated by openring