Previous 1 2 Next 20 Replies Latest reply: Apr 24, 2012 1:39 PM by Pierre L.
Anic264b Level 1 Level 1 (20 points)

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 (25,715 points)

    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,900 points)

    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).

  • Anic264b Level 1 Level 1 (20 points)

    I just want to select them so I can do whatever I want later (like drag&drop or, well, anything).

    Thanks

  • Anic264b Level 1 Level 1 (20 points)

    Unexpected, but it indeed selects a track! Thank you! But what's the meaning of the “select” command then?

     

    And, I found this to be working also:

    tell application "iTunes"

              tell browser window 1

        reveal track 3 of current playlist of application "iTunes"

              end tell

    end tell

    (as long as there is a current playlist (so I play before I run the script)). This has the advantage of me not hard coding a playlist's name in the script.

     

    However, this doesn't work:

    tell application "iTunes"

              tell browser window 1

        reveal {track 3 of current playlist of application "iTunes", track 5 of current playlist of application "iTunes"}

              end tell

    end tell

    So, for selecting more than one track, I'm again stuck. I see the “reveal” command like Command-L (reveal currently playing track). This is also the behaviour of the Finder (one can't reveal several items because the might be in 2 different folders). However, selecting one track a time doesn't help me much.

     

    Could it be iTunes deny selecting more than one file because they may be on different playlists, like the Finder?

  • Frank Caggiano Level 7 Level 7 (25,715 points)

    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,950 points)

    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


  • Anic264b Level 1 Level 1 (20 points)

    Frank Caggiano wrote:

     

    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.

    Thanks. My question is actually “Why?”. Did Apple forget to add a so common command? Security concerns? I don't see an obvious reason.

    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.

    I've considered this, but there are times where it's complex to deal with one full list and a half list.

    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.

    A specific action isn't the problem. There are many times where I just want the tracks I want to be selected so I can act on the selection. It's just that my workflow is based on selection (like in the Finder).

     

    Thank you for your useful answer!

  • twtwtw Level 5 Level 5 (4,900 points)

    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.

  • Anic264b Level 1 Level 1 (20 points)

    Pierre L. wrote:

     

    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.

    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!).

     

    I'm feeling like with Preview and it's unscriptability here. Something Apple has strangely forgot to support and workarounds are what they can (in addition than extra efforts).

     

    Thanks!

  • Anic264b Level 1 Level 1 (20 points)

    twtwtw wrote:

     

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

    I know you're right (it's just sometimes hard to notice things we do in a workflow and explain them).

    Well, I have over 8000 tracks that I have to sort (those are files from a friend, whose hard disk was about to crash, and permitted me to use its files that I could rescue). There are many duplicates and some are of better format/quality than others (some are also protected, and some are just empty). I've made a script to consider the first occurrence of duplicate files and single files, so I could move them to the trash and move on with the remaining. I can't just make a new playlist, using AppleScript, and put them there because 8000 files are very complex to deal with. It might be one file has not the same duration (e.g. 2 seconds longer) but I want to consider it as a duplicate anyway and handle it with the others, so putting them in a separate playlist would be a mess (I would then have to come back and forth between the two lists and remove/add some tracks from one to the other). That's why selecting them would be great: I could then see the result of my script, keeping them in a single-and-whole list, and start selecting/deselecting them as I see one unexpected selection.

     

    If you think I'm considering this all wrong, just tell me; I have the same feeling!

    Thanks!

  • Pierre L. Level 4 Level 4 (3,950 points)

    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.

  • Anic264b Level 1 Level 1 (20 points)

    Wonderful, thanks! Just a last question, and I'll mark this thread as “solved”. The following line is very slow (takes around 8 seconds to complete):

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

    Any idea?

     

    P.S.: sorry for bothering you with my questions, but AppleScript is a special/odd beast to me (I usually try on my own, so I don't disturb others).

  • Frank Caggiano Level 7 Level 7 (25,715 points)

    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?

  • Anic264b Level 1 Level 1 (20 points)

    Frank Caggiano wrote:

     

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

    Yes, it's close, but not exactly the same.

    You want to select all 8000 tracks

    Sort of. They are already all in the same playlist.

    sort them by, say name, and then go through them looking for duplicates

    Yes, just sorting by artist.

    and then out of all the tracks that have the same item you sorted on, select one and delete the rest?

    This is actually a bit of the opposite. Well, my english being limited, perhaps I'll explain otherwise; say I have these songs:

    1.mp3

    1.mp3

    1.mp3

    2.mp3

    2.mp3

    I'd select row 2,3 and 5. Then, once my script finishes, I'd examine the selected rows to see whether they are correctly selected (e.g. row 4 above might be the same as row 1, with a wrong name, or row 1 might be different than row 2, because of, say, the Finder Label (I can't code for every criterion, better to see myself in a second step)).

    Then, you're right, I'd move the selected items to the trash (I've already made the script for that).

    Thanks!

Previous 1 2 Next