serhii.net

In the middle of the desert you can say anything you want

10 Dec 2021

Getting screenshots to work in qtile

Get screenshotting working through a hotkey. I need to screenshot an area of the screen, put the screenshot in a folder, and immediately open it.

In i3 had

bindsym Mod3+s --release exec scrot -s -e 'mv $f ~/s/screenshots && eog ~/s/screenshots/$f'

Nothing I tried worked (didn’t do anything weird):

Key([mod], "s", lazy.spawn(CONFIG_LOCATION + "screenshot.sh"))

Tracked it down to two main issues:

  1. scrot works, scrot -s doesn’t. (Running the shell script directly from shell was fine!)
  2. qtile doesn’t like variables in shell scripts
    # this works
    scrot -u -e 'thunar $f' "/tmp/shot.png"
    # this doesn't
    scrot -u -e 'thunar $f' "$SCREENSHOT_PATH/shot.png"
    

Decided to leave the first one alone, scrot -u gets the currently selected window, which generally is good enough for me.

The second one - first rewrote the script to get passed the target path as positional variable (surprisingly it worked!), then decided to do it python-only. As a bonus, copies the screenshot url to the clipboard.

# definition
copy_command = 'bash -c "echo {0} | xclip -selection c"'
# ...
def take_screenshot():
	SCREENSHOT_FILENAME = datetime.now().strftime("qtile_%y%m%d-%H%M%S%z")+"-$w$h.png"
	screenshot_path = D.SCREENSHOT_DIR +"/"+ SCREENSHOT_FILENAME
	command = f"scrot -u -e 'thunar $f && {Commands.copy_command.format('$f')}' {screenshot_path}"
	return command

#usage
Key([mod], "s", lazy.spawn(Commands.take_screenshot()))

(qtile-dotfiles/config.py at master · justinesmithies/qtile-dotfiles has escrotum as python module, errored out during install in the qtile venv and segfaulted on first run when installed outside of it.)

Nel mezzo del deserto posso dire tutto quello che voglio.