So I recently had the need for quick screenshots, at first I was thinking, a tray icon would be the best solution for my problem, but then I realized it doesn’t get better than a key press, a system wide shortcut. So what did I do? I looked to my window manager for answers, in this case Awesome WM.
First I needed a screenshot program, preferably one that could take shots from the command line. Most people use scrot, but since I had imagemagick installed, I decided to go with “import” a built-in part of imagemagick. Now, the other thing I thought about was that I needed to create a unique identifier when saving the screenshots, so if I take multiple, they don’t overwrite each other. The best way to do this would be to append the UNIX timestamp to the filename so that they all have unique filenames (this assumes you take >1 shot per second).
Now that that’s all figured out, we just have to write the functionality, so first, let’s work out the screenshot command. The parameter for “import” we’ll be needing is, -window, for the window to take the screenshot of, in this case the root window. So our command would look like this:
import -window root /home/hkothari/pictures/screens/shot-$TIME.jpg
Since Awesome is written in LUA, we’ll need to use the “os.time()” function to fetch the time, and to add the key we’re going to use awful.key, adding it in the globalkeys variable, below the comment “– Standard Programs”. So go ahead and open your “.config/awesome/rc.lua” and add the following:
-- Standard Programs awful.key({ }, "F10", function () awful.util.spawn("import -window root /home/hkothari/pictures/screens/pic-" .. os.time() .. ".jpg") end),
This means that when the F10 key is pressed with no modifiers, it will call our screenshot function. (Note: the “..” is for concatenating strings in lua.) And that’s all there is to it.
Related posts:
