Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automate screen captures via Grab and GUI Scripting Apps
I frequently use Grab to capture part of the screen. Until now, I've used Youpikey with a key combination to launch Grab and Shift-Commandd-A to activate the selection for capture. I've now managed to reduce this to one Youpikey keystroke combination in which it launches the AppleScript below. This may not be in itself a significant timesaving hint, but the underlying solution has potential for the scripting of other GUI applications which are generally not scriptable.

Now Grab isn't scriptable, but with the beta version of the System Events application, you can now write AppleScripts that can control the GUI of most OS X applications. This includes the automated selection of menu items, such as the Capture Selection menu items of Grab. The script that I've written and shown below uses the UI Scripting System Events Application. To use it, you'll need to download [81KB] and install the UI Scripting beta application. You can read more about it on Apple's GUI Scripting page.

You can also download the UI Element Inspector [319KB]. It's not essential for this particular application, but it is useful because of the UI Scripting explanation and example given in the Read Me.

The UI Scripting beta requires Mac OS X 10.2.3 as a minimum (I'm running mine under 10.2.4), and you will need to check the box marked 'Enable Access for assistive devices' in the Universal Access section of the System Preferences app for it to work. Happy grabbing!

-- grabselected
-- activates Grab and causes it to capture screen selection
-- requires Dec2002SystemEvents UI Scripting (beta)
-- use of UI Element Inspector optional
-- David Paul, 2003. Made in Finland.
tell application "Grab"
  tell application "System Events"
    tell process "Grab"
      tell menu bar 1
        click menu item "Selection" of menu "Capture"
      end tell
    end tell
  end tell
end tell
[robg adds: I haven't tested this one myself...]
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)

Automate screen captures via Grab and GUI Scripting | 8 comments | Create New Account
Click here to return to the 'Automate screen captures via Grab and GUI Scripting' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
use screencapture instead
Authored by: Diggory on Mar 18, '03 10:45:16AM
Use the shell command "screencapture" instead.

usage: screencapture [-icmwsWx] [file] [cursor]
-i capture screen interactively, by selection or window
control key - causes screen shot to go to clipboard
space key - toggle between mouse selection and
window selection modes
escape key - cancels interactive screen shot
-c force screen capture to go to the clipboard
-m only capture the main monitor, undefined if -i is set
-w only allow window selection mode
-s only allow mouse selection mode
-W start interaction in window selection mode
-x do not play sounds
file where to save the screen capture

e.g. this applescript:

do shell script "screencapture -i ~/myScreenShot.pdf"

will allow you to choose what part of the screen to capture (-i) and will save the file to ~/myScreenShot.pdf

Note - the file format is defined by system defaults - *not* by the extension you give - so trying myScreenshot.jpg will give you a PDF file that has a JPG extension.

This also means you don't have to have the Beta UI Scripting installed.

Use MoreInternet Preference Pane to change Internet Helpers without IE

[ Reply to This | # ]

use screencapture instead
Authored by: englabenny on Mar 18, '03 11:49:06AM

to this use, your cli-utility solution is surely better; but it is nice someone shows me this feature I've only heard of before, to be able to script anything in menus. It sure is usable - even though in this case there is an easier solution..

[ Reply to This | # ]
use screencapture instead
Authored by: pcunix on Mar 18, '03 02:28:23PM
I don't see any point in writing Applescript to do nothing but run a shell command.

If you already have terminal windows open, just type the command. If you want to be able to click on it from Finder, then just put it in a file with the extension .command, and chmod 755 it. So this, for example, would be just:

echo "screencapture -i  ~/myScreenShot.pdf" > sreenshot.command
chmod 755 screenshot.command

Why have the overhead of Applescript when you don't need it?

Tony Lawrence
Free SCO, Mac OS X and Linux Skills Tests:

[ Reply to This | # ]

use screencapture instead
Authored by: Accura on Mar 18, '03 08:11:55PM

i agree with you about not using apple script to run a shell scrip but people like apple script just because they know how to add frills and stuff.

"The time has come," the walrus said. "To talk of many things..."

[ Reply to This | # ]
capture scrollable area?
Authored by: chag on Mar 22, '03 12:48:58PM

is there any way to add the ability to capture the entire scrollable area of
a window? Looking for a replacement for the OS9 app ScreenCatcher.

[ Reply to This | # ]
Why use Grab?
Authored by: MrSandman on Mar 18, '03 11:52:46AM

One must wonder why you are using Grab at all, why not just hit cmd-shift-4 ?

[ Reply to This | # ]
Why use Grab?
Authored by: Red Phoenix on Mar 18, '03 05:27:39PM

Even better: if you want to take a screenshot of a window, menu, or the dock, do command-shift-4 and hit the spacebar. It will highlight in blue the object under the mouse. Just click, and it will save the screenshot to your desktop.

[ Reply to This | # ]
Why use Grab?
Authored by: Frederico on Mar 18, '03 05:42:09PM

Grab allows you to select formats other than PDF. For this reason alone, Grab retains some degree of usefulness.

While we're on this subject, is anyone else pissed off at Apple for continuing to change and dictate to us how we will use Finder screen captures? In this case, I am referring to the new behavior (as of 10.2.4) that now splits a multiple-monitor screenshot into individual pages in a PDF document. A total PITA to try to paste back together into a usable form.

Sometimes I just want to fly into Cupertino First Class just so I can invade the NeXTie stronghold and whack them all repeatedly with a rubber chicken until they understand that Macolytes demand flexiblity, not rigidity.


[ Reply to This | # ]