Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

applescript - cannot reference iTunes context menu

I cannot get any reference for the context menu and its child elements.


Accessibility Inspector reads


AXApplication

AXWindow:AXStandardWindow

AXMenu


but ASE always returns an invalid index. Also tried UI Browser to no avail.


Any ideas?

MacBook Air, OS X Mountain Lion (10.8.3)

Posted on Apr 13, 2013 8:39 AM

Reply
Question marked as Best reply

Posted on Apr 13, 2013 11:47 AM

iamsudo wrote:


I cannot get any reference for the context menu and its child elements.


Nor do I. However, with at least one track selected in the iTunes window, it is not too difficult, using GUI Scripting in conjunction with cliclick, to open the contextual menu and select a menu item. For example:


tell application "iTunes" to activate


tell application "System Events"

tell process "iTunes"

tell window "iTunes"

tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1

tell (row 1 whose selected is true)

set {x, y} to position

end tell

end tell

end tell

end tell

end tell


do shell script "/usr/local/bin/cliclick kd:ctrl c:" & (x + 5) & "," & (y + 5) & " ku:ctrl"


tell application "System Events" to keystroke "Get Album Artwork" & return


Tested under OS X 10.8.3 with iTunes 11.0.2 and cliclick 2.2. (When using GUI Scripting, you must enable access for assistive devices in the Accessibility System Preference pane.)

18 replies
Question marked as Best reply

Apr 13, 2013 11:47 AM in response to iamsudo

iamsudo wrote:


I cannot get any reference for the context menu and its child elements.


Nor do I. However, with at least one track selected in the iTunes window, it is not too difficult, using GUI Scripting in conjunction with cliclick, to open the contextual menu and select a menu item. For example:


tell application "iTunes" to activate


tell application "System Events"

tell process "iTunes"

tell window "iTunes"

tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1

tell (row 1 whose selected is true)

set {x, y} to position

end tell

end tell

end tell

end tell

end tell


do shell script "/usr/local/bin/cliclick kd:ctrl c:" & (x + 5) & "," & (y + 5) & " ku:ctrl"


tell application "System Events" to keystroke "Get Album Artwork" & return


Tested under OS X 10.8.3 with iTunes 11.0.2 and cliclick 2.2. (When using GUI Scripting, you must enable access for assistive devices in the Accessibility System Preference pane.)

Apr 14, 2013 10:03 AM in response to Pierre L.

Thanks, Pierre L.


What I'm trying to achieve is add a track to up next via its HFS path.


This script works with fixed coordinates and runs in about .3-.5 sec.


set TheTrack to "HD:Users:me:Music:iTunes:iTunes Music:Your Music:test.m4a" -- test track

tell application "iTunes"

set SelTrack to (addTheTrack)


revealSelTrack


activate


-- fixed coordinates for iTunes fullscreen 1440.900

do shell script "MouseTools -x 380 -y 180 -controlKey -leftClick" -- context menu

tell application "System Events" to keystroke "Add to Up Next" & return

end tell

This other script also works but runs in 3-4 sec.


setTheTrackto "HD:Users:me:Music:iTunes:iTunes Music:Your Music:test.m4a" -- test track

tell application "iTunes"

set SelTrack to (addTheTrack)


revealSelTrack


activate

tell application "System Events"

tell process "iTunes" to tell text field 1 of (row 1 whose selected is true) of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 to set {x, y} to position

do shell script "MouseTools -x " & (x + 5) & " -y " & (y + 5) & " -controlKey -leftClick" -- context menu

keystroke "Add to Up Next" & return

end tell

end tell


But, if I set the track reference it runs in about .3-.5 sec.


set SelTrack to file track id 15626 of user playlist id 15622 of source id 75 of application "iTunes"



Getting the file track id is somehow slowing getting coordinates to a crawl and vice versa. Any ideas?

Apr 17, 2013 7:44 AM in response to Pierre L.

The following script runs much better although still relies on MouseTools —

perform action "AXShowMenu" would still slow it down.


set TheTrack to "HD:Users:me:Music:iTunes:iTunes Music:Your Music:test.m4a" -- test track

tell application "iTunes"

set SelTrack to (addTheTrack)

reveal playlist "Purchased"

activate

tell application "System Events" to tell process "iTunes" to tell text field 1 of (first row whose value is not null) of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 to set {x, y} to position

revealSelTrack

do shell script "MouseTools -x " & (x + 5) & " -y " & (y + 5) & " -controlKey -leftClick" -- context menu

tell application "System Events" to keystroke "Add to Up Next" & return

end tell


Would you mind testing it to have a second opinion?

Apr 24, 2013 11:13 AM in response to iamsudo

Here's another variation, which doesn't need to “reveal” the track or the playlist:



set theFile to POSIX file "/Users/pierre/Music/iTunes/iTunes Media/Music/Compilations/Gold - Greatest Hits/11 Chiquitita.m4a" -- just an example (ABBA Gold - Chiquitita)


tell application "iTunes"

addtheFile

settheIndextoindexofresult

activate

end tell


tell application "System Events"

tell process "iTunes"

tell window "iTunes"

tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1

set {x, y} topositionofrowtheIndex

end tell

end tell

end tell

end tell


do shell script "/usr/local/bin/cliclick kd:ctrl c:" & (x + 5) & "," & (y + 5) & " ku:ctrl"


tell application "System Events" to keystroke "Add to Up Next" & return

Apr 25, 2013 2:43 AM in response to Pierre L.

Try with another song, maybe…😁


I tried running


osascript -e "tell application \"System Events\" to tell process \"iTunes\" to tell (first row whose selected is true) of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 to perform action \"AXShowMenu\"" > /dev/null 2>&1 &


to see if I could rather use AXShowMenu, but it's nonetheless still very slow.


For now I'll stick with my version of the script. Btw, did you try it?

Apr 25, 2013 5:29 AM in response to iamsudo

For now I'll stick with my version of the script. Btw, did you try it?


I tried, but was unable to run it, no more than any of your other versions. As I said in my second post (in this thread), there seems to be an issue with the “reveal” command. Actually, the issue is most likely due to my setup of iTunes. The “revealSelTrack” line of code reveals the track for just a brief fraction of a second, so that the next line of code cannot be executed. I've finally found out why:


set TheTrack to "Macintosh HD:Users:pierre:Music:iTunes:iTunes Media:Music:Compilations:Gold - Greatest Hits:11 Chiquitita.m4a"

tell application "iTunes"

addTheTrack --> file trackid 2599 of library playlistid 1801 of sourceid 74 of application "iTunes"

end tell

Actually, library playlistid 1801 of sourceid 74 is the same aslibrary playlist "Library" ofsource "Library", whose visible property is false.



Of course, I could use the following workaround:


set TheTrack to "Macintosh HD:Users:pierre:Music:iTunes:iTunes Media:Music:Compilations:Gold - Greatest Hits:11 Chiquitita.m4a"

tell application "iTunes"

addTheTrack

set theDatabaseId to database ID of result

set TheTrack to file track 1 of user playlist "Music" of source "Library" whose database ID is theDatabaseId

revealTheTrack

end tell


But that's not going to make things faster.

Apr 25, 2013 6:11 AM in response to Pierre L.

I use iTunes 11.0.2 in OS X 10.8.3 — visibleoflibrary playlist "Library" ofsource "Library" is set to false.

Despite this setting revealSelTrack effectively reveals the track via its file track id or HFS path allowing the next lines of code to execute.

Do you first revealplaylist "Purchased"?


My last version runs in about .3 sec. Bear in mind that I use TotalSpaces with no transitions to achieve this speed — with Mission Control alone it'd probably need introducing delays, I think.

Apr 25, 2013 7:45 AM in response to iamsudo

Does this script work for you?


No, it doesn't. No error, but the script again reveals the track for just a brief fraction of a second. The reason is that library playlist 1 of source 1 is the same as library playlist "Library" ofsource "Library", and has nothing to do with the “Purchased” playlist.


Could it be that your files are not located in the standard folder, that is ~/Music/iTunes/iTunes Media/Music?


Let's compare:

Your example file: "HD:Users:me:Music:iTunes:iTunes Music:Your Music:test.m4a"

My example file: "Macintosh HD:Users:pierre:Music:iTunes:iTunes Media:Music:Compilations:Gold - Greatest Hits:11 Chiquitita.m4a"


By the way, I've not the slightest idea of what you are talking about when you say “Bear in mind that I use TotalSpaces with no transitions to achieve this speed — with Mission Control alone it'd probably need introducing delays, I think”. 😕

Apr 25, 2013 8:39 AM in response to Pierre L.

Pierre L. wrote:


The reason is that library playlist 1 of source 1 is the same as library playlist "Library" ofsource "Library", and has nothing to do with the “Purchased” playlist.


I get to reveal the Library playlist if I first reveal the Purchased playlist *or* if I run reveal first track of library playlist 1 of source 1 twice in a row.

Could it be that your files are not located in the standard folder, that is ~/Music/iTunes/iTunes Media/Music?


I still use the old ~/Music/iTunes/iTunes Music folder organization — I doubt that that has something to do with your problem.

By the way, I've not the slightest idea of what you are talking about when you say “Bear in mind that I use TotalSpaces with no transitions to achieve this speed — with Mission Control alone it'd probably need introducing delays, I think”. 😕


TotalSpaces is a spaces manager app that among other features has immediate space switching. With Mission Control one would introduce delays for my script to work.

May 10, 2013 8:12 AM in response to iamsudo

iamsudo wrote:


Just one last question, is there any way to access the "Add To…" context menu (the one with the > in a circle) with GUI scripting?


User uploaded file


In the situation depicted by the above screenshot, you can either control click on the song title "Chiquitita" or do a normal click on the button to the right of the title.



User uploaded file User uploaded file



It is rather easy to access the first menu above with GUI scripting:


tell application "iTunes" to activate


tell application "System Events"

tell process "iTunes"

tell window "iTunes"

set {x, y} to position

set {h, v} to size

set {x, y} to {x + h div 2, y + 20}

end tell

end tell

end tell


do shell script "/usr/local/bin/cliclick kd:ctrl c:" & x & "," & y & " ku:ctrl"



On the other hand, the only way I could think of to access the second menu above is to first switch to the MiniPlayer window. The following script seems to work flawlessly on my MacBook Pro under OS X 10.8.3.


tell application "iTunes" to activate


tell application "System Events"

keystroke "m" using {option down, command down} -- switch to MiniPlayer

tell process "iTunes"

repeat until exists window "MiniPlayer"

end repeat

tell window "MiniPlayer"

set {x, y} to position

set {h, v} to size

set size to {400, h}

set {x, y} to {x + 77, y + v div 2}

end tell

end tell

end tell


do shell script "/usr/local/bin/cliclick c:" & x & "," & y



Hope it can help.


Message was edited by: Pierre L.

applescript - cannot reference iTunes context menu

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.