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?
Hi M.
thanks for your help, I think this is going to be complicated for me… :(
this is what I wrote in my scripts editor:
on adding folder items to this_folder after receiving added_items
tell application “Adobe Acrobat Pro”
open added_items
tell application “System Events”
tell menu bar 1 of process “Acrobat”
tell menu “Archivo” of menu bar item “Archivo”
tell menu “Asistente de acciones” of menu item “Asistente de acciones”
click menu item “Pelicula Clondalkin”
tell window “Acción: Pelicula Clondalkin” of process “Acrobat”
get UI elements
end tell
end tell
end tell
end tell
end tell
end tell
end adding folder items to
When I execute it, I don´t have any UI elements results in the main window, I suppose that something is wrong…
I added the script in the actions folder scripts, and it works (well ,…more or less).
I have to depurate some operations, i.e.: once the file is opened in acrobat, the the action does not run. For some reason I can not understand, I have to activate the pdf window and make a single click in any of the top menu options (file, edit,…) then the action starts… (¿?)
Another thing that I have to depurate is that once the action is finished, it appears a dialog window informing me that the file has been saved in de Desktop folder, and I have to click “OK”… is it possible to avoid this step by closing the pdf automatically??
Is it possible that I save the action running on my computer and share the video with you by email to show all this issues?
Thanks a lot in advance.
Joan
G’day Joan
You’re doing a great job. UI scripting is not an easy thing to attempt as your first Applescript.
You should first get the basic applescript running correctly — so that you are happy with it — THEN turn it into a folder action.
Definitely don’t try to debug this script when it is running as a folder action — that is way too hard.
So far your script only opens the window “Acción: Pelicula Clondalkin” — that’s all it does.
The command get UI elements is used in Applescript Editor to find out (in the results window) which elements within the window you can select, or click on, or whatever.
So, at the very least, you’ll need to have the script click a button within that window to make the action run.
And, yes, I expect you will be able to get the script to click that “OK” button at the end too.
You can certainly email me directly — no problem at all — happy to help out any way I can.
macgrunt.au@gmail.com
m.
Hello!,
i think that this post is super-interesting.
I´m trying to apply it to my Acrobat Pro X, running OSX 10.6, but I can´t access the actions wizard window.
Please could you help me to get the applescript code to do it? the I could get the UI elements to continue.
Thanks and Regards.
Joan
G’day Joan
Things have changed a bit since I wrote this post. To enable GUI scripting now you need to go to your System Preferences > Universal Access and check the box at the bottom “Enable access for assistive devices”.
Then I expect the process is probably the same. Eg. :
Hopefully that gets you closer to where you’re going
m.
Hi M. for your quick response!
I have tried your instructions and I get the Actions Wizard opened! (I can feel I´m very close to it!)
then I try to get the UI elements os the window “Editar acciones”,…
My Acrobat Pro X is in spanish so I hope the following code is not too confusing for you:
tell application “System Events”
tell menu bar 1 of process “Acrobat”
tell menu “Archivo” of menu bar item “Archivo”
tell menu “Asistente de acciones” of menu item “Asistente de acciones”
click menu item “Editar Acciones”
end tell
end tell
end tell
tell window “Editar acciones” of process “Acrobat”
return UI elements
end tell
end tell
But then I get this error message telling : “it is not possible to obtain window \”Editar acciones\” of process \”Acrobat\”.” number -1728 from window “editar acciones” of process “Acrobat”.
As you has observed, this is my first experience with applescript.
I defined some actions in the acrobat actions wizard and I would like to execute them by droping a pdf file inside a folder…
would you be so kind yo help me with this?
Thanks and Regards.
Hello again Joan
Welcome to the world of scripting. It can be very frustrating, but very rewarding as well.
I’m not really sure why you cannot address the window “Editar acciones”
Perhaps the syntax is different for that window (?).
But if you are trying to activate an existing action, you do not need to open the Editar acciones window.
Change this section of the script instead :
Then you should find that you can :
Do you need help just with the script, or also with setting up the folder action?
m.