How can I get access to menu bar item in Bartender 3 using AppleScript?

Solution 1:

I'm gathering from your wording that you want to access a menu item of Bartender 3 that sits in the menu bar in much the same way as my Dropbox app does:

MacOS Menu Bar

Hopefully I've this you correctly, not having Bartender myself. However, figuring out how to access the menu item using AppleScript is not too difficult. As you've been doing already, we'll use GUI scripting to do this, so make sure Script Editor has permission to control your computer under System Preferences > Security & Privacy > Privacy > Accessibility (MacOS High Sierra).

Because I don't have Bartender myself, I need you do do some detective work in AppleScript for me (but it's good practice, as it shows you how to solve problems like this in the future):

Part 1: Accessing the Menu Icon

Run this command:

tell application "System Events" to get every process whose name contains "bartender"

I ran a similar command for "dropbox" and it returned this:

    --> {application process "Dropbox", 
         application process "DropboxActivityProvider"}

I imagine Bartender will have one or two processes at most as well. One of them is probably called "Bartender". Choose that one, unless the other one has an obviously helpful name, such as "BartenderMenu" (I doubt it).

Now run this, substituting in the appropriate process name (which is case-sensitive):

tell application "System Events" to tell process "Bartender" to get every menu bar

Hopefully, it returns this:

    --> {menu bar 1 of application process "Bartender" of application "System Events", 
         menu bar 2 of application process "Bartender" of application "System Events"}

menu bar 1 is the menu bar containing the Apple Menu (Apple Menu) in the top-left. menu bar 2 (if it exists) will be the menu bar containing the application icon.

Now you should be able to target that menu icon with this command:

tell application "System Events" to tell process "Bartender" to ¬
    click menu bar item 1 of menu bar 2

Hopefully that should sort you out. For completeness, here's a one line job that gets all the menu bar apps in one go:

tell application "System Events" to get menu bar 2 of every process

It will return an arrange, most of whose items will be missing value (representing processes that don't have a second menu bar—the only ones that do are menu bar apps). In amongst those missing values will be the names of apps that sit up top, and you can probably just pick out "Bartender" (or whatever it'll be called) straight away using this method.

Part 2: Accessing the Menu and its Menu Items

If, when you normally click the menu app icon for Bartender, it displays a menu, then you'll want to know how to access those menu items from AppleScript as well.

The menu itself doesn't come into existence until the icon has been clicked and the menu appears. The menu itself is (or will be) called menu 1. Its individual menu items are called menu items and referenced by their name (which is the text of the menu item itself, e.g. "Quit Bartender 3").

Here's the command that will access the menu:

tell application "System Events" to tell process "Bartender" to ¬
    get menu items of menu 1 of menu bar item 1 of menu bar 2

However, you need to bring the menu into existence first, otherwise it will just throw an error or return missing value. Therefore, run the command to click the menu app icon first, then run that one:

tell application "System Events" to tell process "Bartender" to ¬
    tell menu bar item 1 of menu bar 2
        click
        get menu items of menu 1
    end tell

And, similarly, you can issue a click command to a menu item of your choice by referencing its name:

click menu item "Quit Bartender 3" of menu 1

(suitably enclosed in the relevant tell statements/blocks).

Finally: That Annoying Delay

Annoyingly, there'll be a 5-second delay between issuing a click command and retrieving the list of menu items. I don't know why, but I do know it's a standard issue that other users report. Here's the issue raised over on Stack Overflow together with someone's alleged fix for the problem (I haven't tested this myself).

Any questions or problems, leave a comment and I'll do my best to help.