Search code examples
macosapplescriptitunes

Applescript to launch iTunes with a specific library


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.


Solution

  • 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.