I am trying to create an Applescript to automate an interaction with an application that has a third-party menulet (menu bar item, top right corner). The application in question is called Pomodoro One. It does not have built-in Applescript support, so I want to create a script that clicks on its menu bar and presses "Start Pomodoro".
After hours of research on the Internet and experimentation with Script Editor, plus the creation of a visual automation with Automator, I have come up with this script that SHOULD work:
tell application "Pomodoro One"
activate
end tell
tell application "System Events"
tell UI element "Pomodoro One"
tell menu bar item 1 of menu bar 2
click menu item 1 of menu 1
end tell
end tell
end tell
However, it does not work. Surprising, especially since the the automation in Automator works and this script is basically a paraphrasing of the Applescript that Automator converts its visual automation into. This is the error that I get, both from ScriptEditor as well as running the Applescript in Automator:
System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of UI element "Pomodoro One". Invalid index.
What I think is happening is that when Applescript attempts to click on menu bar 2
(which represents the menulet), the menu that is supposed to appear does not. As if the click is totally not happening. However, I am indeed able to click on menu bar 1
(which represents Pomodoro One's main menu bar). This is stumping why clicking on menu bar 2
yields nothing, especially since UI debugging apps like UI Browser indeed report that the menulet is represented by menu bar 2
when I do visual profiling of the UI.
I am hours into this and I feel very stuck. I am wondering if there is any way that I can get Applescript to play nice with the menulet of any third party applicaton.
EDIT
Based on this post (https://stackoverflow.com/questions/20595708/applescript-to-click-on-a-specific-icon-in-the-mac-menu-bar), I modified my script to be the following:
ignoring application responses
tell application "System Events" to tell process "Pomodoro One"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Pomodoro One"
tell menu bar item 1 of menu bar 2
click menu item "Start Pomodoro" of menu 1
end tell
end tell
This time, it results in the script infinitely spinning and eventually timing out with error -1712: System Events got an error: AppleEvent timed out.
I can assure you that I have accessibility enabled, via System Preferences –> Security and Privacy –> Accessibility for both Pomodoro One and Script Editor.
Best Answer
I encountered the same problem (the script infinitely spinning) in macOS Sierra. I spent some time using Automator and was able to solve it by modifying the script slightly: