How to script Safari's contextual menu "copy image" ?

I’m looking for a way to script the contextual menu “copy image”.
You get this option when right clicking on an image in Safari.
As there is no real selection, I cannot ask for a copy selection.



You need to have both cliclick and MouseLocation for this to work: (you’ll find them both via google.

(You will have to adjust the paths for the do shell scripts and such)

-- McUsr
set mouseLoc to (do shell script "~/opt/bin/MouseLocation")
set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
tell mouseLoc to set {mouseX, mouseY} to {it's text item 1, it's text item 2}
set AppleScript's text item delimiters to astid
set {mouseX, mouseY} to {(mouseX as integer), 1200 - (mouseY as integer)}
set frontProcessName to ""
tell application "System Events"
	set frontProcessName to name of every process whose frontmost is true
	--	tell a to set aa to (get its name)
	set wnCount to count of windows of process named frontProcessName
	if wnCount > 0 then
		tell window 1 of process named frontProcessName
			set wnPos to its position
			set wnsize to its size
		end tell
		set {wnX, wnY, wnWidth, wnHeight} to {item 1 of wnPos, item 2 of wnPos, item 1 of wnsize, item 2 of wnsize}
		set {leftBound, RightBound, upperBound, lowerBound} to {wnX + 1, (wnX + wnWidth - 21), wnY + 50, (wnY + wnHeight - 51)}
		if mouseX ≥ leftBound and mouseX ≤ RightBound then
		else if mouseX < leftBound then
			set mouseX to leftBound
			set mouseX to RightBound
		end if
		if mouseY ≥ upperBound and mouseY ≤ lowerBound then
		else if mouseY < upperBound then
			set mouseY to upperBound
			set mouseY to lowerBound
		end if
	end if
end tell
set mouseLoc to "c" & mouseX & " " & mouseY
do shell script "~/opt/bin/cliclick " & mouseLoc & " &"
delay 0.2
tell application "System Events"
	tell application process frontProcessName
		key code 125
		key code 125
		key code 125
		key code 125
		key code 125
		key code 125
		key code 125
		key code 36
	end tell
end tell


I usually use this, without the last System Events block as a general way to access the contextual menu from a short cut, I have assigned it to ctrl-F10 in Quicksilver.

You’ll need a

tell application "Safari" to activate

to prepend it, and assign it to a shortcut key of your choice in your favorite utility for firing off Applescript by keyboard shortcuts.

By the way: The last System Events block presses arrow down 7 times before hitting enter, as that is what it takes on my version of Safari, to choose the copy image.

And the idea is that the mouse pointer is over the image of course, so the context is correct.

Thanks for this quick reply !