battis.net and I'm all out of bubble gum…

I just mentioned that my wife switches between two different iTunes libraries, including one that lives on a shared volume on our home server. There’s a great tip on Stack Overflow for writing a script to automate that swap between iTunes libraries (rather than having to hold down option while iTunes loads and then click around to find the library you want). But it seemed like it might be nice to document for posterity the full script that we’ve built, since it handles not just the library swapping, but also the mounting of the shared volume as well.

This script takes a little prep work, both generating the library location property and storing the authentication information in the user keychain. The Stack Overflow tip explains the former, and the latter is as easy as connecting to the server and remembering to check the “Save Authentication” checkbox during the authentication process.

-- set server connection information here
property serverProtocol : "afp://"
property serverName : "your-server-name-here.local"
property volumeName : "shared-volume-name-here"
 
-- define the desired library location and save the current library location (http://stackoverflow.com/a/1693973/294171 explains exactly how to generate the appalling hexadecimal value for iTunesLibraryLocation below)
property iTunesLibraryLocation : ""
property libraryLocationPref : "com.apple.iTunes 'alis:1:iTunes Library Location'"
 
-- try to mount share from server
try
	-- authentication credentials stored in keychain
	mount volume (serverProtocol & serverName & "/" & volumeName)
on error
	-- if we can't mount the share, we're done
	display dialog "The shared volume “" & volumeName & "” on the server “" & serverName & "” could not be mounted, so the remote iTunes Library cannot be used. Please check that you are on the same network as “Leviathan” and try again later." buttons ("Le sigh") default button 1 giving up after 15
	return
end try
 
-- quit iTunes if it's running
tell application "System Events"
	if exists (application process "iTunes") then
		tell application "iTunes" to quit
	end if
end tell
 
-- set the desired library location
do shell script "defaults write " & libraryLocationPref & " " & quoted form of iTunesLibraryLocation
 
-- comment the above shell script and uncomment the one below to use the default iTunes Library
-- do shell script "defaults delete " & libraryLocationPref
 
-- (re)open iTunes
tell application "iTunes" to activate

March 18th, 2014

Posted In: How To

Tags: , ,

css.php