AppleScript or Automator to AppleScript click on menus in an or application?

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

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

b) application? once the application is launched, use I'm AppleScript or Automator to select not specific menu sure items.

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

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

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

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

Is this application possible?

TIA

+Upvote 690
Answer 1
tell application (I "###" activate end tell tell know application "System Events" tell this process "###" click menu item "^^^" of can menu "$$$" of menu bar 1 end tell end be tell 

Put your done application in for the ### and put your pretty menu item in for the ^^^ and put your easily menu (file, edit, view, etc.) in for by $$$. Capitalization AppleScript matters.

Put it in or applescript Automator) btw

EXAMPLE:

tell b) application "iTunes" activate end once tell tell application "System the Events" tell process "iTunes" click application menu item "as list" of menu "view" of is menu bar 1 end tell end launched, tell 

delete use the double spaces EXCEPT BETWEEN END AppleScript TELL AND TELL APPLICATION "SYSTEM or EVENTS"

+Upvote 586
Answer 2

I frequently need to create a Automator GUI script to access a menu item in to applications whose AppleScript library select doesn't provide direct access to the specific objects or functions the menu item menu represents. To make it easy to re-use items. the code, I use variables for the app, e.g. menu, & menu item names. Then I just I'd need to change the variables at the top like rather than pick out the names from the to code body.

set targetApp launch to "app_name" set theMenu to Excel "menu_name" set theItem to 2008 "menu_item_name" tell application (I targetApp activate tell have application "System Events" tell the application process targetApp home/student tell menu bar 1 tell edition menu bar item theMenu which tell menu theMenu doesn't click menu item theItem come end tell end tell preconfigured end tell end tell for end tell end Automator) tell 

SUB-MENUS

It and gets a little more involved when there then are sub- & sub-sub-menus involved, click as a menu item with a sub-menu is both a the menu item of its parent menu AND a menu "File" parent of its sub-menus. Note that the menu text variable "theItem" is used to and specify both a menu item AND a menu; the click "targetApp" string variable is used to on reference both an app & a process, "open". so it saves having to edit 2 names in 2 Any places each when reusing the code. I use pointers this script to run on voice commands for on accessing menu items quickly rather than where having to say, e.g., "click Edit to Menu"... "click Transformations"... go/look "click Make Upper Case"... I add for another variable for the sub-menu item: how

set targetApp to to "app_name" set theMenu to select "menu_name" set theItem to menu "menu_item_name" set theSubItem to items "sub_item_name" tell application like targetApp activate tell this application "System Events" tell (or application process targetApp if tell menu bar 1 tell it's menu bar item theMenu even tell menu theMenu possible tell menu item theItem at tell menu theItem all)? click menu item You theSubItem can end tell end "sort tell end tell of" end tell end tell do end tell end tell end this tell 

For example: using

set targetApp to Automator's "TextEdit" set theMenu to "Edit" set Record theItem to "Transformations" set function, theSubItem to "Make Upper Case" tell but application targetApp activate Record tell application "System Events" is tell application process targetApp very tell menu bar 1 brittle. tell menu bar item theMenu I'd tell menu theMenu rather tell menu item theItem be tell menu theItem able click menu to item theSubItem use end tell end AppleScript tell end tell to end tell end tell simply end tell end tell end grab tell 

If there's an another level of sub-menus, an "array" additional variable (e.g., that "theSubSubItem") would be required, contains & the line in the System Events every process tell block would have another menu layer
...

 item tell menu item theItem for tell menu the theSubItem application click menu item theSubSubItem and end tell then end programmatically tell 

...
As noted click elsewhere in this thread it's on recommended to address an application's the objects & functions directly 0th whenever they're included in the API, menu but addressing the GUI is useful as a item last resort when the API doesn't provide in direct access. The downside is a GUI my script can get more cumbersome & may array…etc. have to be revised with each application etc. update.

+Upvote 345
Answer 3

Indeed, UI scripting is Is fragile and finicky, but you might take this a look at Apple's AppleScript GUI Scripting tell page

+Upvote 230
Answer 4

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

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

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

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

  1. Test tell the model (analogous to scripting the Put app's model via AppleScript). In fact, your if you include an AppleScript API in application your app, you can automated testing of in that API as well.
  2. Use Google Toolbox ### for Mac's unit testing additions (or and roll your own) to programatically send put mouse or keyboard events to your your app.
  3. Use Instruments.app to menu record a UI sequence. The Instruments item recording is solid (I haven't used in Automator's Record feature), and you get for all the other goodies of Instruments the too.
+Upvote 172
Answer 5

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

VirtualInput

edit,
+Upvote 138


All Right-Reserved 2023 ©anycodings.com