1 2 Previous Next 17 Replies Latest reply: Apr 13, 2013 8:41 AM by iamsudo
petlan1 Level 1 Level 1 (0 points)

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, Mac OS X (10.5.8)
  • 1. Re: Cannot access context menu item in iTunes using AppleScript
    Camelot Level 8 Level 8 (45,790 points)

    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.

  • 2. Re: Cannot access context menu item in iTunes using AppleScript
    petlan1 Level 1 Level 1 (0 points)

    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.

  • 3. Re: Cannot access context menu item in iTunes using AppleScript
    Pierre L. Level 4 Level 4 (3,860 points)

    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

                   keystroke return

              end tell

              delay 1

              click button 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.

  • 4. Re: Cannot access context menu item in iTunes using AppleScript
    Camelot Level 8 Level 8 (45,790 points)

    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 file of type "JPG")

    set myPic to read myImgFile as JPEG 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.

  • 5. Re: Cannot access context menu item in iTunes using AppleScript
    Pierre L. Level 4 Level 4 (3,860 points)

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

     

    A better (though not faster) solution, which works even when more than one track is selected, is obtained by replacing

        repeat 8 times

            key code 125 -- down arrow

        end repeat

    with

        keystroke "Get Album Artwork"

  • 6. Re: Cannot access context menu item in iTunes using AppleScript
    petlan1 Level 1 Level 1 (0 points)

    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"

  • 7. Re: Cannot access context menu item in iTunes using AppleScript
    Pierre L. Level 4 Level 4 (3,860 points)

    Is there any way to boost performance  of the script ?

    Did you try lessening the number of tracks in the iTunes window by entering some criteria in the search field?

  • 8. Re: Cannot access context menu item in iTunes using AppleScript
    Pierre L. Level 4 Level 4 (3,860 points)

    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

        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)

                                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


     

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

  • 9. Re: Cannot access context menu item in iTunes using AppleScript
    petlan1 Level 1 Level 1 (0 points)

    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

  • 10. Re: Cannot access context menu item in iTunes using AppleScript
    Pierre L. Level 4 Level 4 (3,860 points)

    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.

  • 11. Re: Cannot access context menu item in iTunes using AppleScript
    iamsudo Level 1 Level 1 (0 points)

    Could you update the script for iTunes 11?

    I'm having a hard time with UI Browser to access the context menu

  • 12. Re: Cannot access context menu item in iTunes using AppleScript
    Pierre L. Level 4 Level 4 (3,860 points)

    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

        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 splitter group 1

                        tell splitter group 1

                            tell scroll area 1

                                tell outline 1

                                    tell (row 1 whose selected is true)

                                        perform action "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

            keystroke tab -- select the search field

            key code 51 --

        end tell

        reveal theTrack

    end tell


  • 13. Re: Cannot access context menu item in iTunes using AppleScript
    iamsudo Level 1 Level 1 (0 points)

    Thanks, Pierre L.

     

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

     

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

  • 14. Re: Cannot access context menu item in iTunes using AppleScript
    Pierre L. Level 4 Level 4 (3,860 points)

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

    I really don't know. Actually, any command that follows the perform action "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)

1 2 Previous Next