views:

562

answers:

2

Hi,

I'm not sure if this is do-able via AppleScript and/or Automator…but I'd like to be able to:

a) launch an application (I know this can be done pretty easily by AppleScript or Automator)

b) once the application is launched, use AppleScript or Automator to select specific menu items.

e.g. I'd like to launch Excel 2008 (I have the home/student edition which doesn't come preconfigured for Automator) and then click the "File" menu and click on "open".

Any pointers on where to go/look for how to select menu items like this (or if it's even possible at all)?

You can "sort of" do this using Automator's Record function, but Record is very brittle.

I'd rather be able to use AppleScript to simply grab an "array" that contains every menu item for the application and then programmatically click on the 0th menu item in my array…etc. etc.

Is this possible?

TIA

A: 

I don't know about grabbing an array, but this looks like it might work for sending the mouse clicks, etc:

VirtualInput

Peter Loron
+1  A: 

UI scripting is a fickle beast. It's brittle and often sensitive to many things beyond your control such as unexpected dialogs (from other applications), changes in system font or language, user activity. Not to mention changes in an application's UI across versions.

It's much more robust to use talk directly to the application via its AppleScript API. This is exactly the purpose of AppleScript. You can always see the actions and data an application can provide via AppleScript via the "Open Dictionary..." menu item in AppleScript Editor.app's (select the application you want to script in the file dialog). Instead of simulating a mouse click on a menu, you call the action that menu item triggers directly via AppleScript.

Of course not all applications have a complete (or even any) AppleScript API. Excel, however, has excellent AppleScript support, if that's your target.

If you're really trying to do UI testing, I recommend you start with other options:

  1. Test the model (analogous to scripting the app's model via AppleScript). In fact, if you include an AppleScript API in your app, you can automated testing of that API as well.
  2. Use Google Toolbox for Mac's unit testing additions (or roll your own) to programatically send mouse or keyboard events to your app.
  3. Use Instruments.app to record a UI sequence. The Instruments recording is solid (I haven't used Automator's Record feature), and you get all the other goodies of Instruments too.
Barry Wark
+1. If the Applescript dictionary has an "open" command, you are much better using that than UI scripting. UI scripting works better in theory than anything.
Philip Regan