Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

Cannot access context menu item in iTunes using AppleScript

I have written an AppleScript to import tracks in itunes and write iD3 information for that track. The problem is that I need to add album artwork as well and since it does not exist any api for that in the Dictionary I have to select the song in iTunes right-click and select "Get Album Artwork" in the context menu (I cannot use Advanced - Get Album Artwork since it gets art for all my many thousand tracks). Although I use the Accessibility Inspector I cannot access the menu item "Get Album Artwork". The error message given is 'Can't get menu 1 of window "iTunes" of process "iTunes"'.


This is my best shot. Before I execute the script I open iTunes and select a track manually.

tell application "iTunes" to activate


tell application "System Events"

tell process "iTunes"

tell window "iTunes"

tell menu 1

tell menu item "Get Album Artwork"

perform action "AXPress"

end tell

end tell

end tell

end tell

end tell


This is the information I got from Accessibility Inspector.

<AXApplication: “iTunes”>

<AXWindow: “iTunes”>

<AXMenu>

<AXMenuItem: “Get Album Artwork”>


Attributes:

AXRole: “AXMenuItem”

AXRoleDescription: “menu item”

AXParent: “<AXMenu>”

AXEnabled: “true”

AXPosition: “x=427 y=518”

AXSize: “w=179 h=18”

AXTitle: “Get Album Artwork”

AXHelp: “(null)”

AXSelected (W): “true”

AXMenuItemCmdChar: “(null)”

AXMenuItemCmdVirtualKey: “(null)”

AXMenuItemCmdGlyph: “(null)”

AXMenuItemCmdModifiers: “0”

AXMenuItemMarkChar: “

AXMenuItemPrimaryUIElement: “(null)”


Actions:

AXCancel - cancel

AXPress - press

iTunes 10.4-OTHER, Mac OS X (10.5.8)

Posted on Aug 22, 2011 10:36 AM

Reply
17 replies

Aug 22, 2011 11:01 AM in response to petlan1

I'm not sure I understand:


does not exist any api for that in the Dictionary


Sure you can.


tell application "iTunes"

set sel to (get selection)

set theArt to artworks of item 1 of sel

end tell


This avoids the whole UI scripting mess, no?


Maybe the issue is that tracks can have multiple artworks, therefore what you get back is a list of artworks, which you may have to iterate through, even though there's only 1 artwork for the given track.

Aug 22, 2011 11:22 AM in response to Camelot

The problem is not to set the artwork using the API dictionary, I know that is possible. It is download artwork using iTunes that is not possible.


Problem is that I want to query the artwork in iTunes to set the high quality artwork from Apple for the select (I know there is some applescript out there that fetches artwork from Google and Amazon but I am not pleased with their result).


I want to write a applescript that works like the meny alternative in iTunes when you select a track and right-click othe track and select "Get Album Artwork" from the context menu and art is set for that track.

Aug 22, 2011 4:51 PM in response to petlan1

I want to write a applescript that works like the meny alternative in iTunes when you select a track and right-click othe track and select "Get Album Artwork" from the context menu and art is set for that track.


Here's the best I was able to do:


tell application "iTunes" to activate

tell application "System Events"

keystroke "3" using {option down, command down} -- view as list

tell process "iTunes"

tell window "iTunes"

set theRow to row 1 of outline 1 of scroll area 3 whose selected is true

tell theRow to perform action "AXShowMenu"

repeat 8 times

key code 125 -- down arrow (no reaction for several seconds)

end repeat

keystrokereturn

end tell

delay 1

clickbutton 1 of window 1 -- button 1 to cancel, button 2 to get artwork

end tell

end tell


(In its present state, the script won't work properly if more than one track is selected.)


Hope it can help.

Message was edited by: Pierre L.

Aug 22, 2011 5:25 PM in response to petlan1

I guess I'm still not seeing the issue.


Problem is that I want to query the artwork in iTunes to set the high quality artwork from Apple for the select


OK, I get that, but I've already shown you that it's possible to get the artwork via AppleScript - setting it a little more complex, but still doable.


What's missing, of course, is where you're definging what the artwork should be set to. I assume you have some handle on that. If that's the case then you can set the artwork of any given track via something like:


set myImgFile to (choose fileof type "JPG")

set myPic to readmyImgFileasJPEG picture


tell application "iTunes"

set sel to (get selection)

set curTrack to item 1 of sel

set data of front artwork of curTrack to myPic

end tell


This example prompts you for a JPEG image on disk and then sets it as the artwork for the selected track. Still no UI scripting required. 🙂

Aug 23, 2011 10:53 AM in response to Pierre L.

Camelot: I do'nt have a jpeg I want iTunes to get it for me


Thank you very much Pierre L for your contribution. The script works. Sorry to say it is very slow it takes about 2 minutes 15 seconds until the contex menu appears. These rows seems to be the the performance bottlenecks. Is there any way to boost performance of the script ?


set theRow to row 1 of outline 1 of scroll area 3 whose selected is true

tell theRow to perform action "AXShowMenu"

Aug 24, 2011 9:02 AM in response to petlan1

Is there any way to boost performance of the script ?


Maybe you might want to try the following version of my previous script. It assumes you have selected “as List” from the View menu of the menu bar and also “Song” (ideally) or “All” (minimally) from the context menu of the iTunes window Search field.


tell application "iTunes"

activate

if selection is {} then return

set theTrack to item 1 of selection

revealtheTrack

tell application "System Events"

keystroketab-- select the search field

tell process "iTunes"

tell window "iTunes"

set value of text field 1 to name of theTrack

end tell

end tell

end tell

revealtheTrack

tell application "System Events"

tell process "iTunes"

tell window "iTunes"

tell scroll area 3

tell outline 1

tell (row 1 whose selected is true)

perform action "AXShowMenu"

end tell

end tell

end tell

end tell

end tell

keystroke "Get Album Artwork" & return -- no reaction for several seconds

delay 1

tell process "iTunes"

tell window 1

clickbutton 1 -- button 1 to cancel, button 2 to get artwork

end tell

end tell

keystroketab-- select the search field

key code 51 --

end tell

revealtheTrack

end tell


Message was edited by: Pierre L. (a lot of small corrections)

Aug 24, 2011 12:02 PM in response to Pierre L.

Now it works much better. Problem was solved after I performed a search so I only Selected on song.


tell application "iTunes" to activate


tell application "System Events"

tell process "iTunes"

tell window "iTunes"

tell text field 1

keystroke "Portishead Cowboys"

end tell

end tell

end tell

end tell


tell application "iTunes"

activate

if selection is {} then return

set theTrack to item 1 of selection

reveal theTrack

tell application "System Events"

keystroke tab -- select the search field

tell process "iTunes"

tell window "iTunes"

set value of text field 1 to name of theTrack

end tell

end tell

end tell

reveal theTrack

tell application "System Events"

tell process "iTunes"

tell window "iTunes"

tell scroll area 3

tell outline 1

--tell (row 1 whose selected is true)

tell row 1

perform action "AXShowMenu"

end tell

end tell

end tell

end tell

end tell

keystroke "Get Album Artwork" & return -- no reaction for several seconds

delay 1

tell process "iTunes"

tell window 1

click button 1 -- button 1 to cancel, button 2 to get artwork

end tell

end tell

keystroke tab -- select the search field

key code 51 -- ⌫

end tell

reveal theTrack

end tell


Thank you so very much Pierre L for the solution

Aug 24, 2011 3:04 PM in response to petlan1

I'm glad the script ended up being useful for you. On the other hand, it seems to me that the first 10 lines of your script aren't necessary and just duplicate what the first part of mine does. You could manually select any number of tracks before launching the script, since the script will reveal and search just the first one of them.

Apr 11, 2013 5:20 PM in response to iamsudo

The following version should work with iTunes 11, although the script is still overly slow.


tell application "iTunes"

activate

if selection is {} then return

set theTrack to item 1 of selection

revealtheTrack

tell application "System Events"

keystroketab-- select the search field

tell process "iTunes"

tell window "iTunes"

set value of text field 1 to name of theTrack

end tell

end tell

end tell

revealtheTrack

tell application "System Events"

tell process "iTunes"

tell window "iTunes"

tell splitter group 1

tell splitter group 1

tell scroll area 1

tell outline 1

tell (row 1 whose selected is true)

performaction "AXShowMenu"

end tell

end tell

end tell

end tell

end tell

end tell

end tell

keystroke "Get Album Artwork" & return-- no reaction for several seconds

tell process "iTunes"

repeat until exists (window 1 whose subrole is "AXDialog")

end repeat

click button 2 of (window 1 whose subrole is "AXDialog")

-- button 1 to cancel, button 2 to get album artwork

end tell

keystroketab-- select the search field

key code 51 --

end tell

revealtheTrack

end tell

Apr 13, 2013 2:18 AM in response to iamsudo

Why is selecting a menu item of context menu so slow?

I really don't know. Actually, any command that follows the performaction "AXShowMenu" line of code seems to be delayed in time.


You might try this for instance:


tell (row 1 whose selected is true)

perform action "AXShowMenu"

return "That's really slow"

end tell

Thinking of workarounds, have you tried clicking by calculating coordinates for a menu item from those of the context menu? Would that be quicker?

I don't remember if I've tried that. I know I've tried a lot of things without success. Initially though, I just wanted to show the OP that it was possible to access the menu item "Get Album Artwork". And frankly, since then I've lost interest in that script.


Message was edited by: Pierre L. (added an example)

Apr 13, 2013 7:56 AM in response to Pierre L.

I tried revealing a track and clicking with MouseTools and it's very fast. Only problem is I've done this with fix coordinates and I'd like to share my script with others regardless of display and app configuration.


I can get AXPosition for row 1 > text field 1, but I cannot get any reference for the context menu and its child elements — Accessibility Inspector reads window 1 > menu 1, but ASE always returns an invalid index. Any ideas?

Cannot access context menu item in iTunes using AppleScript

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