iTunes Playlist Check if Track Exists

Hi

I am struggling to work out how to determine if a particular track exists in a particular playlist and to add it to the playlist if not there:

This is what I have (in relevant part):

try
set tempTrack to (1st track of library playlist 1 whose name is nm and time is ti and album is alb and artist is artst and album artist is albartist and composer is cpsr and track number is trck)
if not (my playlist iPodplaylist contains tempTrack) then
duplicate tempTrack to playlist iPodplaylist
end if
end try

The if statement does not seem to ever capture anything so even when the loop runs into a track that is not in the playlist, it does not add it (as it should).

All above is inside a tell statement for 'iTunes"

Any suggestions?

Also, I am trying to limit the number of tracks that my script operates on in my main library. Am stuck trying to limit tracks to those added after a certain date. Code snippet I have (which does not work" is as follows:

set tracksTodo to tracks of library playlist 1 of iPodSource whose date added is after “20/06/2006”

Playing with dates is not easy, I know. But you can see hopefully what I am trying to do…

Any suggestions?

Thanks

Hi,

one of the biggest difficulties in AppleScript is the date class.
Often it depends on the date format in international settings. which can be different.
In your case you’re comparing a date, therefore the value to compare must be a date, not a string

set tracksTodo to tracks of library playlist 1 of iPodSource whose date added is after date "20/06/2006"

this works probably in NZ and UK, but not in Germany or in US, because in Germany the date format is 20.06.2006 and in US 06/20/2006

Hi, kiwilegal.

Your ‘if’ line’s erroring because its syntax is incorrect and the ‘try’ block’s causing the error to be ignored. But with a correct syntax (if there is one) the duplication would always occur, because the tracks in one library are always different from those in another, even when they play the same source. It’s also possible to have two tracks with the same source in the same playlist.

One way round this would be to use the same filter on the destination playlist:

tell application "iTunes"
	try
		set tempTrack to (1st track of library playlist 1 whose name is nm and time is ti and album is alb and artist is artst and album artist is albartist and composer is cpsr and track number is trck)
		if not ((1st track of playlist iPodplaylist whose name is nm and time is ti and album is alb and artist is artst and album artist is albartist and composer is cpsr and track number is trck) exists) then
			duplicate tempTrack to playlist iPodplaylist
		end if
	on error msg
		display dialog msg
	end try
end tell

This could probably be optimised a bit, but I don’t know what the rest of your script’s doing.

Stefan’s already pointed out that dates should be specified as AppleScript date objects. You should also use ‘comes after’ rather than ‘is after’.

As with NG, I do not know what the rest of you script is doing,
But the library for iTune has a database ID

database ID database ID (integer, r/o) : the common, unique ID for this track. If two tracks in different playlists have the same database ID, they are sharing the same data.

so here is an example.

tell application "iTunes"
	set iPodplaylist to "TESTLIST"
	set tempTrack to item 1 of selection
	set database_ID to database ID of tempTrack
	try
	
		if not ((1st track of playlist iPodplaylist whose database ID is database_ID) exists) then
			duplicate tempTrack to playlist iPodplaylist
		end if
	on error msg
		display dialog msg
	end try
end tell

Hi Mark,

this won’t work in this case, because iPodplaylist is a playlist on an iPod, which has different database IDs

Ah I see. I knew the OP was doing this for an iPod but was getting the list from a playlist on the Mac not the iPod.

will this work for a localised date format

property Localised_date_format : "20/06/2004" as string -- Set your own countries format
tell application "iTunes"
	
	set tempTrack to item 1 of selection
	set date_ to "set date_time to date " & quote & Localised_date_format & quote
	
	set date_added to date added of tempTrack
	if date added of tempTrack comes after (run script date_) then
		say "yes"
	else
		say "no"
	end if
end tell

**EDIT
Just set my date format in System prefs to USA
tested the script with “06/20/2004” - USA date format

quit iTunes and Script editor so they would pick up the new date format.

It worked.

Not with german date format settings.
One possibility to set a “worldwide-valid” date is

tell (current date) to set theDate to it - time
tell theDate to set {year, its month, day} to {2006, 6, 20}

Just woke up and seen all your posts. Will work through them, which will hopefully allow me to complete what I was trying to do

Many thanks

Another way is simply to write a date object specifier into the script, in your own format, and compile it. A compiled date is valid anywhere.

-- Date object specified according to my short date preference.
set cutoffDate to date "20/6/06"
-- After compilation, the script text looks like this in Script Editor on my machine.
set cutoffDate to date "Tuesday 20 June 2006 00:00:00"

While my text above would annoy many people because they’d need to alter it to get it to compile on their machines, the script I’ve compiled from it would give them no problem at all. They could open it in their own script editors and see the date specifier displayed in their own preferred format.

One problem with Stefan’s method is that the result can depend on when the script’s run. If it’s run on the 31st of a month, setting the month of the current date to 6 will cause an overflow into July, because June only has 30 days. The day needs to be set to less than 29 first, so that no overflows occur:

tell (current date) to set theDate to it - time
tell theDate to set {day, year, its month, day} to {1, 2006, 6, 20}

Or, indeed:

tell (current date) to set {time, day, year, its month, day, theDate} to {0, 1, 2006, 6, 20, it}