7

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

3
  • Can you describe what steps you normally take to open the app ? For example, is it simply a matter of clicking on the icon in the menu bar, or do you have to right-click and select a menu option ? Is there a keyboard shortcut that opens the app ? If you can answer these questions, I might be able to provide you with a workable solution.
    – CJK
    Commented Feb 24, 2018 at 13:42
  • Its a single click to open the app. There is no shortcut by default.
    – Rathan
    Commented Feb 25, 2018 at 11:32
  • If you really want to do it by TERMINAL and your chosen answer does NOT help you –as some apps are unscriptable– there's an all-round solution described on the link below, that makes a simple "click x y" command available. (On that page you'll find procedures how to install it in 10 minutes' time.) stackoverflow.com/questions/4230867/… Commented Nov 11, 2018 at 13:58

3 Answers 3

6

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:

System Preferences on 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.

1

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.

1
  • There's always GUI scripting (but it needs accessibility permission).
    – SilverWolf
    Commented Feb 25, 2018 at 2:40
0

You can compile a very simple 14-line Foundation framework app, that will from then on give you the ability to click any point x/y onscreen,

[ Contents drawn back due to massive anonymous down-voting … ]

1
  • [ If you down-vote an answer that's meant to help others (or at least gives hints) please be so honest to write one short sentence WHY you don't "like" it ! ] Commented Nov 11, 2018 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .