Skip navigation

Selecting tracks in itunes?

2043 Views 20 Replies Latest reply: Apr 24, 2012 1:39 PM by Pierre L. RSS
1 2 Previous Next
Anic264b Level 1 Level 1 (10 points)
Currently Being Moderated
Apr 21, 2012 6:08 PM

Greetings!

 

Selecting tracks in iTunes, using AppleScript, remains a mystery to me. For example, this short script:

tell application "iTunes"

  set t to first track of current playlist

  select t

end tell

I think the script is obvious, but I get this error instead:

iTunes got an error: URL track id 896882 of user playlist id 896879 of source id 76 doesn’t understand the select message.

(well, if the select command is not made for selecting tracks, what else in iTunes?)

 

Thinking it may be because iTunes expect a list, I then try this:

tell application "iTunes"

  set t to {first track of current playlist}

  select t

end tell

But the same error happens.

 

Finally, out of idea, I try this:

tell application "iTunes"

  set t to {first track of current playlist}

  set selection to t

end tell

I get this, now:

iTunes got an error: Unknown object type.

(in the “selection” line).

 

I've also tried things like this:

tell application "iTunes"

  set t to selection

  select t

end tell

To be sure the data is the expected type, but the same error happens.

 

Now, I'm really out of idea. I have over 8'000 files to sort, and I made a script to get every duplicate track, but now I can't select them (I can only uncheck the checkbox, for visual feedback, but it's far from ideal).

 

I can't think it's not possible to select some tracks in iTunes. What else might I try?

 

TIA

Mac Pro, Mac OS X (10.7.2)
  • Frank Caggiano Level 7 Level 7 (22,820 points)
    Currently Being Moderated
    Apr 21, 2012 8:46 PM (in response to Anic264b)

    What do you want to do with the track?

     

    tell application "iTunes"

              get name of track 3

    end tell

    or

     

    tell application "iTunes"

      play track 3

    end tell



    work fine

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Apr 21, 2012 10:39 PM (in response to Anic264b)

    to select a track in iTunes, you want to do something like the following:

     

    tell application "iTunes"

              tell browser window 1

                        reveal track 3 of user playlist "playlist" of application "iTunes"

              end tell

    end tell

     

    remember, the selection is a property of a browser or playlist window (not of a track), and you have to specify the playlist to display in the window (current playlist will work for the currently visible one).

  • Frank Caggiano Level 7 Level 7 (22,820 points)
    Currently Being Moderated
    Apr 22, 2012 5:49 AM (in response to Anic264b)

    It's not possible to reveal multiple tracks in iTunes via Applescript. You can get all the track selected in the GUI but you can't set them.

     

    If you really need to do this you could resort to GUI scripting iTunes but it seems to me you'd be better collecting all the tracks you want and putting them in a playlist. Then, I think, you could display the playlist.

     

    Remember you don't need to reveal the tracks in the iTunes window in order to act on them in Applescript. So again if you described what it is you are tying to do (get all tracks by a particular artist for example and play them) a better solution might be found.

     

    regards

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Apr 22, 2012 7:29 AM (in response to Anic264b)

    The following script uses GUI Scripting and Cliclick to select multiple tracks in the current playlist. Not very elegant, but it seems to do what you are asking for.

     

    set theTrackNumbers to {3, 5} -- for example

    tell application "iTunes"

        activate

        reveal current playlist

        tell application "System Events" to tell process "iTunes"

            click menu item "Select All" of menu 1 of menu bar item "Edit" of menu bar 1

            click menu item "Select None" of menu 1 of menu bar item "Edit" of menu bar 1

            command key down -- to select multiple tracks

            repeat with k in theTrackNumbers

                try

                    set {x, y} to position of row k of outline 1 of scroll area 3 of window "iTunes"

                    do shell script "/usr/local/bin/cliclick " & (x + 5) & space & y

                end try

            end repeat

            command key up

        end tell

    end tell


  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Apr 22, 2012 9:14 AM (in response to Anic264b)

    With respect to "why", why would you need to select more than one track?  Most of the things that you need to do with multiple tracks have separate applescript commands that don't require user interaction.  for instance, if you have a list of tracks and want to delete them, you would normally use the delete command and do so directly rather than select them in a browser window and delete them manually.

     

    Seriously, we need a better idea of what you're trying to do.

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Apr 22, 2012 10:33 AM (in response to Anic264b)

    Almost! The only remaining problem is because I have many tracks, I can only select visible ones (clicks whose y is greater than the view are just ignored. Fortunately, they aren't sent to the outside view!).

     

    Maybe you might want to try an improved version of the previous script:

     

    set theTrackNumbers to {3, 5} -- for example

    tell application "iTunes"

        activate

        set thePlaylist to current playlist -- or to any other playlist

        reveal thePlaylist

        set N to number of tracks in thePlaylist

        tell application "System Events" to tell process "iTunes"

            click menu item "Select All" of menu 1 of menu bar item "Edit" of menu bar 1

            click menu item "Select None" of menu 1 of menu bar item "Edit" of menu bar 1

            command key down -- to select multiple tracks

            repeat with k in theTrackNumbers

                try

                    if k < N / 2 then

                        set theValue to (k - 1) / N

                    else

                        set theValue to k / N

                    end if

                    set value of scroll bar 1 of scroll area 3 of window "iTunes" to theValue

                    set {x, y} to position of row k of outline 1 of scroll area 3 of window "iTunes"

                    do shell script "/usr/local/bin/cliclick " & (x + 5) & space & y

                end try

            end repeat

            command key up

        end tell

    end tell

     

    Message was edited by: Pierre L.

  • Frank Caggiano Level 7 Level 7 (22,820 points)
    Currently Being Moderated
    Apr 22, 2012 10:53 AM (in response to Anic264b)

    IIt's still not clear what it is you are trying to do. Is this close:

     

    You want to select all 8000 tracks sort them by, say name, and then go through them looking for duplicates and  then out of all the tracks that have the same item you sorted on, select one and delete the rest?

1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.