Skip to content

Instantly share code, notes, and snippets.

@battis battis/iTunesLibraryToggle
Last active Aug 21, 2018

What would you like to do?
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
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
printf "\n"
# reset iTunes database location...
printf "Nuking iTunes defaults from orbit\n"
defaults delete '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"
printf "\033[1;32m$1 is already linked as the iTunes Library\033[0m\n"
# re-open iTunes with new library location
printf "Opening iTunes\n"
open "/Applications/"
# 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
printf "\033[1;31mThe iTunes directory is not a symlink!\033[0m\n"
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
You can’t perform that action at this time.