Prevent iTunes from resetting my music library location

My iTunes library is located on an network drive, or rather, the music is, and my library file is located on my MacBook.

  • Library file: /Users/glenn/Music/iTunes/iTunes Library
  • Music: /Volumes/MyVolume/Music

But every now and then when I use the laptop somewhere outside my WiFi network, and then come back, fire up iTunes and play a song I get a library full of those pesky little exclamation point icons next to my songs. This is because iTunes have guessed that "Oh, he's not at home, let's reset the music files location to /Users/glenn/Music/iTunes." And there are no music there.

How can I prevent iTunes from doing this "smart" guess, and just leave the setting alone?

PS. And MyVolume is auto-mounted on my laptop. But of course, if iTunes is running when opening up the lid of the MacBook, it takes some time for it to mount, and iTunes won't find the path to the music at first.


Solution 1:

You can make "iTunes Music" an alias to your music folder on your network drive. That should keep iTunes' smartness at bay.

Solution 2:

Finally figured out how to get around this myself. My solution is to have two separate libraries, one local and one on my drive. I created a LaunchAgent (see https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html) that runs a script when the mount path to the disk changes (via WatchPaths). The script checks whether the directory /Volumes/drivename exists, and if it does it sets runs a command to tell iTunes to change to the Library on the drive.

if [ -e "$mountPath" ]
then
   defaults write com.apple.iTunes 'alis:1:iTunes Library Location' -data "drive hex path data"
else
   defaults write com.apple.iTunes 'alis:1:iTunes Library Location' -data "local hex path data"
fi

Otherwise it runs the same command with the path to the local library.

Mac OS does some weird kind of encoding for the hex data path, so I would recommend holding down the option key while starting iTunes, selecting each library, and examine the plist for the path data (note that when you use the defaults command you have to remove the brackets and spaces from the hex data).

This way, if your drive isn't mounted, iTunes will load a local library and won't mess up your main one. This can be handy for traveling, etc when you just want to use the local library to play stuff on iCloud.