Cracked it: automatically adding files to iTunes without copying

Yep, finally did it! Has been bugging me for years!

Click here to skip directly to the solution for adding files to iTunes without copying

So, this is the story: I have iTunes and in spite of it’s faults, it’s excellent at organizing music and setting metadata. It’s also the only reasonable way to sync up your iOS devices or Apple TV with new stuff. Problem is, once we get into video, one iTunes setting becomes an issue: “Copy files to iTunes Media folder when adding to library“.
Screenshot: copying files to iTunes

Many people keep this setting on, because it means iTunes will automatically add songs to the Music folder and put them in the right album folder, etc. Bascially, the files will be copied to your iTunes directory. But when you start adding video files in gigabyte sizes, often stored on external drives, you don’t want to fill up your drive with copies, you’d prefer to have them on the external drive.

There is one simple way to add files “by reference” (e.g. not copying but referring to original location) by simply following this MacWorld hint: basically, hold the Option key while dragging a file to the iTunes window (not the dock icon, and the Command key is not needed in spite of the hint suggesting so).

Problem is, that is easy to forget to do. When you have lots and lots of videos, you may also want to use scripts to automatically add the right kind of files to iTunes. So I went out searching for how to programmatically – in a script – add files as references to iTunes. Turns out it’s not so easy.

First approach was to look at the AppleScript API for iTunes. Following the famous Doug’s AppleScripts for iTunes guide seemed to be the right place to start. There I learnt that I can add files to iTunes by running the AppleScript:

tell application "iTunes"
set newFile to (choose file with prompt "Select a song to add...")
add newFile
end tell

You can replace the “choose file” prompt with a programmatically created file path. Unfortunately, this adds the song according to the “Copy files to iTunes…” preference, but not as a reference.

Second attempt: I couldn’t find a way to emulate the drag ‘n drop in the MacWorld tip using AppleScript, but what I found out is that you can also go to File > Add to Library while holding Option key, and pick your file while still holding the Option key. That will add the file as a reference, not as a copy.

So how to automate? AppleScript has commands like:

tell application "System Events"
key down option
key up option
end tell

… but it all failed on the case that “Add to Library” command would open a choose file box which makes automation impossible.

So what did I finally find? It’s a bit weird. As described in How to move files into iTunes instead of copying, there is a folder called “Automatically add to iTunes” inside the iTunes music folder. Anything you drop there will be added immediately to iTunes. But now I wanted to add a reference to a file elsewhere, not just move the file itself into iTunes. It turns out that if I drop an OS X alias file (e.g. a shortcut) pointing to the original file, iTunes will add it as a reference!

Two peculiarities to note:

  1. Create an alias file by right-clicking and choosing “Make alias” or by drag ‘n drop while holding Shift+Option+Command. Unix symlinks, that otherwise work in OS X, will not add the file as a reference, so avoid them for this purpose.
  2. Once the alias file has been added, iTunes will fail to remove it (normally, files dropped in the “Automatically add” folder will be moved into the iTunes structure). That means that iTunes will see this file over and over again – on your next drop in the folder, iTunes will add both the new file, and the old leftover file again. So you need to manually remove the alias as soon as it’s in iTunes.

But what about scripting? Well, try these two lines on the OS X terminal:

osascript -e 'tell application "Finder" to make new alias file to POSIX file "/path/to/original.file" at POSIX file "/Users//Music/iTunes/iTunes Music/Automatically Add to iTunes"'
rm "/Users/
/Music/iTunes/iTunes Music/Automatically Add to iTunes/original.file"

Replace the paths to the ones correct in your system, of course. The two lines can easily be added to any shell script or python script, which could then process for example your most recently downloaded video files.

I will be adding this feature to a soon finished script that automatically organizes your video collection, so stay tuned!

iTunes silently not importing files

After using the simplest ever method for adding files to iTunes, I kind of forgot the whole importing thing – it’s automatic. But I recently reminded myself of the need to regularly check the “Automatically Add to iTunes” folder for any files that were rejected.

What I saw was that more than 15 MP3-files had not been imported. I tested them and they played fine on VLC, even in QuickTime Player. But in spite of trying all possibilities of adding them to iTunes, they were ignored. Weird.

Something in the files made iTunes throw up. I never figured out what, but I figured out a solution. I went to Mac App Store and downloaded Music Converter (free version). When I drag and drop an audio file onto it, it will do any necessary conversion and send it to iTunes. It can even make Apple Lossless, FLAC, etc! In this case, the conversion was instantaneous, hinting at there was something in the MP3 container or ID3 tags that were wrong (rather than a more time consuming codec conversion / transcoding), but that Music Converter could fix. Now all my songs are imported in iTunes!