Scripting timed screenshots

I’ve known about the screencapture command for ages. My first screenshot/upload utility was written in Perl back in early 2006 and was invoked via Quicksilver. That early script gradually evolved into the snapftp script, written in Python and invoked via FastScripts, I use today. I learned yesterday that I haven’t been keeping up with screencapture’s improvements over the years. It now has an option for doing timed screenshots, which I’ve been using the venerable Grab application for. Using this option, I now have a smoother workflow for taking screenshots of menus and other ephemeral screen displays.

When I say screencapture “now has an option,” I’m using a very elastic definition of “now.” Looking back through the history of the screencapture man page, which you can do through the “Choose a version” popup menu, I’m embarrassed to say that it’s had this option since Leopard. Apparently, I haven’t checked the man page since I wrote that first script in 2006. Better late than never, I guess.

My timed screenshot script is barely a script at all:

bash:
#!/bin/bash

screencapture -T10 -P ~/Desktop/snap.png

Just one line. The -T10 option tells it to take the screenshot 10 seconds after the script is called. I find that’s usually plenty of time to do whatever clicking or hovering is needed to bring up the item I want in the screenshot. The -P option tells it to open the screenshot file in Preview when it’s done. With a timed screenshot, screencapture takes a picture of the entire display, which is seldom what I want. By bringing it up in Preview right away, the script saves me the step of finding the file on the Desktop and opening it up for cropping.

You might think something this simple would be better defined as an alias in my .bashrc file than as a separate script. That’d be true if I called it from the Terminal, but I don’t. Calling a screenshot utility from the Terminal is clumsy, because you have to get the Terminal window out of the way before the screenshot is snapped. By saving it as a script, named “Timed Screenshot”, in my ~/Library/Scripts folder, I can call it via FastScripts with any application active. I’ve assigned it the ⌃⌥⌘3 keyboard shortcut, reminiscent of the builtin ⇧⌘3 full-screen capture shortcut.

Now it’s fast and easy to take screenshots like this

Menu screenshot

Although I’m kicking myself for wasting time with Grab for all those years, at least I won’t be wasting any more time with it.


5 Responses to “Scripting timed screenshots”

  1. Lauri Ranta says:

    I’ve just used a shell function that saves a screenshot every second. It’s hard to tell if it’s more or less convenient than the timer approach though. You don’t always have to wait ten seconds and you get multiple shots at taking the screenshots, but you have to go back to a shell window to press C-c and delete the other screenshots later.

    screencaps() {
        while :; do
            screencapture -C ~/Desktop/$(date +%y%m%d%H%M%S).png
            sleep ${1-1}
        done
    }
    

    -C includes the cursor in the screenshots.

    To save an image of a single window with a shadow, you have to use the interactive mode:

    screencapture -T5 -ic
    
  2. Dr. Drang says:

    Using the command line for taking screenshots would drive me crazy. Too much back and forth with the Terminal for an action that is entirely visual.

  3. Ben K says:

    Wouldn’t it still be easier to just hit Cmd-Shift-4, hold the Ctrl key and draw a marquée around the area of interest, then switch to Preview and do a File > New?

    That way you end up with a pre-cropped document with no intermediate file to dispose of, and no script needed.

  4. Dr. Drang says:

    Ben,
    That can work for menus because you can click on a menu and it’ll stay open while you draw the marquee (although I find that it often changes the menu as I draw the enclosing rectangle), but I’ve never gotten it to work for things like tips that appear when you hover over an item.

  5. Ben K says:

    Hmm; fair point. I was going to remark that you can change the menu selection using cursor keys if need be, but I hadn’t thought about other mouse-interactive things that you might wish to capture.

    (I just instigated a tooltip in Safari and had no trouble capturing it, but undoubtedly other apps or controls might behave differently.)