some applications have poor scripting support — acrobat is a shocker. have a look at the acrobat scripting dictionary to see just how restricted the opportunities are — even for the pro version. but it IS still possible to automate acrobat through GUI (graphical user interface, or gooey) scripting. GUI scripting has been available since OSX 10.3 (panther) and uses system events to, basically, activate menus, open windows and click buttons in the same way that you would using a mouse.
this post will outline a process for working out just one example — running a batch sequence from the advanced menu. but hopefully you’ll glean enough information to be able to adapt what’s here for other scripts too.
first you need to enable GUI scripting. you can do this through the applescript utility found in applications > applescript :
with GUI scripting, rather than telling an application to do something (eg. tell application “Adobe Acrobat Pro”), you tell system events to tell the application to do something. system events refers to applications as processes. make sure acrobat is running and then run this in script editor (also found in applications > applescript) :
tell application "System Events" get name of every process end tell
you’ll get a list of names in the results window — some of which you may not recognise. this is where you find out that system events refers to ‘adobe acrobat pro’ as, simply, ‘acrobat’. so you would address acrobat like this :
tell application "System Events" tell process "Acrobat" -- do something -- end tell end tell
the first thing you’ll want to address is the menu bar at the top of the screen — acrobat, file, edit, view, etc. the following script shows that, to access a menu, you need to first address the associated menu item. weird eh? :
tell application "System Events" tell process "Acrobat" tell menu bar 1 tell menu bar item "Advanced" tell menu "Advanced" tell menu item "Document processing" tell menu "Document processing" click menu item "Batch Processing..." end tell end tell end tell end tell end tell end tell end tell
pretty straightforward so far? ok. now, the above script will open the ‘batch sequences’ window :
with this example we want to run the optimise sequence — so we need to select the sequence and then click the run sequence button. and this is where things REALLY get confusing. exactly how do we find the terminolgy to address the right bits? we ask for the window’s UI elements :
tell application "System Events" tell menu bar 1 of process "Acrobat" tell menu "Advanced" of menu bar item "Advanced" tell menu "Document Processing" of menu item "Document Processing" click menu item "Batch Processing..." end tell end tell end tell tell window "Batch Sequences" of process "Acrobat" return UI elements end tell end tell
two things to notice here : first, the number of lines has been reduced by combining sets of two (eg. tell menu bar 1 of process “Acrobat”); second, when a new window is opened you need to go right back to system events to address that window (you can’t just continue on from after “click menu item “Batch Processing…””). the above script will give you something like this in script editor’s results window :
a whole bunch of buttons — most of them are listed by name (that makes it easy to find the run sequence button) but what about those first three? buttons 1, 2, & 3 are the red, yellow and green lights you get at the top of every window these days. the only other UI elements are some static text (the window’s title) and a scroll area — ahh, that sounds right, the list of sequences are within something that might be called a scroll area… ok, how do we find out how to talk to the scroll area? we ask for its UI elements :
tell application "System Events" tell menu bar 1 of process "Acrobat" tell menu "Advanced" of menu bar item "Advanced" tell menu "Document Processing" of menu item "Document Processing" click menu item "Batch Processing..." end tell end tell end tell tell window "Batch Sequences" of process "Acrobat" tell scroll area 1 return UI elements end tell end tell end tell
only two UI elements for the scroll area — one is a scroll bar (probably not what we’re looking for) and the other is an outline. outline? WTF? oh well, let’s keep going, see what we find (we’re actually nearly there) :
tell application "System Events" tell menu bar 1 of process "Acrobat" tell menu "Advanced" of menu bar item "Advanced" tell menu "Document Processing" of menu item "Document Processing" click menu item "Batch Processing..." end tell end tell end tell tell window "Batch Sequences" of process "Acrobat" tell outline 1 of scroll area 1 return UI elements end tell end tell end tell
ooh… that looks like something we can work with. let’s try it :
tell application "System Events" tell menu bar 1 of process "Acrobat" tell menu "Advanced" of menu bar item "Advanced" tell menu "Document Processing" of menu item "Document Processing" click menu item "Batch Processing..." end tell end tell end tell tell window "Batch Sequences" of process "Acrobat" tell outline 1 of scroll area 1 select row 4 end tell click button "Run Sequence" end tell end tell
bingo! first we have to open the batch sequences window, then select row 4 of outline 1 of scroll area 1, then click the run sequence button. phew!
now you can see why GUI scripting is only used as a last resort. there are third party products which make it easy to drill down through the various UI elements. and apple provide a free accessibility inspector with its developer tools or the, also free, UI elements inspector.
but why do things the easy way when you can have sooo much fun working it out the hard way?