How can I get access to menu bar item in Bartender 3 using AppleScript? 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
            click

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

                if exists menu item "Connect" of menu 1 then
                    click menu item "Connect" of menu 1
                    return "Connecting..."
                else
                    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?
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:

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 &gt; Security &amp; Privacy &gt; Privacy &gt; 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 &quot;System Events&quot; to get every process whose name contains &quot;bartender&quot;

I ran a similar command for &quot;dropbox&quot; and it returned this:
    --&gt; {application process &quot;Dropbox&quot;, 
         application process &quot;DropboxActivityProvider&quot;}

I imagine Bartender will have one or two processes at most as well.  One of them is probably called &quot;Bartender&quot;.  Choose that one, unless the other one has an obviously helpful name, such as &quot;BartenderMenu&quot; (I doubt it).
Now run this, substituting in the appropriate process name (which is case-sensitive):
tell application &quot;System Events&quot; to tell process &quot;Bartender&quot; to get every menu bar

Hopefully, it returns this:
    --&gt; {menu bar 1 of application process &quot;Bartender&quot; of application &quot;System Events&quot;, 
         menu bar 2 of application process &quot;Bartender&quot; of application &quot;System Events&quot;}

menu bar 1 is the menu bar containing the 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 &quot;System Events&quot; to tell process &quot;Bartender&quot; 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 &quot;System Events&quot; 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 &quot;Bartender&quot; (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. &quot;Quit Bartender 3&quot;).
Here's the command that will access the menu:
tell application &quot;System Events&quot; to tell process &quot;Bartender&quot; 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 &quot;System Events&quot; to tell process &quot;Bartender&quot; 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 &quot;Quit Bartender 3&quot; 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.