Is there a Terminal command to open a Mac menu bar item?
Suppose I want to open a specific app which has an icon on the menu bar tray, is it possible to do it using a shell command?
In my case, there is an app called "Quick View Calendar" (available in Mac App Store) which shows up in the Menu bar, which I would like to open with a Terminal command and to use that code to run an Automator service to
Solution 1:
Since all that's required is a single click to the menu bar icon in order to open the app, AppleScript can achieve this very easily using GUI scripting, which requires the right accessibility permissions to be granted to whichever program you'll use to execute the AppleScript (e.g. Script Editor, Automator, Terminal, etc..).
So, first, grant the appropriate permissions via System Preferences. This screenshot is from MacOS 10.13:
After this is done, open up Script Editor and run this script. I don't have Quick Calendar View myself, but the principles for most menu bar apps are the same, so this will hopefully obtain a list of application names that have icons sitting in your menu bar:
tell application "System Events" to get the name of ¬
every process whose class of menu bar 2 is menu bar
"Quick View Calendar" or something appropriately similar ought to appear in the list that is returned by running that command. This is the name that I've used in the following snippet, which is what actually does what you need, namely to issue a mouse click to the menu bar icon and open the application. If the name is something different, you can change that yourself:
tell application "System Events" to tell process "Quick View Calendar" to ¬
click menu bar item 1 of menu bar 2
You can paste this code into a Run AppleScript action in Automator (again, provided Automator has been granted accessibility permissions), which will then become part of your workflow in creating the service you want to create.
Your original request was to run a command from Terminal to perform this action, so you can simply open up Terminal (again, accessibility permissions!), and type this:
osascript \
-e 'tell application "System Events" to ¬' \
-e 'tell process "Quick View Calendar" to ¬' \
-e 'click menu bar item 1 of menu bar 2'
If you run into any problems, let me know.
Solution 2:
Terminal (bash, csh, zsh, et. al.) doesn't "open" menu bar commands. This is what AppleScript is for - it scripts the GUI.
To get an application to "execute" an option/function on the command line that would normally accessed through the menu bar, you pass command line parameters. Typically, the command looks like this:
$ command -x [optional arguments]
The above command will execute with the option flag x
(where x
is any flag that the command accepts) and any optional arguments for that flag.
The caveat here is that the application must accept command line options. If it doesn't there will be nothing you can script from the command line to do what you want.
If that is the case, look to AppleScript, but be aware, that not all apps are scriptable under AppleScript.