The example AppleScript code, shown below, was tested in Script Editor and as an Automator Service/Quick Action under macOS Catalina and macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
- 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Example AppleScript code:
ignoring application responses
tell application "System Events" to ¬
click menu bar item 1 of ¬
menu bar 2 of ¬
application process "TextInputMenuAgent"
end ignoring
delay 0.1
do shell script "killall 'System Events'"
delay 0.2
tell application "System Events"
launch
click menu item 1 of ¬
menu 1 of ¬
menu bar item 1 of ¬
menu bar 2 of ¬
application process "TextInputMenuAgent"
end tell
Notes:
In the second tell application "System Events"
block:
Change:
click menu item 1 of ¬
To:
The actual name of the menu item, e.g click menu item "U.S." of ¬
or whatever the actual name is.
You can also change the number e.g. 1
to whatever number is appropriate.
As tested, this works for me without issue and, sans the built-in three tenths of a second combined delay, was instantaneous.
If the example AppleScript code shown above does not work for you, then use the Using: cliclick method shown in my answer AppleScript - Can't get rid of delay after click
Or for a more robust solution also using cliclick
, a third-party command line utility, the following example AppleScript code can be used for more than the immediate menu item you are trying to click.
Example AppleScript code:
-- # This script works on menu bar items in menu bar 2.
-- # Set the name of the application process and the
-- # name of the target menu item. It will use cliclick
-- # a third-party command line utility to perform the
-- # clicks as a workaround to the 5 second delay bug.
-- #
-- # Note that this requires the target menu item to have
-- # a name that System Events can retrieve properties for.
-- # It is from these properties it position and size is
-- # ascertained in order to calculate where to click it.
-- # User set properties.
property appProcessName : "TextInputMenuAgent"
property menuItemName : "U.S."
property cliclick : "/usr/local/bin/cliclick"
-- # The code below is tokenized and
-- # should not need to modified.
property xPos : missing value
property yPos : missing value
tell application "System Events" to ¬
set thePositionSizeList to ¬
the {position, size} of ¬
menu bar item 1 of ¬
menu bar 2 of ¬
application process ¬
appProcessName
my setXposYposFrom(thePositionSizeList)
my clickAtXposYpos()
tell application "System Events"
set theTargetMenuItemProperties to ¬
properties of ¬
first menu item of ¬
menu 1 of ¬
menu bar item 1 of ¬
menu bar 2 of ¬
application process ¬
appProcessName whose ¬
name is menuItemName
set thePositionSizeList to ¬
the {position, size} of ¬
theTargetMenuItemProperties
end tell
my setXposYposFrom(thePositionSizeList)
my clickAtXposYpos()
-- ## Handlers ##
to setXposYposFrom(thisList)
set xPos to ¬
(item 1 of item 1 of thisList) + ¬
(item 1 of item 2 of thisList) / 2 ¬
as integer
set yPos to ¬
(item 2 of item 1 of thisList) + ¬
(item 2 of item 2 of thisList) / 2 ¬
as integer
end setXposYposFrom
to clickAtXposYpos()
set shellCMD to {¬
cliclick, " -r c:", ¬
xPos, ",", yPos} as string
do shell script shellCMD
end clickAtXposYpos
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.