hi there, i’m pretty green and not entirely sure whether what i want this script to do can even be done. . . but i figured it was worth a try:
i’m trying to train an applescript to automatically increase the rating of a song in itunes whenever it successfully completes. i have the rating adjustment part all worked out - what i can’t come up with is a way to get the script to run in response to an action (i.e. reaching the end of the track). i was assuming that since itunes itself updates track info upon completion that it seems to consider something there as an event, and perhaps i could piggyback onto that? any help is appreciated.
after further research, i can semi-answer my own question in that itunes itself looks like it isn’t going to help with this project. so my other solution would be to set up a script with some sort of idle handler that could check track info and run the rating update if the track were found to be in the final minute (for example). any suggestions on how to code that?
I believe that you are correct in that only a script running in the background (all the time) is going to do the job. I am assuming this is on a Mac, not an iPod, as scripting is not possible on the pod.
The big question I have is when do you want the rating to actually change? Do you want the track to finish, or just be in the final few seconds? I am not sure it matters, but I think the scripting would be simpler for a background script to run as a loop that finds the current track playing, and simply checks iTunes every 5 or 10 seconds to A) see if that is still the current track, and B) if not, has the play count increased by 1? If A is false and B is true, then change the rating. If A is true, then it will just come back in another 5 or 10 and recheck. If both are false, you have a new track anyway, and it is automatically rechecking.
I can certainly help with the iTunes scripting, but I am unsure about the background issues.
Thanks for the help. I am intending to use this on a Mac, to answer your first question. As for when I’d want the rating to change, I suppose after successful completion would be ideal - but I’d be perfectly happy if it updated in the last 5-10 seconds as well. Either way it’s roughly the same functionality. To me, at any rate, catching it by player position seems easier so that is what I’ve been toying with. . . though if you have your own idea I’d love to hear it.
Here’s what I’ve managed so far
global last_rated
set last_rated to ""
on idle
tell application "iTunes"
if player state is not stopped then
if player position is greater than ((current track's finish) - 20) then -- trying to ensure we're in the final 20 seconds
copy name of current track to now_playing
if now_playing is not last_rated then
my step_up(get current track) -- rerate script goes here
copy now_playing to last_rated
end if
end if
end if
return 15
end tell
end idle
to step_up(this_track)
tell application "iTunes"
try
set rat to (get this_track's rating)
if rat is less than or equal to 98 then set this_track's rating to (rat + 2)
if rat is greater than 98 then set this_track's rating to 100
end try
end tell
end step_up
Does that look right? I think it’s doing what it’s supposed to, but I’m hesitant to off and let it fly without another opinion.
Sorry, I did not see your script before I put something together. I will take a look at it, but I believe it may be more than necessary. What I have here is very close, but not perfect:
tell application "iTunes"
if player state is not stopped then
set {now_track, now_trk_count} to {current track, (current track)'s played count}
repeat
if current track is not now_track then
delay 2 --Seems to need the delay for the [played count] variable to catch up.
if (now_track)'s played count is not equal to now_trk_count then
set (now_track)'s rating to 60 --(Each star is 20, so 60 would be 3 stars)
end if
set {now_track, now_trk_count} to {current track, (current track)'s played count}
end if
end repeat
end if
end tell
I am not sure what is wrong, but unless the user keeps clicking the iTunes application window, it stops after each track. It is really infuriating, I am not sure what to do about it. It is probably something to do with my endless repeat, but I have run out of play time for today. Perhaps someone else will come along and proffer another solution, otherwise, I should have some time later tonight or tomorrow.