I would like to write an AppleScript that would allow me to launch iTunes with a given Library instead of having to hold down the Option key and browsing for one. I'm already aware of Doug's Library manager, which is not quite what I want. The AppleScript would be for a specific library.
2 Answers
iTunes doesn't allow you to do this with AppleScript, but you can write directly into iTunes' preferences, where it stores a bookmark (alias) to the currently selected library (or nothing, if you're using a library in the default location).
First, you'll need to obtain the alias data for your selected library location. Open iTunes holding down the Option key, select your library and quit iTunes. Then, in Terminal, run:
defaults read com.apple.itunes 'book:1:iTunes Library Location' | pbcopy
This will copy the library alias data to the clipboard.
Finally, here's the script:
property otherLibraryLocation : "" -- paste location between the quotes
property libraryLocationPref : "com.apple.iTunes 'book:1:iTunes Library Location'"
-- first, 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
-- then, set the location
do shell script "defaults write " & libraryLocationPref & " " & quoted form of otherLibraryLocation
-- uncomment the following line to use the default iTunes library instead
-- do shell script "defaults delete " & libraryLocationPref
-- finally, relaunch iTunes
tell application "iTunes" to activate
Paste the library location between the quotes in the first line of the script, and you should be all set. To return to the original library, uncomment the line including defaults delete
.

- 43,532
- 6
- 101
- 124
-
Nice! I imagine it will eventually break because aliases are deprecated, but in the meantime... – Nicholas Riley Aug 06 '13 at 00:28
-
1For me (macOS 10.13.2, iTunes 12.7.4.76), this default recently changed from `'alis:1:iTunes Library Location'` to `book:1:iTunes Library Location` - not sure when this changed but give it a try if the above solution doesn't work for you anymore. – Jonas W Apr 15 '18 at 10:09
-
That makes sense given aliases were replaced by bookmarks. – Nicholas Riley Apr 16 '18 at 14:33
-
Does no longer work for me with iTunes 12.8 even with the changed property key (book...). – Augunrik Oct 14 '18 at 05:41
-
Works in iTunes 12.9 / macOS 10.14 with the 'book' location. (I just updated my answer in place as it's been long enough!) – Nicholas Riley Oct 14 '18 at 11:39
You can create a symlink from ~/Music/iTunes to your chosen directory path in a unix shell script (man ln). And an AppleScript can call a unix shell script by sending the appropriate message to the Terminal app.

- 70,107
- 14
- 90
- 153