Skip to content

Instantly share code, notes, and snippets.

@battis
Last active August 21, 2018 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save battis/966e6314355a147d5e78096d09a29394 to your computer and use it in GitHub Desktop.
Save battis/966e6314355a147d5e78096d09a29394 to your computer and use it in GitHub Desktop.
Toggling between iTunes Libraries
#!/usr/bin/env bash
#
# usage: iTunesLibraryToggle [library] [playlist]
# e.g. iTunesLibraryToggle "$HOME/Music/iTunes (Music & Audiobooks)" Audiobooks
iTunesLibraryToggle() {
# make sure the iTunes directory is, in fact, a symlink
iTunesDir=$HOME/Music/iTunes
if [[ ! -d "$iTunesDir" || -L "$iTunesDir" ]] ; then
# check to see if symlink is already pointing at the library we're trying to open
if ! readlink "$iTunesDir" | grep -q "$1" ; then
# quit iTunes before messing with settings
if pgrep -xq -- "iTunes" ; then
printf "Quitting iTunes"
osascript -e 'quit app "iTunes"'
while pgrep -xq -- "iTunes"; do
printf "."
sleep .25
done
printf "\n"
fi
# reset iTunes database location...
printf "Nuking iTunes defaults from orbit\n"
defaults delete com.apple.iTunes 'Database Location'
# ...and link to the new iTunes library location
printf "Linking $1 as the iTunes Library\n"
rm "$HOME/Music/iTunes"
ln -s "$1" "$HOME/Music/iTunes"
else
printf "\033[1;32m$1 is already linked as the iTunes Library\033[0m\n"
fi
# re-open iTunes with new library location
printf "Opening iTunes\n"
open "/Applications/iTunes.app"
# open requested playlist (if any)
case "$2" in
"Music" | "Audiobooks" | "Movies" | "TV Shows" )
printf "Opening $2 playlist\n"
osascript -e "tell application \"iTunes\" to reveal playlist \"$2\""
;;
*)
# do nothing
;;
esac
else
printf "\033[1;31mThe iTunes directory is not a symlink!\033[0m\n"
fi
}
iTunesLibraryToggle "$1" "$2"
#!/usr/bin/env bash
iTunesLibraryToggle "$HOME/Music/iTunes (Movies & TV Shows)" Movies
#!/usr/bin/env bash
iTunesLibraryToggle "$HOME/Music/iTunes (Music & Audiobooks)" Music
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment