Add and remove a track from a playlist

I found samples but I guess my real question is about Applescript itself. Is everything a special case?

To add a track to a play list, this seems to work although it is slow:

tell application "iTunes"
	repeat with t in tracks of playlist "Podcasts"
		if (unplayed of t is false) and (played count of t is 0) then
			set l to location of t
			add l to playlist "Temp Podcast List"
		end if
	end repeat
end tell

It appears to be making copies somehow.

Why do I need to take the location? A playlist has “tracks”, not locations. So why doesn’t:

add t to tracks of playlist "Temp Podcast List"

work?

Or, for that matter, why doesn’t:

add location of t to playlist "Temp Podcast List"

work?

Notice that “location” is not even an (documented) attribute of “track” (or “item”) (according to the script editor). But more to the point, my real question is how do I figure this out from the documentation? How do I write code rather than typing like a monkey until something pretends to work?

Next comes the question of removing a track from a playlist. I’ve yet to see how this is done. I did find really complicated scripts just to remove a single item from a list. It all seems so out dated.

I want to do this:

tell application "iTunes"
	repeat with t in tracks of playlist "Temp Podcast List"
		if played count of t is not 0 then
			delete t from playlist "Temp Podcast List"
		end if
	end repeat
end tell

Or something similar. Taking the location of t doesn’t work in this case.

I’m tempted to try the Ruby interface to Applescript. Ruby is much more consistent. But I assume that I’m going to get trapped by the same underlying non-orthogonality of Applescript.

I keep trying to understand Applescript but I must be going about it entirely wrong.

Hi,

it’s quite easy.

The property location of class track is an alias which points to the location of the audio file on the hard disk. The add command expects an alias or a list of aliases as parameter.
The first example makes a physical copy of the podcast tracks. Afterwards there are two identical entries of each track in the library. But in most of the cases it’s sufficient to add a track to a playlist in the usual way like a pointer or alias.
The code to do this is

duplicate track x of library playlist 1 to playlist y

the delete command has no parameter from
use this syntax


tell application "iTunes"
	repeat with t in tracks of playlist "Temp Podcast List"
		if played count of t is not 0 then
			tell playlist "Temp Podcast List" to delete contents of t
		end if
	end repeat
end tell

Note: contents of t dereferences the list item

That makes good sense to me since you want to apply the verb “delete” to the playlist.

I surfed the net more and found this (which I don’t because it implies a search):

delete (some track of library playlist 1 whose database ID is dbid)

(The script set dbid earlier to the database id of the trace.)

I’ve also seen SQL’ish statements like “whose” and other keywords. All the tutorials I’ve seen so far hit at the wrong level for me. I’m coming from a long life of programming. I need just some global mappings of terms I’m use to to Applescript terms. If you have any pointers to such, I’d appreciate knowing about them.

Thank you for your time.

The best source for a programmer wanting to understand AppleScript is
Matt Neuburg’s book, “AppleScript, The Definitive Guide”

Matt covers all the ins and outs and the good and bad of AppleScript.

Have you had a look at appscript.
It is a scripting bridge for Ruby, Python and Obj-C. You may find that scripting
with Ruby or Python is easier to grasp with your background.

Martin Michel is the Python expert on the forum and I have some samples of
Ruby using rb-appscript at AllanCraig.net.

hth,

Craig

Use the ‘add’ command to import new audio/movie files. Use the ‘duplicate’ command to copy existing track objects from one playlist to another.

Pretty much. The advantage of using a Ruby-Apple event bridge is that Ruby is much better than AppleScript at non-application-scripting-related tasks. For manipulating iTunes et-al, however, you still need to understand how application scripting works in principle, and how individual applications’ scripting interfaces work in practice. The former is often explained poorly or incorrectly; the latter almost always very inadequately documented. A lot of it is a matter of learning by example, for which you’ll need at least a basic familiarity with AppleScript as that’s what most existing examples are written in.