UI Scripting contextual menus

How to choose and execute a menu item in a contextual menu with UI Scripting?

Namely in iTunes: choosing “Convert ID3 Tags.” in a track contextual menu (might not be there) and “Export.” in a Playlist contextal menu.

		tell application "System Events"
			tell process "iTunes"
				set theSelectedRow to (row 1 of outline 1 of scroll area 3 of window "iTunes" whose selected is true)
				set {x, y} to position of theSelectedRow
				key down control
				tell me to do shell script "/usr/local/bin/cliclick c:" & x & "," & y
				key up control
				keystroke "Convert" & return
				delay 1
....

implies intalling cliclick and keystroke “Convert” to select the menu item. Is there a better way?

TIA

Hello

Here is a handler which I used to trigger a contextual menu in Numbers.
It was designed to convert the selected standard row 1 into a header one.

It does the trick but there is a long pause before really triggering the menu item.

Maybe it may be used with iTunes.


tell application "Numbers"
	set dname to name of document 1
	tell document 1 to tell sheet 1 to tell table 1
		set selection range to range "A1:A1"
	end tell
end tell

my convertRow1ToHeaderRow(dname)

on convertRow1ToHeaderRow(d_Name)
	local boutonMenu, avant, tempsRequis, errmsg, errNbr, xPos, yPos
	
	tell application "Numbers"
		activate
		tell application "System Events" to tell application process "Numbers"
			tell window d_Name to tell first splitter group to tell last splitter group to tell first splitter group to tell last scroll area to tell (first UI element whose role is "AXLayoutArea") to tell first UI element to tell first UI element to tell last group
				set boutonMenu to first menu button
				(*
Reveal the contextual menu *)
				tell current application to set avant to current date
				click boutonMenu
				tell current application to set tempsRequis to (current date) - avant
				repeat 50 times
					if exists menu 1 of boutonMenu then exit repeat
					delay 0.5
				end repeat
				tell boutonMenu to set {xPos, yPos} to position
			end tell -- window.
			click at {xPos + 26, yPos + 28}
		end tell -- System Events
		display dialog "Waited " & tempsRequis & " seconds" & return & "before giving hand back to the script !" buttons {"Continue"} default button 1
	end tell
end convertRow1ToHeaderRow

Oops, the phone rang and I clicked [ Submit ] erroneously :rolleyes:

Yvan KOENIG (VALLAURIS, France) mardi 9 octobre 2012 18:55:54

I think you forgot the handler Yvan.

:lol:

Thanks, it was helpful to get me started. Unfortunately there is no menu button :frowning:

The progress so far:

			tell window d_Name
				tell scroll area 3
					get it
					tell outline 1
						get it
						tell row 19 
							--click --row 19
							perform action  "AXShowMenu"
							keystroke "Convert" & return

And hop, there it is the wanted dialog!

Can we say perform action “AXShowMenu” is a reasonably general solution for contextual menus?

Hello, I’m really puzzled.


activate application "iTunes"
tell application "System Events" to tell application process "iTunes"
	tell window 1
		--class of every UI element
		-->{button, button, button, button, slider, button, scroll area, radio group, text field, scroll area, UI element, scroll area, scroll area, browser, button, button, button, button, static text, button, button, static text, button, button, button, button}
		position of every scroll area
		--> {{665, 44}, {303, 91}, {1290, 114}, {673, 91}}
		size of every scroll area
		--> {{475, 44}, {185, 827}, {213, 804}, {616, 827}}
		(*
		class of every UI element of scroll area 3
		--> {UI element}
		class of every UI element of first outline of scroll area 3
		--> error number -1719 from outline 1 of scroll area 3 of window 1 of application process "iTunes"
		*)
		class of every UI element of last scroll area
		--> {outline, scroll bar, scroll bar}
		class of every UI element of first outline of last scroll area
		--> {group, column, column, column, column, column, column, column, column, column, column, column, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row, row}
		tell first outline of last scroll area
			tell row 25
				position
				--> {1456, 1014}
				size
				--> {1177, 19}
				value of first text field
				--> "Little Numbers"
			end tell #  row 25
			tell (first row whose value of text field 1 is "1901")
				if not value of attribute "AXSelected" then
					set value of attribute "AXSelected" to true
					repeat until value of attribute "AXSelected" is true
						delay 0.1
					end repeat
				end if
				# Now the wanted row is selected
				set {xTop, yTop} to position
				tell application "ASObjC Runner"
					{xTop + 1, yTop + 1}
					click button once at result holding down {control}
					{xTop + 5, yTop + 2}
					set mouse location to result
					# The contextual menu is visible
				end tell
				delay 0.5
				class of UI elements
			end tell # (first row whose value of text field 1 is
		end tell # first outline of last scroll area
		class of UI elements
		# I am really puzzled, I don't see the menu object.
		--> {button, button, button, button, slider, button, scroll area, radio group, text field, scroll area, UI element, scroll area, scroll area, browser, button, button, button, button, static text, button, button, static text, button, button, button, button}
	end tell # window 1
end tell # System Events & process

(1) Here, the tracks aren’t listed in scroll area 3 but in scroll area 4.
(2) the script reveal the pop up menu but I’m unable to find its descriptor.

Yvan KOENIG (VALLAURIS, France) mercredi 10 octobre 2012 19:49:02

The iTunes sidebar is showing.
Under most circumstances last scroll area works, but not if the sidebar is opened after the playlist is showing. Thanks for poniting me to a future bug!

I guess that explains why I was having so much problems (besides my complete noobiness).

In my script above (soon edited) click is not needed and the script leaves the selection as is, a good thing. Thanks for showing a few things about the process.