Skip to content

Instantly share code, notes, and snippets.

@ChristoferK
Last active September 11, 2018 05:03
Show Gist options
  • Save ChristoferK/f703de49e37185c185ccd20932a3f7ed to your computer and use it in GitHub Desktop.
Save ChristoferK/f703de49e37185c185ccd20932a3f7ed to your computer and use it in GitHub Desktop.
[Click Menu Item] A code refactoring of the original pair of handlers, now unified into a single handler that receives an application and a list of strings that represent a path through the application's menu tree to the menu item that is to be clicked. #AppleScript #recursion #menus #menu_items #System_Events
to clickMenuItem(pObj, mItems as list)
local pObj, mItems
if mItems = {} then return
script menuItem
use application "System Events"
property m0 : item 1 of mItems
property mN : rest of mItems
property |?| : pObj's class = menu
on mObj()
if |?| = false then
set A to pObj's name
activate the application named A
set M to a reference to the ¬
menu bar item named m0 of ¬
menu bar 1 of the ¬
process named A
else
set M to a reference to the ¬
menu item named m0 of ¬
pObj
end if
end mObj
property mx : my mObj()
property valid : mx exists
property M : a reference to the menu named m0 of mx
end script
tell the menuItem
if its valid = false then return
if its mN = {} then return «event prcsclic» its mx
my clickMenuItem(contents of its M, its mN)
end tell
end clickMenuItem
clickMenuItem(application "Safari", ["Bookmarks", "Edit Bookmarks"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment