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

trying a script in iTunes

Hey all!

I'm trying to make a script to make iTunes:
1. find all the songs in my library containing a user-defined word in its name.
2. play ALL these songs.
3. stop.

What i've managed until now...
1. Find these songs
2. If the result of the search is only 1 song, play it and then stop.

However, if the result of the search contains more than one song, I'm still not able to find the way to play ALL of them. What i get is ONLY THE LAST ONE of them played, then stop.

The question: How can i write the command to play EVERY song of the list?

Thanks for any help,
(more questions to follow...)

J.

MacBook Pro 15", Mac OS X (10.5.8)

Posted on Feb 28, 2011 6:38 AM

Reply
Question marked as Best reply

Posted on Feb 28, 2011 7:51 AM

This is surprisingly not-easy to do due to the fact you cannot pass iTunes a list of tracks to play.

Off hand, I think your options are to either:

1) build a new playlist consisting of your found tracks, then play the playlist

2) run a repeat loop that starts the first track and watches the player state to detect when that track finishes at which point you play the next track and watch the state again, wait for the track to end, then start the next track, etc. It's not trivial and you won't have any nice fade between tracks (in fact you may end up with slight pauses).

Do either of these sound appealing?
7 replies
Question marked as Best reply

Feb 28, 2011 7:51 AM in response to Jota Be

This is surprisingly not-easy to do due to the fact you cannot pass iTunes a list of tracks to play.

Off hand, I think your options are to either:

1) build a new playlist consisting of your found tracks, then play the playlist

2) run a repeat loop that starts the first track and watches the player state to detect when that track finishes at which point you play the next track and watch the state again, wait for the track to end, then start the next track, etc. It's not trivial and you won't have any nice fade between tracks (in fact you may end up with slight pauses).

Do either of these sound appealing?

Feb 28, 2011 3:44 PM in response to Jota Be

2 Scripts... 1 without creating a playlist:
this script should run best as an AppleScript application...

tell application "iTunes"

set mysongs to every track whose name contains "angel"

display dialog "now playing " & (count of mysongs) & " songs." giving up after 1

repeat with i from 1 to count of mysongs

set mysong to item i of mysongs

play mysong

##give iTunes a sec to realize current track

delay 1

repeat until mysong ≠ current track

#delay otherwhise cpu load is very highUser uploaded file

delay 0.25

end repeat

stop

end repeat

end tell



to create a real playlist is better anyway, because the script does not have to run all time:
2. Solution with creating a playlist first:

tell application "iTunes"

set mysongs to every track whose name contains "ever"

-- display dialog "now playing " & (count of mysongs) & " songs." giving up after 1

set playlistname to "PLAY_THIS"

try

get playlist playlistname of source 1 as reference

on error

tell source 1

make new playlist with properties {name:playlistname}

end tell

end try

tell playlist playlistname of source 1

delete every track

set shuffle to false

set song repeat to off

end tell

repeat with i from 1 to count of mysongs

set mysong to item i of mysongs

set playlistsongs to {}

try

#try because of error on empty list

set playlistsongs to database ID of every track of playlist playlistname of source 1

end try

if (database ID of mysong) is not in playlistsongs then

copy mysong to playlist playlistname of source 1

end if

end repeat

tell playlist playlistname of source 1 to play

end tell

Mar 2, 2011 4:18 AM in response to hubionmac

Thanks both of you for the suggestions.
So basically the options are the same:
1. through a created playlist.
2. through a loop.

The "playlist solution" would work ok for me if there would be a way to automatically delete the created playlist after it is played. Haven't started to consider this option yet.
And btw, the "playlist" script proposed by hubionmac didn't work for me: i got an error mssg at the line
delete every track

The "loop solution" allowed me to complete the script. Works great when i try running it from AppleScript app.
BUT when running within iTunes, it causes almost a "non responding" state of the app, all the controls in iTunes becomes unresponsive for the time during the script is running. Is this normal? Would it be avoided if the script is run as an application, as hubionmac recommended?

Thanks again for the input,
J.

Mar 2, 2011 4:24 AM in response to Jota Be

Responding to myself regarding the first part:
Even if there would be possible (through the respective commands) to delete the playlist after it stops, this would still imply that the script has to run all the time during the playlist is played, thus leading to the same problem expressed in the second part of my post.
😟

J.

Mar 2, 2011 6:56 AM in response to hubionmac

hubionmac:
Thanks for the correction, i will try it later.

Actually the deletion is not THAT important, is more for practical reasons (let's call it laziness).
The scripted i planned didn't started with the idea of buying playlists, but more as a way to play some tracks "on the fly". Like writing the "keyword" on the right-top search field of iTunes and playing the resulting list, but getting as a result of the search the tracks containing the "keyword" ONLY in its title, and NOT in its album or artist name.
And since i really doubt these search criteria can be modified, i tried to get the same result trough a script.

Anyway, i'll give a try to the "playlist solution" and see how it goes.

Thanks again,
J.

Mar 4, 2011 2:15 AM in response to hubionmac

hubionmac:

I've tried the "playlist solution", as i called it, and works fine. I've also added a couple of "display dialog" and it looks almost as what i wanted.
Just to clarify myself, can you shortly explain me the lines

set playlistsongs to {}
try
#try because of error on empty list
set playlistsongs to database ID of every track of playlist playlistname of source 1
end try
if (database ID of mysong) is not in playlistsongs then
copy mysong to playlist playlistname of source 1
end if


Is not redundant, since it is previously written

tell playlist playlistname of source 1
delete every track

?

Again, is just me trying to fully understand the script.
Thanks a lot,
J.

trying a script in iTunes

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