How can I automatically add tracks to the OS X iTunes Library?
November 14, 2008 2:05 PM   Subscribe

Is there a scriptable solution to add items to the OS X iTunes library?

I am in the process of switching management of my iPod from my older XP desktop to my MacBook. For a couple of years now I have been using a homegrown Perl program to download podcasts, and then running the console version of iTunes Library Updater as a scheduled task to automatically add them to the iTunes library. It's great -- works totally in the background, and when I sync my iPod everything is Just There.

I'd like to duplicate this process on my MacBook. The Perl script obviously ports fine, and I can schedule it using cron, but I can't find a way to automatically add items to the iTunes library. Some things I've considered:

-- iTLU is written in .NET, so that's out (unless it runs under mono -- but I fear that way lies madness.)

-- At Doug's Applescripts, the suggested solution is to add a folder action to the folder you want to watch. However, my understanding is that folder actions only work if the folder is actively being worked with in the Finder (open, having things dropped on it, etc.), so adding files to my podcast folder behind the scenes won't trigger a folder action.

-- There's a Mac::iTunes module on CPAN, but it looks pretty unfinished (for instance, under "Bugs" for Mac::iTunes::Library::Write it says "just wait".)

This is for a MacBook running Leopard. Any suggestions?
posted by harkin banks to Computers & Internet (15 answers total) 2 users marked this as a favorite
I'm not sure your assumption about folder actions is correct. Have you actually tried it? You could just use the command-line to copy an MP3 file into the watched folder, for instance.
posted by xil at 2:16 PM on November 14, 2008

Xil, itunes does not support watched folders, only one-time imports. It's a long standing complaint that Steve Jobs has yet to address.
posted by nomisxid at 2:32 PM on November 14, 2008

Is there some functionality in your perl script that isn't already handled by iTunes' built-in podcasting functionality? I can see automating this process if you want to automate adding music tracks to iTunes, but it seems to me that the built-in podcast handling of iTunes should cover just about everything.

Instead of using perl, you can whip up something in Automator using a combination of Get Folder Contents, Filter Finder Items, Import Files into iTunes, and Move Finder Items to Trash. Save it as an application and then schedule it to run periodically (Lingon is very helpful with this).
posted by phrayzee at 2:43 PM on November 14, 2008

nomisxid: I think we aren't talking about the same thing.

I was talking about the Doug's Applescripts link, where he describes attaching a script to a folder via the Finder. That script would tell iTunes to add the new file to its library.

I'm aware that iTunes itself doesn't know how to watch folders for new files, but that's not the same thing.
posted by xil at 3:00 PM on November 14, 2008

Best answer: $ osascript -e 'tell application "iTunes" to open file "/Users/me/Downloads/newfile.mp3"'
posted by majick at 3:10 PM on November 14, 2008

(actually Applescript might require you to use : delimited paths to a file, but invoking osascript is definitely how you want to tell an application what to do)
posted by majick at 3:18 PM on November 14, 2008

Best answer: majick has the right idea: From your perl script, just run osascript to tell iTunes what to do. This works for me on the command line:
osascript -e 'tell application "iTunes" to add POSIX file "/Users/xil/Psycho_Chicken.mp3"'

posted by xil at 3:22 PM on November 14, 2008

Quicksilver's ITune plugin allows you do do it not exactly automatically, but in just a few keystrokes.

But yeah, on rereading I'm with phrayzee. Use ITunes' podcast page to download everything, then have a smart playlist for genre:podcast that is set to sync to the iPod (either by syncing all playlists or by selecting individual ones, in the preferences). Apple makes it pretty easy to get your podcasts onto an iPod. It's What They Do.
posted by rhizome at 4:01 PM on November 14, 2008

I created an Automator workflow called "Update iTunes"that I can access by right-clicking on my desktop to scan a folder for files and then add them to iTunes. Not quite as neat as what you want but it does the trick, and you can probably use it with what phrayzee has suggested. I'd duplicate the steps but I can't seem to locate it, nor remember what I did!
posted by synecdoche at 4:28 PM on November 14, 2008

Response by poster: xil: no, I hadn't tried setting up a folder action because the Mac person I asked about it told me that it wouldn't work. But you're right -- it works even if the folder is not open. So, yay! However, apparently it won't work recursively (boo!), and I have my podcasts organized by show name. If I have to manually enable the folder action for each show folder it's not the end of the world but it would be nice to have it all automagical-like. I only need to go one level deep -- is it possible to attach a folder action to my Podcasts folder that attaches another folder action to newly created folders?

phrayzee/rhizome: I wrote my application a few years ago, when iTunes podcast handling was very rudimentary (you couldn't specify different retention policies for different podcasts, for example). I have a couple of shows that I archive, so I use Perl to rename the files with a datestamp (and sometimes re-tag them too). Plus it runs even when I forget to leave iTunes open, and it's very lightweight.

WCityMike: Hazel looks intriguing for a couple of reasons, I'll check it out. That's probably the easiest of all of the possible solutions.
posted by harkin banks at 8:45 PM on November 14, 2008

Seconding Hazel: It's worth every last penny.
posted by nathan_teske at 8:58 PM on November 14, 2008

@harkin banks: Ah, that's perfectly valid. You should at least check out the latest iTunes' podcasting abilities, especially the individual feed settings for retention. iTunes won't do auto-renaming with a time-stamp, but it's fairly easy to whip up an Applescript that can be called via iTunes' script menu or some other trigger like Quicksilver.
posted by phrayzee at 11:07 PM on November 14, 2008

BTW, if you're wanting to script anything with iTunes on a Mac, I'd suggest going with Applescript. You might be able to port the functionality of your Perl script over to Applescript. To see all of the innards of iTunes open up the Applescript Editor located in /Applications/Applescript, go to File->Open Dictionary and select iTunes from the list.

I have several scripts that I use to manage tracks. I first got started by downloading one of the scripts from Doug's Scrips (linked above) and customizing it. One of my scripts appends the track's play time to it's name so I can see how a long a podcast will play from my iPod's track list menu. I also use smart playlists to sync TV episodes on my iPod. If there is an episode that I want to skip, I use a script that increments it's play count and sets the "last played date" to the current date. In effect I'm telling iTunes that I watched the episode and I want it cleared out of the smart playlist.
(sorry this paragraph was so long, but I thought it might be relevant)
posted by phrayzee at 11:25 PM on November 14, 2008

Response by poster: Thanks for all the suggestions! I ended up calling xil's version of the osascript command from Perl to add tracks. I couldn't figure out how to delete tracks by file location using Applescript, so rather than doing targeted removals I call a cut-down version of Doug's Super Remove Dead Tracks at the end.
posted by harkin banks at 11:22 PM on November 16, 2008

Response by poster: ... and two months later, an update for anyone who stumbles upon this thread and tries to use xil's approach of using applescript from within Perl to add songs to the iTunes library. After I solved the problem that led to this question, I adapted the technique to a program that brings over music over to my Mac from my home server, where the "master" library lives. Unfortunately I found rather quickly that simply calling osascript within backticks causes all sorts of quoting problems when your filename contains shell metacharacters (e.g. tunes by Jane's Addiction, or song titles that contain parentheses). So, after much googling, this is the solution I came up with:

foreach my $add (@additions) {
my $applescript = <>ADD;
tell application "iTunes" to add POSIX file "$add"
system('osascript', '-e', $applescript);

Important bits here are a) to use a here document contained in a variable, and b) use the list form of system. The here document handles the quoting properly with no crazy regexes needed to escape special characters. (Don't forget like I did that leading whitespace will prevent the parser from finding the string terminator.) And if given a list longer than one item system doesn't hand off the arguments to the shell for interpolation -- instead, it calls the program directly, using the arguments provided. Now it works just fine for filenames containing single quotes and parentheses. Yay!

posted by harkin banks at 8:54 PM on January 7, 2009

« Older How can I hear my iPhone in my 2003 Jetta?   |   Disk Drawer Double Bind Newer »
This thread is closed to new comments.