Help! Can AppleScript access context menu?
Hello Apple fans,
I am a long time Mac user, but very new to AppleScript. I've been working through some online tutorials, learning some of the basics, and I've made some progress. However, I am completely stumped with what I'm trying to accomplish. Any help that you scripting experts can provide would be most appreciated.
This is what I'm trying to do: I would like to make an AppleScript that can access the right-click (or Control-click) context (or contextual) menu in Safari.
For example, if there is an image on the page in Safari, if you right-click it, a context menu pops up with various commands: "Open Link in New Tab", "Copy Link", "Save Image As...", etc. I'm using "Open Image in New Tab" just to test whether AppleScript is indeed accessing this menu. I'm hoping I can substitute another command once I prove that the script actually works.
This is what I've done so far:
- I navigate to a web page with an image.
- Using the application "UI Browser", I can find what the pathway to access that image using UI Scripting.
- I can copy and paste that pathway into my AppleScript Editor.
- I make some minor adjustments to the actions I'm hoping to perform.
This is what "UI Browser" gives me as a typical pathway to an onscreen image:
application "Safari"
standard window "NAME OF WEBPAGE" (window 1)
group (group 3)
group (group 1)
group (group 1)
scroll area (scroll area 1)
HTML content (UI element 1)
group (group 6)
link (UI element 1)
image (image 1)
ACTIONS:
press
show menu
After getting the above pathway, I then right-click the image and choose "Open Image in New Tab" and this is what "UI Browser" displays:
ELEMENT:
Role: menu item
Title: "Open Image in New Tab"
Description:
Help:
Application: Safari
ELEMENT PATH (FROM LEAF ELEMENT):
menu item "Open Image in New Tab" (menu item 12)
menu (menu -9223372036854775808)
[MISMATCH-different children] application "Safari"
ACTIONS [2]:
cancel
press
So this is what my AppleScript looks like:
tell application "System Events"
tell process "Safari"
tell image 1 of UI element 1 of group 6 of UI element 1 of scroll area 1 of group 1 of group 1 of group 3 of window 1
click
end tell
end tell
end tell
When I run this script, it succesfully clicks the image (the image automatically changes when clicked, so I know it works). However, I cannot figure out how to access the context menu, as if I were right-clicking the image. That is what is stumping me: the context menu, and executing commands in it.
I've tried "perform action "ShowMenu"", or "perform action "Show Menu"". I've tried "click menu item 12 of menu 1", or "tell menu item 12 of menu 1" "press". I've tried "click "Open Image in New Tab"", and variations such as that. So far, I just can't figure it out!
If you've made it this far reading my question, thank you! If you can offer any assistance with this, I would sincerely appreciate it.
Thank you very much,
Melissa
iMac (27-inch Late 2009), Mac OS X (10.6.8)