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

Selecting tracks in itunes?

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


selectt

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}


selectt

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


selectt

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)

Posted on Apr 21, 2012 6:07 PM

Reply
20 replies

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

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

Apr 22, 2012 4:22 AM in response to twtwtw

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?

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

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

revealcurrent 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

commandkey 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

Apr 22, 2012 8:54 AM in response to Frank Caggiano

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!

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.

Apr 22, 2012 9:18 AM in response to Pierre L.

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!

Apr 22, 2012 9:34 AM in response to twtwtw

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!

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

revealthePlaylist

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

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

Apr 22, 2012 10:52 AM in response to Pierre L.

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

Apr 22, 2012 11:02 AM in response to Frank Caggiano

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!

Apr 23, 2012 2:35 PM in response to Anic264b

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?



The following new version of the script might be somewhat faster:


set theTrackNumbers to {3, 5} -- the numbers must be in increasing order


tell application "iTunes"

activate

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

set theNumber to number of tracks in thePlaylist

revealthePlaylist

end tell


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

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

set {w0, h0} to size of scroll area 3 of window "iTunes"

set {w1, h1} to size of row 1 of outline 1 of scroll area 3 of window "iTunes"

set {x1, y1} to position of row 1 of outline 1 of scroll area 3 of window "iTunes"

set x to x1 + 5

set focused of outline 1 of scroll area 3 of window "iTunes" to true

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

set N0 to (h0 - 23) div h1

set theRemainder to theNumber mod N0

set imax to (theNumber - theRemainder) div N0

if theRemainder = 0 then

set imax to imax - 1

set theRemainder to N0

end if

set i to 0

set j to 0

set N to N0

repeat with k in theTrackNumbers

if k > theNumber then return -- optional, just for caution

repeat while k > N

key code 121 -- scroll down

set i to i + 1

if i < imax then

set j to j + N0

set N to N + N0

else

set j to j + theRemainder - 1

set N to theNumber

end if

end repeat

command key down

tell me to do shell script "/usr/local/bin/cliclick " & x & space & (y1 + (k - j - 1) * h1)

command key up

end repeat

end tell



Message was edited by: Pierre L. (one line removed)

Selecting tracks in itunes?

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