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!

How to move files into iTunes instead of copying

If you have Apple TV or iPad to watch movies, and you want to add movies from outside (perhaps downloaded by some way or another…), you quickly encounter one annoyance: it can be quite slow to copy a 1-2 GB movie file into iTunes. It is because iTunes often is setup to copy everything into its own organized folders (under ~/Music/iTunes/).

Even worse, if you have added it to iTunes, you will now have two copies, eating up double space on your drive until you delete the original.

There is however one very simple way to avoid this. Under the iTunes folder there is one called “Automatically add to iTunes”. If you are on a Mac, you can basically just drag that folder onto the Dock or in the “Places” part of the Finder sidebar. Next time you need to add any file to iTunes without wanting to wait for a copy – just drop the files on this folder shortcut, and the files will be moved and instantly added to iTunes.
Try it!