Open screenshots immediately

Screenshots often come in handy—for instance, when you need a quick snapshot of something you’re working on or when you want to send a picture of an error message to IT. Mac OS X includes some good built-in tools for taking screenshots—Shift-Command-3 will take a picture of the whole screen, and Shift-Command-4 will let you drag a region to capture (or press the space bar to use the camera icon to take pictures of windows).

But using these tools, means you often do the “screenshot two-step.” First, you take the picture. Then you switch to the Finder, hunt for the saved file on your desktop, and open it to make sure you captured what you intended. With a little help from Unix and AppleScript, you can automate this process (as long as you’re running OS X 10.3 or 10.4). An AppleScript will activate a Unix script that captures the screenshot, writes it to a uniquely named file, and then opens the image in Preview.

Open ScriptEditor (in the Applications/AppleScript folder), and paste in the following code:

	do shell script "DATE=`date '+%Y%d%m-%H%M%S'`;
	FILE=~/Pictures/screenshot-${DATE}.png;
	screencapture -i -W -x $FILE;
	if [ -e $FILE ]; then
	open /Applications/Preview.app $FILE;
	fi"
	

When you’ve entered the script, select File: Save As, give the script a name, set the File Format pop-up menu to Application, and save the script to a convenient location. One possible spot is your user folder /Library/Scripts folder—from here it will be easily accessible in the Scripts menu. (Create this folder if you don’t have it already.) To learn more about using the Scripts Menu, see the end of this article.

Digging into the code

Since part of the point of these hints is to teach, here’s what each piece of the script does. Feel free to skip this section if you’d just like to put the script to work. (The semicolons in the above code simply separate commands, so they won’t be explained below.)

  • do shell script “ — This command tells AppleScript that the stuff following the double-quotes is a Unix shell script.
  • DATE=`date ‘+%Y%d%m-%H%M%S’` — This sets a variable, DATE, by using the unix date command. The variable is set to the current date and time, separated by a hyphen.
  • FILE=~/Pictures/screenshot-${DATE}.png — Another variable, FILE, is created, setting the filename that will be used for the screenshot. It includes the DATE variable, ensuring that the file will have a unique name. If you’re running OS X 10.3, change .png to .pdf. That was the default file type for screenshots in Panther.
  • screencapture -i -W -x $FILE — This is the actual unix command used to take the screenshot. The -i -W bit sets the capture to “interactive window” mode (i.e. the camera icon), and then -x $FILE specifies the filename to be used. Note that if you’d rather have the region capture available, just leave out the -W from the command.
  • if [ -e $FILE ]; then — An if statement that simply makes sure the file that was just created actually exists.
  • open /Applications/Preview.app $FILE — Opens the newly-created file in Preview.
  • fi” — The end of the if statement.

Using the script

After you’ve saved the script, using it is simply a matter of activating it. If you saved it to your user’s Library/Scripts folder, that’s as simple as choosing it from the Script icon in the menubar. You’ll then see the camera icon onscreen; press the Space Bar when it’s over the window you’d like to capture. After a brief delay, the image you captured will open in Preview.

An even easier way to put the script to use is through a keyboard shortcut—using any one of a number of programs ( Peter Maurer’s Butler, Objective Development’s LaunchBar, Startly Technologies’ QuicKeys X, Blacktree’s Quicksilver, and so on), you can assign a hot key to launch your AppleScript application. Now you don’t even need the menubar; just hit the hot key from any program.

There you have it! Snap-and-view, just like that!

Aside: The Scripts Menu

Apple included a system-wide feature known as the Scripts menu with all recent versions of OS X. Through the Scripts menu, you can use predefined and user-created AppleScripts from anywhere, via an icon in the menubar. This feature, however, is disabled by default.

To enable it, open the Applications/AppleScript folder. If you’re running OS X 10.3, just double-click the Install Script Menu program and you’re done. If you’re using OS X 10.4, launch the AppleScript Utility. Check the Show Script Menu in menu bar box, and also (if you want to have access to some useful scripts) check the Show Library scripts box, then quit the program. After running the proper program for your OS release, you should now see the Script icon in the menubar, as seen at right.

When you click the icon, you’ll see a bunch of folders for existing scripts provided by Apple. But you can also add your own things to this menu. To do so, create a folder called Scripts in your user’s Library folder. Anything you place in this folder, including other folders, will show up at the bottom of the Scripts menu. So for this hint, if you save your new AppleScript application into that folder, you’ll be able to get to it from any application.

Subscribe to the Create Newsletter

Comments