How to code an AppleScript to do a mouse click on a menu bar item

applescript

I have an awesome Pomodoro application, but there are no keyboard shortcuts to start and pause it. I've been trying to code an AppleScript for the past hour, reading the AppleScript reference and searching online, but I'm getting nowhere.

What I want to do is have an AppleScript (it will be launched from an app called BetterTouchTool) click on coordinates on the menu bar, (the Pomodoro app runs in the background and I have all the coordinates needed), a cursor-back needed for the second cursor back (to get back to the initial cursor position), click a second time on the 'Start/Pause' dropdown item (from coordinates), and then do a second cursor-back to have the cursor back to its original position.

EDIT: the application is a timer application (in the menu bar) with a dropdown menu that has as the first item either 'Start', 'Pause' or 'Resume'—which is what I want to be able to click via a keyboard-assigned AppleScript.

EDIT2: got this far:

tell application "System Events" 
    click (click at {1447, 10}) 
end tell 
tell application "System Events" to tell process "Pomodoro Timer" 
    click (click at {1456, 37}) 
end tell

But don't know how to code the cursor back, and I get this error:

System Events got an error: Can’t get menu bar item "00:58" of menu
bar 2 of application process "Pomodoro Timer".

Could someone please help me out?

Best Answer

There's a scripting addition called "AppleScript Toolbox" (it's an osax). Get it at https://astoolbox.wordpress.com/

With that, you can simply write:

AST click at {10, 20}

That clicks into the menu bar, for instance, and opens the Apple menu.

If you want to preserve the original cursor position first, use this:

set origPos to AST mouse point location

And to restore it use:

AST set mouse point location origPos