Getting the song *just* finished playing.
Hiya,
Im in the process of researching a script to create play statistics for iTunes (Top 10's, Chart movers etc etc). Im relatively new to the whole scripting idea - but Ive done alot of programming in other languages.
What Im trying to do is get iTunes to call a script (or poll iTunes consistently) to pass out the database id of the song it just finished playing. I assume there is an event going on at that time - due to the Play Count incrementing - but Im not sure how to achieve it.
Id appreciate a tip (or 3) if someone could point me in the right direction. Once I get that part working, it should be relatively easy to parse a file to create some really interesting statistics (and even build playlists based on the top songs for a week or month!).
Many thanks for any help you can provide!
Mat
[858 byte] By [
Prenexus] at [2007-11-9 13:26:14]

# 1 Re: Getting the song *just* finished playing.
hi mat,
i am not aware of an event sent by itunes when it switches tracks, before smart playlists came along i wrote a little script which created a playlist of recently played songs and a daily playlist.. its called "tunespy" and is available from doug's site at www.malcolmadams.com/itunes
its an idler app which means that it sits idle doing nothing on the system then every few seconds (which you could specify) it would ask itunes what its playing, if the track had changed then it would add it to the playlist.. something similar would do for you but instead of adding the new song you could do what you like with the old one...
have a look at the code, its well commented so should be easy for you to follow.. once you have downloaded it you can open it up using the script editor found in applications/applescript.. fire up the editor and under "file" menu is an open option.. take that and select the downloaded file
HTH and post back if you need help mod'ing it for your purposes...
deeg at 2007-11-15 17:25:12 >

# 2 Re: Getting the song *just* finished playing.
Many thanks, I shall take a look - I thought I might have to do something similar to this if the hooks were'nt provided.
Does this script place much of a demand on the system? Im a little wary of polling the system too often and causing a drag.
Hopefully once I look at the script in detail Ill understand a little better whats going on ;)
Mat
# 3 Re: Getting the song *just* finished playing.
there is some overhead but this is miniscule compared to itunes.. i believe an idler only wakes up when there are available resources anyway which is why its best not to use one if you require a really accurate interval between checks but in most cases in itunes a second later does not make much difference.
if timing is of more importance then a repeat loop with a delay of x seconds is more accurate but the downside is that this would use more resources.. the code for a repeat loop is...
tell application "iTunes"
repeat
beep
delay 15
end repeat
end tell
the number after the delay is the number of seconds delay between each cycle of the loop
deeg at 2007-11-15 17:27:10 >

# 4 Re: Getting the song *just* finished playing.
Ok, thanks to your tips deeg Ive managed to get a prototype up and working :)
I borrowed some ideas from a few different scripts, including one that exports the iTunes database to AppleWorks and one that writes to SimpleText on command.
At the moment the script requires that you have the database open in the background, so my next question is - Is there anyway to make Applescript silent?
Ideally Id like to silently write to a text file - ie, not have to have the text file open to add entries to it.
Is there anyway to do this? If I can write silently to a file, then I can provide a comma delimited file to a database for import at a later date, rather than have to have the database open at all times.
Many thanks :)
Mat
# 5 Re: Getting the song *just* finished playing.
hi mat,
yes this is possible...
see http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.11.htm for some 'offical' examples of writing to files and logs
deeg at 2007-11-15 17:29:11 >

# 6 Re: Getting the song *just* finished playing.
I was trying to creat chart type playlist with the smart playlist, and I didn't succeed. So I realised I couldn't...
I would need a "most play count in a time period - lest's say in the last week or month" option, so I could do a Top 50 of the month or something like that... That would be nice.
Are those scripts are doing stuff like that? I never scipted in my life ;-)
If not, is there a way??
Thanks! :-)
# 7 Re: Getting the song *just* finished playing.
hi,
well mine aren't :).. TuneSpy does have the option to create a daily playlist but over a month thats going to be upto 31 extra playlists in your source window.. not really a good idea..
do you have a spread sheet app at all ? it might be possible to take a daily playlist and add its contents to the spreadsheet the next day, then delete the playlist.. you could then create a monthly playlist from the spread sheet app if its scriptable..
have you checked out Charts-O-Mat (http://www.versiontracker.com/dyn/moreinfo/macosx/13661) ?
deeg
deeg at 2007-11-15 17:31:14 >

# 8 Re: Getting the song *just* finished playing.
No, I don't have a spread sheet app at all. And to be honest, I never worked with that! :D
Be I give Charts-O-Mat a try to see how it works...
Thanks! :)