Loading iTunes library from command line

Having separated iTunes libraries for different setups (e.g., media center, local music, remote playlists) is a powerful way to organize your media library and share resources among different devices (e.g., iPhone synchronization), but it can be painful. While it is possible to load a different iTunes library by opening iTunes and pressing “Alt” at the same time, this is not optimal. Ideally, we would write a script to pass iTunes the library it should load at launch time (e.g., open /Applications/iTunes.app –load-library=$LIBRARY_PATH). However, to the best of my knowledge, the iTunes API does not expose this – which I honestly do not understand. Googling for a workaround I could not find a good/complete solution, so I made my own. Here you have the setup and the scripts that implement it.

The core idea is to replace the metadata files that iTunes uses by symbolic links to the different libraries. In this way, changes on the iTunes end are not required. I will be posting some code while I describe the setup. The complete script can be found at the end of this post, together with an Alfred workflow to manage it in a more convenient way. Both can be found in my github (here)

The first thing to do is to setup your local library. In my case, I wanted to move the whole iTunes library to a Network-Attached Storage (NAS) device. For that, we need to move both the content and the metadata. For the content, it is better to let iTunes do the job. Go to iTunes > Preferences > Advanced and change the iTunes Media folder location to the location you want your library to reside; allow iTunes to keep the library organized; and wait for the library to be moved. This is a one-time operation. From from this point on, the library will be kept in the new location (in the NAS in my case) and we will only play around with metadata to move among the different iTunes libraries.

First, we move the library files.

mv $ITUNES_FOLDER_PATH/sentinel $NAS_LIBS/sentinel_local
mv $ITUNES_FOLDER_PATH/iTunes\ Library.itl $NAS_LIBS/itunes_library_nas.itl
mv $ITUNES_FOLDER_PATH/iTunes\ Library.xml $NAS_LIBS/itunes_library_nas.xml
mv $ITUNES_FOLDER_PATH/iTunes\ Music\ Library.xml $NAS_LIBS/itunes_music_library_nas.xml
mv $ITUNES_FOLDER_PATH/iTunes\ Library\ Extras.itdb $NAS_LIBS/itunes_library_extras_nas.itdb
mv $ITUNES_FOLDER_PATH/iTunes\ Library\ Genius.itdb $NAS_LIBS/itunes_library_genius_nas.itdb

Then, we name our local library in a more convenient way. Note that this library is not necessarily the same we have just moved to the NAS. This means that if you do not have a local iTunes library you should create a new library before executing this code (we just moved the iTunes metadata files to the NAS). To create a new iTunes library, just open iTunes; it will create it for you. Populate it, and then execute the code below to rename the files.

mv $ITUNES_FOLDER_PATH/sentinel $ITUNES_FOLDER_PATH/sentinel_local
mv $ITUNES_FOLDER_PATH/iTunes\ Library.itl $ITUNES_FOLDER_PATH/itunes_library_local.itl
mv $ITUNES_FOLDER_PATH/iTunes\ Library.xml $ITUNES_FOLDER_PATH/itunes_library_local.xml
mv $ITUNES_FOLDER_PATH/iTunes\ Music\ Library.xml $ITUNES_FOLDER_PATH/itunes_music_library_local.xml
mv $ITUNES_FOLDER_PATH/iTunes\ Library\ Extras.itdb $ITUNES_FOLDER_PATH/itunes_library_extras_local.itdb
mv $ITUNES_FOLDER_PATH/iTunes\ Library\ Genius.itdb $ITUNES_FOLDER_PATH/itunes_library_genius_local.itdb

Before we create the symbolic links, we need to tell iTunes to always load the library based on its metadata. This is, not to cache the library. We do this with the following line. Back in the days, this mechanism could be used to load a new library by providing the path to it. However, since aliases became deprecated later versions of iTunes do not support it anymore.

defaults write com.apple.iTunes 'alis:1:iTunes Library Location' ''

Finally, we can just create symbolic links to the library of our preference. To like to the local library:

ln -s -f $ITUNES_FOLDER_PATH/itunes_library_local.itl $ITUNES_FOLDER_PATH/iTunes\ Library.itl
ln -s -f $ITUNES_FOLDER_PATH/itunes_library_local.xml $ITUNES_FOLDER_PATH/iTunes\ Library.xml
ln -s -f $ITUNES_FOLDER_PATH/itunes_music_library_local.xml $ITUNES_FOLDER_PATH/iTunes\ Music\ Library.xml
ln -s -f $ITUNES_FOLDER_PATH/itunes_library_extras_local.itdb $ITUNES_FOLDER_PATH/iTunes\ Library\ Extras.itdb
ln -s -f $ITUNES_FOLDER_PATH/itunes_library_genius_local.itdb $ITUNES_FOLDER_PATH/iTunes\ Library\ Genius.itdb
ln -s -f $ITUNES_FOLDER_PATH/sentinel_local $ITUNES_FOLDER_PATH/sentinel

To link to the NAS:

NAS_LIBS=$MOUNTING_POINT/iTunes

if [ ! -d "$MOUNTING_POINT" ]
then
  mkdir $MOUNTING_POINT
fi

if [ ! -d "$NAS_LIBS" ]
then
  mount -t afp afp://$USERNAME:$PASSWORD@$NAS_IP/$NAS_MEDIA_PATH $MOUNTING_POINT
fi

ln -s -f $NAS_LIBS/itunes_library_nas.itl $ITUNES_FOLDER_PATH/iTunes\ Library.itl
ln -s -f $NAS_LIBS/itunes_library_nas.xml $ITUNES_FOLDER_PATH/iTunes\ Library.xml
ln -s -f $NAS_LIBS/itunes_music_library_nas.xml $ITUNES_FOLDER_PATH/iTunes\ Music\ Library.xml
ln -s -f $NAS_LIBS/itunes_library_extras_nas.itdb $ITUNES_FOLDER_PATH/iTunes\ Library\ Extras.itdb
ln -s -f $NAS_LIBS/itunes_library_genius_nas.itdb $ITUNES_FOLDER_PATH/iTunes\ Library\ Genius.itdb
ln -s -f $NAS_LIBS/sentinel_nas $ITUNES_FOLDER_PATH/sentinel

Note that my bas uses afp. You can change that part to use other protocols. If you want to connect remotely you can create a ssh tunnel and use the protocol of your choice on top of it. To do so:

ssh -C -L 2001:localhost:548 -N root@192.168.1.13
open afp://localhost:2001/

For this example I use root. If you intend to use this it is better to use a user with access restricted to what iTunes should have access to.

The complete, updated version of the script can be found in my github (here) under GPLv2. You can modify and/or complete the script for your own use cases. Feel free to submit a pull request with your changes 🙂

I also created an Alfred workflow to easily manage the script. You can find it in my github too (here). Remember to fill the $USERNAME and $PASSWORD fields with your NAS credentials.

Enjoy! 🙂

Javier.

Leave a comment