For the software on the top-right menu bar, I can use the following AppleScript to access the menu bar items like this

activate application "SystemUIServer"
set deviceName to "Somebody’s AirPods"

tell application "System Events"
    tell process "SystemUIServer"
        set bluetoothMenu to (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
        tell bluetoothMenu

            set deviceMenuItem to (menu item deviceName of menu 1)
            tell deviceMenuItem

                if exists menu item "Connect" of menu 1 then
                    click menu item "Connect" of menu 1
                    return "Connecting..."
                    set batteryLevelMenuItem to (menu item 3 of menu 1)
                    tell batteryLevelMenuItem
                        set batteryLevelText to title of batteryLevelMenuItem
                    end tell

                    key code 53 -- esc key

                    return batteryLevelText
                end if
            end tell
        end tell
    end tell
end tell

This AppleScript helps me connect to my AirPods. But if I put the bluetooth item into Bartender 3, it doesn't work. What should I add into the codes to get access to item in Bartender?

  • AppleScript integration is application dependent, and–AFAIK–Bartender doesn't offer this as of now. Your best bet will probably be GUI scripting.
    – JMY1000
    Commented Jan 7, 2018 at 7:16
  • @JMY1000 Thank you! I have no idea what is GUI scripting, but I want to learn it. Where should I start?
    – Patrick
    Commented Jan 7, 2018 at 13:44
  • 1
    @Patrick, GUI scripting is precisely what you’ve done in your code block above. It’s using System Events to access GUI (graphical user interface) elements and sending them action commands as if the user had performed a physical action upon the element himself, e.g. click.
    – CJK
    Commented Jan 7, 2018 at 14:11

1 Answer 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
        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.

  • 4
    Instead of giving me fish, you just told me how to fish. Thank you very much! BTW, I just found out the fastest way to access items in Bartender 3;you just need to add a keystroke to show Bartender bar items(the shortcut in the preferences of Bartender). But I'm using your method to play with the menu bar 2.
    – Patrick
    Commented Jan 8, 2018 at 15:39
  • 4
    Linked my SO account to this exchange just to upvote you. Stupidly thorough, awesome answer. Thanks so much!
    – tknickman
    Commented Apr 29, 2018 at 2:08

You must log in to answer this question.

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