Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jabrowntx

macrumors 6502
Original poster
Jul 5, 2007
261
1
Thanks in advance for your help. I am definitely an Applescript rookie and have managed to get as far as I have with the script below through lots of trial and error (and about 6 hours). I'm sure there is a more elegant way to do this!

I'm trying to write a script that consolidates the played count of duplicates in a playlist to a single track then resets the played count of the others to null. I have the meat of the script working, but only for a single track name. What I'd like to do is get a list of all of the track names in the playlist based on properties in comment (contains "already rated). I've tried:

Code:
set my_tracklist to (get database ID of every track of playlist "Dupes" 
whose comment contains "already rated")

which is fine, but how can I convert that database ID to the track name for use in something like:

Code:
set myCounter to get database ID of (every track of playlist "Dupes" 
whose name is "Behind Blue Eyes" and comment does not contain "already rated"

where "Behind Blue Eyes" would be replaced with the variable of the conversion of the database ID to track name

Below is the full script that works when I'm able to specify the track name. I'd like to be able to wrap this in a repeat with to loop through all of the tracks in the playlist.

Code:
tell application "iTunes"
	-- myCounter is the track used to maintain the played count for duplicates
	
	set myCounter to get database ID of (every track of playlist "Dupes" whose name is "Behind Blue Eyes" and comment does not contain "already rated")
	set myCounter to (myCounter as integer)
	set upDateMe to get played count of (every track whose database ID is myCounter)
	
	-- noCounter is the tracks that are going to be added to myCounter then reset to null
	
	set noCounter to get database ID of (every track of playlist "Dupes" whose name is "Behind Blue Eyes" and comment contains "already rated")
	
	set my_list to noCounter
	
	repeat with my_item in my_list
		set temp to get (played count of every track whose database ID is my_item)
		-- display dialog "temp is now " & temp
		set temp1 to upDateMe + temp
		set upDateMe to temp1
		set (played count of every track whose database ID is my_item) to ""
	
	end repeat
	set (played count of every track whose database ID is myCounter) to upDateMe
end tell
 
You might have better luck with something like this:

Code:
	set my_tracklist to (every track of playlist "My Purchases" whose comment is "already rated")

I tried to use "database ID" and had little success. This gives you a reference, which you can then access the properties, like:

Code:
	database ID of item 1 of my_tracklist

Although iTunes can repeat track names, it shouldn't repeat track references, so you might not need database IDs. However, I don't believe my iTunes library has many (or any) duplicates, so I don't have the best setup to test.

Unfortunately, this doesn't work:

Code:
	properties of item 1 of my_tracklist

... and it should.

mt (BTW, OS: 10.6.1, iTunes: 9.0.1)
 
You might have better luck with something like this:

Code:
	set my_tracklist to (every track of playlist "My Purchases" whose comment is "already rated")

Perfect! Thank you, it's a much better and more effective script now -- and such a simple change. Obviously, I was stuck on a single way to try to accomplish looping through the tracks (i.e, the database ID) because I knew I would need it to do other things within the script.

I had the original working last night but with a 300+ element list which would have meant some manual processing if the contents of my Dupes playlist change.

This is the final, working result for anyone who cares.

Code:
tell application "iTunes"
	
	
	set song_titles to name of every track of playlist "Dupes" whose comment contains "counter"
	
	repeat with my_song in song_titles
		-- myCounter is the track used to maintain the played count for duplicates
		
		set myCounter to get (the database ID of every track of playlist "Dupes" whose name is my_song and comment contains "counter")
		set myCounter to (myCounter as integer)
		set upDateMe to get played count of (every track whose database ID is myCounter)
				
		-- noCounter is the tracks that are going to be added to myCounter then reset to null
		
		set noCounter to get (the database ID of every track of playlist "Dupes" whose name is my_song and comment contains "already rated")
		
		set my_list to noCounter
		
		repeat with my_item in my_list
			set temp to get (played count of every track whose database ID is my_item)
			set temp1 to upDateMe + temp
			set upDateMe to temp1
			set (played count of every track whose database ID is my_item) to ""
		end repeat
		set (played count of every track whose database ID is myCounter) to upDateMe
		
	end repeat
end tell
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.