Help update an iTunes script to auto run nightly...

I got a script for iTunes from Macupdate but the dev is MIA.
The file is called Rating Writer and is here:
http://www.macupdate.com/info.php/id/14526/rating-writer

Ive had trouble with losing song ratings so this script uses the comments field of the song files to note the songs rating. That way if/when I lose the ratings again within iTunes the songs rating is still attached to the track, not just the iTunes Library file. Then I just have a different Smart Playlist for each of the star ratings and the track is added to the proper playlist based on its “Comment” field value (1, 2, 3, 4, or 5). So if I lose the rating I just run the script and it adds the songs rating back to the iTunes star rating.

So here is the help Im looking for:
I want the script to select all songs and write to the comment field automatically every night while I sleep.

The script is already complete and works, I just need to tell it to select all songs and run at a specified time every night.

Thanks!

Are you operating under 10.4 or 10.5? In either case, a Stay Open application will do it, but if you are running Leopard, it may be neater to use a launchd agent to call the script. Launchd is available in Tiger, but it is a little buggy. The Leopard version is flawless, near as I can tell (so far).

You can also consider using an iCal alarm that calls the script every night, or every other night, whichever you choose.

You say that the script works, but you still need to select all the tracks? Does it not select all the tracks automatically?

EDIT:

I just downloaded the script; it is pretty interesting. It will be a simple task to just take the portion you want (either write or read ratings) and automate it for nightly running. I will be pleased to assist you, just let me know how you want to handle the automation.

Thanks a ton Craig,
I was hoping to modify the script myself to select all tracks in my iTunes library but I just cannot figure out the proper terminology. (Im a designer, definitely not any type of programmer though I thought I would try a few years ago LOL).
I did manage to break the script into two parts since I only want it to run the “Write Ratings” nightly, I will only run the “Restore”, when I need to due to losing my ratings.
Here is what my guess was for getting the script to select all the tracks in the Library: Its not selecting all the tracks btw as Im sure you can probably tell just by looking at it.
BTW I am running Leopard 10.5.1

tell application "iTunes"
	
	--Main dialog box
	set options to display dialog "Rating Writer v1.0" & return & return & "To write your ratings to the song file's ID3 tag, click Write." & return & return & "" buttons {"Cancel", "Write"} default button 2
	
	--This section of the script writes the current ratings to the file's ID3 tag
	if button returned of options contains "write" then
		display dialog "Rating Write" & return & return & "Warning: This script will completly clear the comments field in the song information to write your rating! if you have important contents in the comments field, do not run this script!" buttons {"Cancel", "Continue"} default button 2
		
		set thisPlaylist to view of front window
		set the_source to kind of container of thisPlaylist
		set selectedTracks to (every track of thisPlaylist whose enabled is true)
		set old_fi to fixed indexing
		set fixed indexing to true
		set sel to get a reference to selection
		repeat with t in sel
			set t to contents of t
			set track_rating to ""
			if class of t is file track then
				set track_rating to (get rating of t)
				if track_rating is 100 then
					set comment of t to "My Rating: 5 Stars"
				else if track_rating is 80 then
					set comment of t to "My Rating: 4 Stars"
				else if track_rating is 60 then
					set comment of t to "My Rating: 3 Stars"
				else if track_rating is 40 then
					set comment of t to "My Rating: 2 Stars"
				else if track_rating is 20 then
					set comment of t to "My Rating: 1 Star"
				end if
			end if
		end repeat
		set fixed indexing to old_fi
		display dialog "Writing is complete!" & return & return & "Use Restore when you are ready set the ratings on your new library."
	else
		display dialog "No tracks were selected! Select one or more tracks then run the script again."
	end if
	
end tell

swiz:

You don’t need all the dialog stuff, especially since you want to run it automatically, so I pared it down some more for you:

with timeout of weeks seconds
	tell application "iTunes"
		--This section of the script writes the current ratings to the file's ID3 tag
		set sel to every track of first library playlist
		repeat with t in sel
			if class of t is file track then
				set track_rating to (get rating of t)
				if track_rating is 100 then
					set comment of t to "My Rating: 5 Stars"
				else if track_rating is 80 then
					set comment of t to "My Rating: 4 Stars"
				else if track_rating is 60 then
					set comment of t to "My Rating: 3 Stars"
				else if track_rating is 40 then
					set comment of t to "My Rating: 2 Stars"
				else if track_rating is 20 then
					set comment of t to "My Rating: 1 Star"
				end if
			end if
		end repeat
	end tell
end timeout

I believe it could be optimized even more, how many tracks do you have in your playlist? Cleaner code should mean faster operation (in a vacuum, of course), but if speed is not a major issue, this should work. Even so, I put the whole thing inside of a timeout block just in case it takes a while to run.

Any decision on how to automate? I prefer launchd, but it is not for everyone, and is way in the background, so harder to reach to turn it off (you need to be comfortable with Terminal) and back on if you want. It may be simpler to use either iCal or a Stay Open application to start. Just let me know, and I can help you set it up.

Wow, awesome Craig :slight_smile:
I have just under 11,000 tracks in my Library ( I mow grass all day everyday so I need alot of variety :slight_smile: )
I wouldnt say I am fluent with the Terminal but I definitely feel comfortable using it. If you would suggest launchd then as long as your guiding me Im game for sure.
Im gonna update the script now, Im excited to run it :slight_smile:
Thanks again!

Oh, BTW, I forgot to ask, will this launch iTunes to run the script if iTunes isnt already running? Not that important because my iTunes is almost always open.

I hope you have some nice headphones…

Anyway, I would like to know how long that thing takes to run. Processing 11,000 tracks every night could be an adventure. If you have a number of tracks with no rating at all, you might try replacing the first line of the script with this:

set sel to every track of first library playlist whose rating ≠ 0

That way, only tracks with an actual rating are processed. The only problem is if you every change a rating during the day all the way no stars, then the comment will still carry whatever the previous rating was.

As for the launchd agent, please take at look at this article and this one also. They give you the basics of how to get an agent up and running. Here is my suggestion for a plist file to run your script every night at 11:15 PM:


"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http:// 
[url=http://www.apple.com/DTDs/PropertyList-1.0.dtd>]www.apple.com/DTDs/PropertyList-1.0.dtd">[/url]
<plist version="1.0">
<dict>
        <key>Disabled</key>
        <true/>
        <key>Label</key>
        <string>iTunesRatingsINComments</string>
        <key>LowPriorityIO</key>
        <true/>
        <key>Program</key>
        <string>/usr/bin/osascript</string>
        <key>ProgramArguments</key>
        <array>
                <string>osascript</string>
                <string>/Users/casdvm/Desktop/AutoFlash.scpt</string>
        </array>
        <key>ServiceDescription</key>
        <string>Updates all iTunes tracks every night at 11:15 PM</string>
        <key>StartCalendarInterval</key>
		<dict>  
			<key>Hour</key>
			<integer>23</integer>
			<key>Minute</key>
			<integer>15</integer>
		</dict>
</dict>
</plist>"

The details on how to save this and launch it are all in the first article. It is a pretty easy read, so don’t worry about studying it in detail. The main thing is to get the path to script correct in the second line up there under ProgramArguments.

This should be enough to get things rolling. Let me know if you hit any snags.

Oh yes, if iTunes is not currently running, automatically running the script will launch it.

Good luck,

That looks great Craig.
Im having a little issue now that I wasnt having before.
When I run the script, it gets about 1.5 minutes into processing and an iTunes window pops up and says “File Permission Error”
and this is the message in the Console:
“Nov 16 01:28:59 kevin-husteds-imac-g5 Synergy[199]: Error executing AppleScript (audioscrobblerTimerFired:)”

Ive just disabled Synergy and ran the script again and got the same error from iTunes but nothing showed up in the console… :confused:

Ughh.
Synopsis-
This whole day has been an iTunes nightmare.
Initially I was hoping to ultimately be able to rate my Library when I was on my laptop too, not just when Im at my iMac.
So I figured I would copy my entire Library to my shared folder on my iMac.
That didnt allow me to rate the songs, and in the process of relocating the entire music folder, which I thought was supposed to keep the ratings and such intact, the ratings were lost.
So, I take an hour or two and re-rate viewing my plugged in iPod window because I had not yet updated the iPod.

So I switched my iTunes Library back to my normal Music folder and decided to scrap the Laptop rating idea once and for all.

I just realized, it was still reading the music from my Shared folder even though my iTunes prefs pointed at the Music folder. As simple as iTunes is to use, its a nightmare with its preferences and folder methods.

So, I believe that is where my permissions problem arose, but now I believe I am going to have to AGAIN re-rate viewing my iPod.

Good grief!

Hi,

a suggestion for a speed optimized version of the script
It affects only the rated tracks, whose comment property is empty respectively doesn’t contain “Rating”

with timeout of weeks seconds
	tell application "iTunes"
		--This section of the script writes the current ratings to the file's ID3 tag
		set sel to file tracks of first library playlist whose rating is not 0 and comment contains "Rating"
		repeat with t in sel
			set track_rating to (get rating of t) / 20 div 1
			set comment of contents of t to "My Rating: " & track_rating & " Star" & item (((track_rating is 1) as integer) + 1) of {"s", ""}
		end repeat
	end tell
end timeout

Thanks StefanK,
Am I correct from reading the script to assume that this will skip over any tracks with no rating but will also update the tracks which have already been hit by the script before?

No, once rated tracks won’t be updated.
If you want this, remove

and comment contains "Rating"

I guessed, only new added or rated tracks should be affected

The main reason I wanted to get this script going is because I have had really bad luck with losing my song ratings(usually related to moving iTunes library or something like that) so Im wanting to use this to keep the rating in the audio file instead of iTunes library so in the event that I do lose the iTunes data again I wont lose all my ratings.

Im not so much worried about how long it takes to do its job since I will auto-run it while I am sleeping nightly; I have about 11,000 songs and the current script takes about 5 mintues to process, but Im sure it will take longer on the nights it runs when I have rated more songs.

These forums are great, some of the most helpful I’ve found.

Maybe when I learn a little more I will add onto it so I wont lose play counts too :slight_smile:

I just want to thank you guys for helping out. I’ve got it working great!!!

Just wanted to post my overall wrap-up with this topic.
I ended up using this script for the embedding of my ratings into the comments field of the mp3 tags:

with timeout of weeks seconds
	tell application "iTunes"
		
		set sel to every track of first library playlist whose rating ≠ 0
		repeat with t in sel
			if class of t is file track then
				set track_rating to (get rating of t)
				if track_rating is 100 then
					set comment of t to "My Rating: 5 Stars"
				else if track_rating is 80 then
					set comment of t to "My Rating: 4 Stars"
				else if track_rating is 60 then
					set comment of t to "My Rating: 3 Stars"
				else if track_rating is 40 then
					set comment of t to "My Rating: 2 Stars"
				else if track_rating is 20 then
					set comment of t to "My Rating: 1 Star"
				end if
			end if
		end repeat
	end tell
end timeout

I removed all the dialogs since it will be run on auto when Im not around.

The info for running this with launchd was super useful and since Im a graphics guy I did a little searching on Macupdate and found an app called Lingon which creates launchd files in a typical app GUI frontend. At first I was having trouble, it would run until I emailed the author and he informed me that I had to save it as an app and not a scpt. Here is the link for the app:
http://www.macupdate.com/info.php/id/19879/lingon

For the Restoring of the ratings in iTunes from the comment tags in the mp3 files I used this script:

tell application "iTunes"
	
	set sel to every track of first library playlist
	repeat with t in sel
		set t to contents of t
		set track_rating to ""
		if class of t is file track then
			set track_rating to (get comment of t)
			if track_rating is "My Rating: 5 Stars" then
				set rating of t to 100
			else if track_rating is "My Rating: 4 Stars" then
				set rating of t to 80
			else if track_rating is "My Rating: 3 Stars" then
				set rating of t to 60
			else if track_rating is "My Rating: 2 Stars" then
				set rating of t to 40
			else if track_rating is "My Rating: 1 Star" then
				set rating of t to 20
			end if
		end if
	end repeat
end tell

This will just remain in my iTunes/Scripts folder since I will only run this when I lose my iTunes ratings again; which thanks to you guys I am not afraid of anymore :slight_smile:

Ok, my scripting brethren.

The latest iTunes update appears to have broken my thos script for me, I get this error message when it runs now:
http://web.mac.com/swizcore/Snaps/ituneserror.jpg

Any ideas of how to repair this?

Thanks guys/gals :slight_smile: