I learned how to create a playlist in a previous question, but now I can't figure out how to add tracks to it. Right now I have:
tracks.sort(key=lambda tup: tup[0])
i = 0
for trackList in generatePlaylists(tracks,10):
i += 1
playlistname = str(i)
p = {'name': playlistname}
playlist = iTunes.classForScriptingClass_("playlist").alloc().initWithProperties_(p)
iTunes.sources()[0].playlists().insertObject_atIndex_(playlist, 0)
# Find the playlist I just made
for playlist in iTunes.sources()[0].playlists():
if playlist.name() == playlistname:
newPlaylist = playlist
# Add the tracks to it
for track in trackList:
print track[1].name()
iTunes.add_to_(track[1],newPlaylist)
My tracks are in a list of tuples tracks
, where the first element of the tuple is a score and the second is the actual track object. generatePlaylists
is an iterator which splits all library tracks into 10 lists.
The above code runs without error, but in iTunes the playlists are empty.
First, here's the short answer:
The problem is that
iTunes.add_to_
sends theadd
command, which takes a file (alias) and imports it into a playlist; you want to send theduplicate
command, which takes any object and makes another copy of the object. You don't have a file, you have a track. (You could get a file viatrack.location()
, but you don't want to re-import the file, just copy the track over.)Also, in this case, you need to call the method on the track, rather than calling it on the app and passing it the track.
The first half of this is hard to explain without a solid understanding of the iTunes object model (and the AE model underneath it). But you don't really need to understand it. In most cases, by looking over the iTunes scripting dictionary (in AppleScript Editor) and trial and error (in AppleScript Editor or with py-appscript) you can figure it out what you want. (Just make sure you're working on a scrap library, or have a backup…) In this case, the only commands it could possibly be are
add
,copy
,duplicate
, ormove
, so just try them all and see what they do. Or, alternatively, go to dougscripts and download a bunch of samples and find one that does what you want.The second half of this, figuring out how to translate to ScriptingBridge… well, I can't explain it without going into a long rant on SB (which hhas does much better than me, if you want to read one). But the basics are this: As far as iTunes is concerned,
duplicate
is a command. If you give it a direct object (tell application "iTunes" to duplicate theTrack to thePlaylist
) it'll use that; if not, you're asking the subject to duplicate itself (tell theTrack to duplicate to thePlaylist
). It works exactly like English. But SB insists on an object-oriented model, whereduplicate
is a method on some object. So, only one of those two forms is going to work. In general, you can figure out which by just looking atdir(iTunes)
anddir(track)
to see which one has a method that looks like the command you want.As you can tell from the above, you've got a lot of trial and error ahead of you if you're trying to do anything complicated. Good luck, and keep asking.
PS, I have no idea why your code fails silently. The obvious way the
add_to_
method should translate into a command should raise a -1708 error (as appscriptiTunes.add(track, to=newPlaylist)
or AppleScriptadd theTrack to newPlaylist
both do…).