Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. -- `menu_click`, by Jacob Rus, September 2006
  2. --
  3. -- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
  4. -- Execute the specified menu item. In this case, assuming the Finder
  5. -- is the active application, arranging the frontmost folder by date.
  6.  
  7. on menu_click(mList)
  8. -- Validate our input
  9. if mList's length < 3 then error "Menu list is not long enough"
  10.  
  11. -- Set these variables for clarity and brevity later on
  12. set appName to item 1 of mList
  13. set topMenu to item 2 of mList
  14. set r to (items 3 through (mList's length) of mList)
  15.  
  16. -- This overly-long line calls the menu_recurse function with
  17. -- two arguments: r, and a reference to the top-level menu
  18. tell app "System Events" to my menu_click_recurse(r, ((process appName)'s (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
  19. end menu_click
  20.  
  21. on menu_click_recurse(mList,parentObject)
  22.  
  23. -- `f` = first item, `r` = rest of items
  24. set f to item 1 of mList
  25. if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
  26.  
  27. -- either actually click the menu item, or recurse again
  28. tell app "System Events"
  29. if mList's length is 1 then
  30. click parentObject's menu item f
  31. else
  32. my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
  33. end if
  34. end tell
  35. end recurse
  36.  
  37. --========================================================================--
  38. --========================================================================--
  39.  
  40. -- To show how amazingly much better this function is than the way that
  41. -- Apple expects users to use
  42.  
  43. -- my new way
  44. menu_click({"TextEdit", "Edit", "Speech", "Start Speaking"})
  45.  
  46. -- the old way:
  47. tell app "System Events"
  48. tell ((process "TextEdit")'s (menu bar 1)'s (menu bar item "Edit")'s (menu "Edit")'s (menu item "Speech")'s (menu "Speech")) to click menu item "New"
  49. end tell
  50.  
  51. -- or Apple's way on apple.com:
  52. tell app "System Events"
  53. tell process "TextEdit"
  54. tell menu bar 1
  55. tell menu bar item "Edit"
  56. tell menu "Edit"
  57. tell menu item "Speech"
  58. tell menu "Speech"
  59. click menu item "Start Speaking"
  60. end tell
  61. end tell
  62. end tell
  63. end tell
  64. end tell
  65. end tell
  66. end tell
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement