My Phils-iTunes-Library-Switcher-Script for notebook-users

Hi,

I guess the easiest way to say thank you for the nice support at MacScripter is to release the source code… and say thank you.

“Thank you!” :slight_smile:

I’ve written a script that easily switches between two iTunes-libraries. If you’re using a notebook and have most of your music on an external device and only a subset of the music on your internal drive, this Script is for you.

Check it out at:
http://snapup.net/blog/2005/10/29/phils-itunes-library-switcher/

Feel free to leave a comment or ask for more documentation (I’m not that good in writing manuals)

The Source-Code:


-- PILS: Phils-iTunes-Library-Switcher
-- written by: phil
-- release date: 2005-10-27
-- version: 0.1
-- released under GPL license http://www.gnu.org/licenses/gpl.html
-- Contact: phil@snapup.net
-- Newest version can be found on my blog: http://www.snapup.net/blog/

-- *** What this Script does ***
-- This script will switch between two different iTunes libraries.
-- Nice for everyone who has a large MP3-collection on an external device and only a subset of music while on the go.

-- *** Installation ***
-- 1. You need to create to new folders in your Music directory in your home-directory
--     ~/Music/iTunes-external, and
--    ~/Music/iTunes-internal.
--    You can easily rename the current iTunes-Folder to one of the above, duplicate it, name it to the other and then
--    continue to work with each copy.
-- 2. You need to create symbolic-link in the iTunes-external folder to your external device where all your MP3s are stored.
--   ln -s "/Volumes/YOUR-EXTERNAL-DEVICE-NAME/FOLDER-TO-MP3s" "~/Music/iTunes-external/iTunes Music"
-- 
-- If you have any questions or suggestions how to optimize code or add features--> email me

-- *** How does it work? ***
-- The Script will first check if iTunes is running and close it.
-- Then it checks if all directories exist, and asks what it should do: switch to the internal or external profile, launch iTunes etc.
-- I've put in some error tracking into that script order to give you some error messages if things are not setup correctly.

-- *** Thank you to ***
-- Nigel Garvey and archseed for the hints about how to close iTunes on MacScripter.
-- Topic: http://bbs.applescript.net/viewtopic.php?pid=47474#p47474
-- Also thanks to all you nice people helping out Scripting Newbies like myself.
-- You make the world go round!

-- Some last words: Go vegan!

property internal_music_directory : POSIX path of (path to home folder from user domain) & "Music/iTunes-internal"
property external_music_directory : POSIX path of (path to home folder from user domain) & "Music/iTunes-external"
property path_to_external_music_files : ""
property internal_music_directory_exists : false
property external_music_directory_exists : false
property internal_is_active : false
property external_is_active : false
property path_to_external_music_files_valid : false

close_itunes()
check_configuration()
switch_configuration()

-- Subroutines 
on close_itunes()
	-- Check if iTunes is currently running. If it is, ask to shutdown iTunes and continue OR quit script
	tell application "System Events"
		if (application process "iTunes" exists) then
			activate
			display dialog "iTunes is running. Shut down iTunes Now?" buttons {"Cancel", "Quit Now"} default button 2 with icon stop giving up after 10
			if button returned of the result is "Quit Now" then
				tell application "iTunes" to quit
				repeat while (application process "iTunes" exists)
					delay 0.5
				end repeat
				beep
				display dialog "iTunes has quit now." buttons "Continue" giving up after 2
			else
				-- Do not shutdown iTunes or waited longer than 10 seconds
				-- abort Script:
				error number -128
			end if
		else
			-- iTunes is not running
		end if
	end tell
end close_itunes


on check_configuration()
	-- Check if all the neccessary directories exists and determnine which configuration is present.
	
	-- Check if internal-music-directory exists or if it is active
	-- VARs: internal_music_directory_exists, internal_is_active
	try
		alias POSIX file (internal_music_directory)
		set internal_music_directory_exists to true
		set internal_is_active to false
	on error
		-- if not internal_music_directory_exists then
		try
			alias POSIX file (internal_music_directory & "-active")
			set internal_music_directory_exists to true
			set internal_is_active to true
		on error
			set internal_music_directory_exists to false
			display dialog "Error 01: Internal music directory /itunes-internal is missing." & return & "Read the manual!" buttons {"Quit"} default button 1 with icon stop
			error number -128
		end try
		-- end if
	end try
	
	-- Check if external-music-directory exists or if it is active
	-- VARs: external_music_directory_exists, external_is_active
	try
		alias POSIX file (external_music_directory)
		set external_music_directory_exists to true
		set external_is_active to false
	on error
		-- if not external_music_directory_exists then
		try
			alias POSIX file (external_music_directory & "-active")
			set external_music_directory_exists to true
			set external_is_active to true
		on error
			set external_music_directory_exists to false
			display dialog "Error 02: External music directory /itunes-external is missing." & return & "Read the manual!" buttons {"Quit"} default button 1 with icon stop
			error number -128
		end try
		-- end if
	end try
	
	-- Check if path to external music files exists and if it is valid (= if external device is attached)
	try
		alias POSIX file (external_music_directory & "-active/iTunes Music")
		set path_to_external_music_files_valid to true
	on error
		try
			alias POSIX file (external_music_directory & "/iTunes Music")
			set path_to_external_music_files_valid to true
		on error
			set path_to_external_music_files_valid to false
		end try
	end try
end check_configuration


on switch_configuration()
	if (internal_is_active = external_is_active) and path_to_external_music_files_valid then
		-- both directories are inactive and it's possible to use internal OR external
		-- Ask which profile should be used.
		display dialog "Currently neither Internal nor External is active!" buttons {"Quit", "Use external", "Use internal"} default button 3 with icon caution
		if button returned of result = "Use external" then
			switch_to_external()
		else if button returned of result = "Use internal" then
			switch_to_internal()
		else
			error number -128 -- Quit-button was hit
		end if
		
	else if not path_to_external_music_files_valid then
		-- Don't care which one is active. If external music directory is not present, 
		-- Ask to use internal
		display dialog "Path to external music files couldn't be found." buttons {"Quit", "Try external", "Switch to internal"} default button 3 with icon caution
		if button returned of result = "Try external" then
			switch_to_external()
		else if button returned of result = "Switch to internal" then
			switch_to_internal()
		else
			error number -128 -- Quit-button was hit
		end if
		
	else if internal_is_active then
		-- ask to change to external
		display dialog "Currently iTunes is using the <Internal> profile." buttons {"Quit", "Switch to external", "Continue with internal"} default button 3
		if button returned of result = "Switch to external" then
			switch_to_external()
		else if button returned of result = "Continue with internal" then
			switch_to_internal()
		else
			error number -128 -- Quit-button was hit
		end if
		
	else if external_is_active then
		-- ask to change to internal	
		-- ask to change to external
		display dialog "Currently iTunes is using the <External> profile." buttons {"Quit", "Switch to internal", "Continue with external"} default button 3
		if button returned of result = "Continue with external" then
			switch_to_external()
		else if button returned of result = "Switch to internal" then
			switch_to_internal()
		else
			error number -128 -- Quit-button was hit
		end if
		
	else
		-- booh? Something went wrong. One of above if-when-conditions should catch every possibility
		display dialog "Error-Code 04: Something went wrong." & return & "Please contact the author." buttons {"Quit"} default button 1 with icon stop
		error number -128
	end if
end switch_configuration


on switch_to_external()
	-- if internal_is_active change pathname then links iTunes-folder to external profile
	try
		if internal_is_active then do shell script "mv " & internal_music_directory & "-active " & internal_music_directory
		if not external_is_active then do shell script "mv " & external_music_directory & " " & external_music_directory & "-active"
		do shell script "ln -sfh " & external_music_directory & "-active " & POSIX path of (path to home folder from user domain) & "Music/iTunes"
	on error
		display dialog "Error-Code 06: Couldn't switch to external. Please contact the author." buttons {"Quit"} default button 1 with icon stop
		error number -128
	end try
	--	display dialog "External is active!" buttons {"Quit"} default button 1
	--	error number -128
	launch_itunes("External is active")
end switch_to_external


on switch_to_internal()
	-- if external_is_active change pathname then links iTunes-folder to internal profile
	try
		if external_is_active then do shell script "mv " & external_music_directory & "-active " & external_music_directory
		if not internal_is_active then do shell script "mv " & internal_music_directory & " " & internal_music_directory & "-active"
		do shell script "ln -sfh " & internal_music_directory & "-active " & POSIX path of (path to home folder from user domain) & "Music/iTunes"
	on error
		display dialog "Error-Code 07: Couldn't switch to internal. Please contact the author." buttons {"Quit"} default button 1 with icon stop
		error number -128
	end try
	--	display dialog "Internal is active!" buttons {"Quit"} default button 1
	--	error number -128
	launch_itunes("Internal is active")
end switch_to_internal


on launch_itunes(message)
	display dialog message & return & "Launch iTunes now?" buttons {"Quit", "Launch iTunes"} default button 2
	if button returned of result = "Launch iTunes" then tell application "iTunes" to launch
	error number -128
end launch_itunes

Model: iBook G3, 800 Mhz, 640 MB RAM
AppleScript: 1.10
Browser: Safari 412.5
Operating System: Mac OS X (10.4)