Jump to content

How do I click a menu item of application in Mac menubar?


Recommended Posts

Link to workflow: workflow_link.

 

Using the information from this apple stackexchange link, I tried to create an apple script to click the menu "Get LaTeX" from the app called "Mathpix Snipping Tool".

 

NOTE: The Mathpix Snipping Tool is a great app, it can get the OCR of any image and converts it into LaTeX item.

Since it is difficult to remember so many keyboard shortcuts, I decided to write alfred keyword to simulate "ctrl-cmd-m" of this app, but got 

problem and posted the question.

 

Here is the code:

tell application "Mathpix Snipping Tool"
    activate
end tell
tell application "System Events"
    click menu item "Get LaTeX" of menu 1
end tell

 

When I run the first part in terminal using `osascript myscript.scpt` it runs fine. However, when I run the whole code it gives error.

mymathpix.scpt:94:138: execution error: System Events got an error: Can’t get menu 1. (-1728)

The application is not a regular application like Safari with menu items, its just hanging in top of the menubar and we can use some keyboard shortcuts to run the commands.

 

How to fix the problem?

 

Thanks.

mathpix.png

Edited by Bhishan
fixed typo
Link to comment
  • deanishe changed the title to How do I click a menu item of application in Mac menubar?

@Bhishan, this is the general form the AppleScript code will take when trying to access a menu bar icon and its menu:

 

use application "System Events"

 

property P : a reference to the process named "Mathpix Snipping Tool"

property M : a reference to menu bar item 1 of menu bar 2 of P

property I : a reference to the menu item named "Get LaTeX" of menu 1 of M

 

click M

click I

 

It doesn't work for every single menu bar application, and there's an annoying 5-second delay between the first click command and the second.  This is a known, persistent feature that has various documented so-called solutions, none of which are reliable, but you can have a Google and see what works for you.

 

Don't forget to make sure Terminal (or whichever program is executing the above script) has accessibility privileges granted to it.

Edited by CJK
Link to comment
  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...