Clicking an app in the system menu bar

Occasionally, my Dropbox will stop synchronizing. I have a trick to wake it up, which I’ve turned into a KM macro, but I would like KM (or Applescript within a KM macro) to click the Dropbox item in the system menu bar at the end of the macro so that I can see if the technique actually got Dropbox to come to its senses.

Since the position of the Dropbox icon moves depending on what else is going on with the system menu bar, how would I tell KM to always click the Dropbox icon, wherever it happened to be?

Selecting status menu items is difficult primarily because there is no easy way of specifying them - they don’t have names so its hard to identify them.

You can use a Click at Found Image, although even that can be difficult as they often animate or change icons based on various states. If the icon does not animate or change state (or has a known state when you want to select from it), then this can be a good solution, followed by Insert Text by Typing “Menu Name%Return%”

To click relative to an image, you use the Mouse Click action, configure it to be relative to a found image, and take a screenshot of the desired area of the screen using Command-Control-Shift-4, and paste it into the image well on the action. The image has to be unique (which includes not being visible in the action if the image is small enough not to be shrunk in the image well) otherwise Keyboard Maestro will not know where to click. The Display option in the action will allow you to see where Keyboard Maestro is matching.

Hey Peter,

What’s the trick? Please share.

As for clicking the Dropbox menu try this AppleScript:

tell application "System Events"
  tell UI element "Dropbox"
    tell menu bar item 1 of menu bar 2
      perform action "AXPress"
    end tell
  end tell
end tell

It works on Mavericks, but it may need to be tweaked for Yosemite.

-Chris

1 Like

Stick something into a Dropbox, leave it there for a few moments, and then, if you wish, remove it. That will usually wake Dropbox up. It doesn’t matter what, so I created a simple text file that says “Wake up, Dropbox!” and store it on my desktop. I then use an equally simple macro to copy it to a folder in Dropbox, leave it there for 15 seconds, and then delete it from Dropbox. I got stuck though, on finishing the macro so that it would auto open Dropbox’s menu item so I could check to see if it worked, which is why I asked my original question.

If it didn’t work, then I have another simple macro that uses two simple Applescript items separated by a brief pause:

Step 1:
tell application "Dropbox"
quit
end tell

Step 2:

Pause for 1 second

Step 3

tell application "Dropbox"
launch
end tell

Doing the quit/restart process however, is a last resort, because it takes a few minutes for Dropbox’s app to go through a all of its own steps during its startup to get things going anew. However, it only takes about 30 seconds using the first trick to wake Dropbox up.

1 Like

Thanks, Chris! That worked exactly as I hoped, on Yosemite (10.10.4) without any changes.

Thanks for the icon image idea, Peter. I’ve got a use for that immediately!

I believe this should be in the wiki. It took a whole day of searching for this before I eventually stumbled on this page.

Hey Mirizzi,

There’s a lot of stuff that needs to be in the wiki.   :smile:

If you don’t find something after an hour+ of searching then it’s a good idea to ask.

Note my post [ here ].

-Chris

I added it to the Move or Click Mouse action wiki page.

Of course, the wiki is editable, so if anyone would like to add stuff to the wiki, they are more than welcome to do so (contact support for a login).

That’s awesome. I was looking for a reliable way to do this. Thanks!

1 Like

For general reference, this command will return a list of those apps sitting in the menu bar:

    tell application "System Events" to ¬
    	get the name ¬
    		of every process ¬
    		whose class ¬
    		of menu bar 2 is menu bar

To retrieve an object reference for some arbitrary menu bar app’s menu icon:

    tell application "System Events" to ¬
        get menu bar item 1 of menu bar 2 of ¬
	        (some process ¬
        		whose class ¬
        		of menu bar 2 ¬
	        	is menu bar)
2 Likes