Possible to access a shortcut menu choice with an AppleScript?

I’m working with a Web application that has some important functions in menus only accessible via control-left click. I am able to access that menu in my AppleScript by using

tell application “System Events”
tell process “Safari”
keystroke “5” using {control down}
end tell
end tell

but then I can’t figure out how to make a selection. Selecting the first letter of the command I want followed by the return key is not working. Is there some way to access this shortcut menu directly? Thanks.


you can’t access contextual menus with AppleScript

Are you saying that by hitting control-5 using system events, you are able to make the menu item you want pop up? If so, you could choose an item by moving up and down with the arrow keys and then selecting using the space bar.

tell application "System Events"
    tell process "Safari"
        keystroke "5" using {control down}
        delay 1
        key code 125  --down arrow
        key code 126  --Up arrow
        keystroke space
    end tell
end tell

Hi, StefanK
In System Preferences>> Universal Access >> Mouse, turn mouse keys on.
Then you can access the contextual menu using the script written by Matt-Boy.
Control + 5 (generally numeric keypad 5, i think) opens contextual menu.
Can you ask your genius mind to write a script which turns mouse keys on, open the contextual menu and then turn mouse key off? Or would such a script be too inefficient to serve any purpose? Every window switcher misses a keyboard shortcut for contextual menu.

Hi Chris,

it’s not difficult to write a script to turn Mouse Keys on or off,
but you can do it only with GUI scripting and I don’t know to turn the focus back to the target application

this toggles the value of Mouse Keys

on toggleMouseKeys()
	tell application "System Preferences"
		reveal anchor "Mouse" of pane id "com.apple.preference.universalaccess"
	end tell
	tell application "System Events"
		tell process "System Preferences"
			click (1st radio button of radio group 1 of tab group 1 of window "Universal Access" whose value is 0)
		end tell
	end tell
	quit application "System Preferences"
end toggleMouseKeys

i know that :). That is why i asked if the script would be efficient to serve any purpose. With GUI scripting, only the manual work of turning mouse keys on and off is assigned to the script. I was expecting a solution without GUI scripting which silently turns mouse keys on and off and that too in no time.

Thanks very much for this. I had tried something very similar and it wasn’t working, but I was missing the delay 1. I think that was the problem, need to go try that!