Help with Applescript not working in BTT

I use this applescript to click a custom menubar icon configured in BTT:

try
	tell application "System Events"
		tell application process "BetterTouchTool"
			tell menu bar item 2 of menu bar 2
				click
			end tell
		end tell
	end tell
on error the_error
	return the_error
end try

It works good in applescript, but if I use it as an action in BTT, I get this error:
System Events got an error: Can’t get menu bar 2 of application process "BetterTouchTool". Invalid index.

Any ideas to fix it?

The script isn't working for me, neither with the script editor nor with BTT. If I understand you correctly, this sequence should, however, accomplish the same task.

Thanks @SLE

The applescript is not clicking a menu bar item of the titles menu. It clicks an icon item of the menubar, like the icon of Control Center. But in my case, the applescript clicks a custom menubar icon configured in BTT.

You can test the following applescript. It only changes the menu bar item number (1 instead of 2). This one clicks the default status bar icon of BTT.

try
	tell application "System Events"
		tell application process "BetterTouchTool"
			tell menu bar item 1 of menu bar 2
				click
			end tell
		end tell
	end tell
on error the_error
	return the_error
end try

either this one

or this one

try

tell application "System Events"

tell application process "BetterTouchTool"

-- Click on the menu item

click menu bar item 2 of menu bar 2

delay 0.5

-- Simulate one arrow down key presses

key code 125 -- Arrow Down

-- Simulate pressing the Return key

key code 36 -- Return

end tell

end tell

on error the_error

return the_error

end try

works for me

I have the menu bar configured as "hide always automatically" in macOS System Configuration.

The "Click Menu Bar Status Item" action in BTT only works for me if the menubar is visible.

The applescript you wrote works fine if I run it from an external applescript, but not if I run it inside BTT. Can you run it inside BTT with menu bar hidden?

I doubt you can activate something that is not visible - but I am certainly no expert in that regard.

I can activate any menu bar item running the code from an external applescript, it doesn't matter if the menu bar is visible or hidden.

The problem is that the same code gives an error if I run it inside an applescript action inside BTT.

@Andreas_Hegenberg

I have my main menu bar set to hide automatically in macos system configuration.

question 1 - The BTT action "click menu bar status item" doesn't work when the main menu bar is hidden. Is it possible to make it work even the manubar is hidden?

question 2 - I use this applescript to click a custom menubar icon configured in BTT:

try
	tell application "System Events"
		tell application process "BetterTouchTool"
			tell menu bar item 2 of menu bar 2
				click
			end tell
		end tell
	end tell
on error the_error
	return the_error
end try

It works good in external applescript, but if I use it in a BTT action, I get this error:
System Events got an error: Can’t get menu bar 2 of application process "BetterTouchTool". Invalid index.
Why I get this error and what I should do to fix it?

Thanks.

If you just want to trigger the actions assigned to a custom menubar item in BTT, the easiest solution would be to use execute_assigned_actions_for_trigger. The required UUID of the trigger can be received by right-clicking the custom menu trigger in BTT.

tell application "BetterTouchTool" to execute_assigned_actions_for_trigger "69983672-05EF-4B01-9499-EEAB41D00917"

Apple Script is a bit limited/complicated when doing UI-scripting in the app that is executing the script.

1 Like

Easy and fast solution!
Thanks!

There is only one menu bar status icon that I can not click from my floating menu with the hidden menu bar, that is the BTT icon:

Any idea of how can I click it?

Thanks!

I can't think of a good way right now. However I think it makes sense to offer showing that menu as an action in BTT - then people could also easily replace the BTT menubar icon with a custom one.
I'll have a look!

Yessssssss!! Good solution!!