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!

4 thoughts on “Cracked it: automatically adding files to iTunes without copying”

  1. Hi, nice article describing some solutions to difficult problems caused by itunes funny ‘auto add to itunes folder’.
    Also in the exact folder is a ‘not added’ folder, where i have found itunes duplicating videos i have downloaded into a folder itunes is not, at all, associated with… ??? that it ‘did not’ add? isnt that a bit stupid?

    I have the add to itunes option un-selected because i like to organise my own folders and have run into huuuge problems with itunes auto add option severely messing up my music and limiting ways in which i can use my music. Which in my opinion is typical of apple, delicately trying to get people to do something ‘their (apples) way’, but thats another topic.
    So back on topic, i do not have that option selected because of past problems; yet still itunes is duplicating my videos into its ‘auto add’ and ‘not auto added’ folders. This to me seems ludicrous, being that videos take up a hell of a lot more room.
    And this wasnt a very evident problem, i felt lucky to have found a huge amount of Gigs of my precious hard drive taken up by videos i had watched and, to my knowledge completely deleted?

    tl;dr – Have the ‘auto add’ feature disabled, itunes still loves to duplicate all my videos i download, even if i dont add them to itunes. Oh, and in secrecy, because i dont see them in my itunes its only that i checked the gay auto add folder. lol

    /itunesrant, apologies on the ranty bit and thanks if you have any advice, as google is failing me on this difficult problemo,
    Sam.

  2. I early on decided that I can skip the time it takes to sort the folders and just let iTunes do it. It may not sort it to my exact preference, but never have caused any errors for me.

    The behavior of auto-add folder for me is that iTunes will move the file into it’s own architecture if it’s allowed to organize. I will test what happens with that option off. Nevertheless, your downloaded videos should only come into the auto-add folder if you move them there, or have a script that does it for you. If they can’t be added to iTunes due to format, it will be kept in a subfolder in auto-add, but should not duplicate.

  3. I’ve been using this method for a while now, specifically because I saw this post. But I just upgraded to Yosemite, and the iTunes 12.0.1.26 that comes with it, and this method no longer seems to work. The alias file just sits there… any other ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *