An AppleScript to manage two music folders

Jan 26, '05 09:18:00AM

Contributed by: designr

My iTunes music library has recently grown to 20 GB. This pretty much hosed the tiny 40 GB drive in my Powerbook. My solution was to move my ~/Music folder to an AFP server share named "MusicShare" (you could use a FireWire drive). I then renamed the "Music" folder on the server share to "ServerMusic." Finally, instead of a "Music" folder in my ~/home, I created a soft link named "Music" that pointed to the /Volumes/MusicShare/ServerMusic/.

But, I also wanted to carry 1 or 2 GB of MP3s with me when I was not at home. So I created a smaller "LocalMusic" folder in /Users/Shared/. I put it there so any logged-in user could play music from the same library. Then all I needed was a way to change the soft link "Music" in my ~/home directory to point to one or the other of the two "Music" folders.

So I wrote an Applescript that:

  1. Checks to see if iTunes is running;
  2. Gets the name of the current ~/home folder (so it works for all users);
  3. Determines if the server share (or Firewire drive) is mounted;
  4. Gets the correct path to the mountpoint (different for each logged-in user in the case of FUS); and
  5. Makes a new soft link in the current users' ~/home directory.
Read on for the script...

Enter this in Script Editor:

tell application "System Events"
  if not (exists application process "iTunes") then
    set short_name to do shell script ("id -un")
    tell application "Finder"
      if (exists folder "MusicShare:ServerMusic:") then
        set the_path to POSIX path of file "MusicShare:ServerMusic:"
        do shell script "ln -fhs '" & the_path & ¬
         "' '/Users/" & short_name & "/Music'"
      else
        do shell script ¬
         "ln -fhs '/Users/Shared/LocalMusic/' '/Users/" & ¬
         short_name & "/Music'"
      end if
    end tell
  end if
end tell
tell application "iTunes" to activate
Save the script as an Application called iTunes Script.app, paste an icon on it, and use the script in your Dock instead of iTunes. Don't forget to correct the permissions for both the script and the Music folders.

Now when I launch iTunes, the script checks to see if my server share is mounted. If so, iTunes opens my entire server music library. If not, iTunes opens my current favorites local music library. If iTunes is already running, it just brings iTunes to the foreground.

TIP: Plug your new Mac Mini into your home stereo system. Move your Music folder into your ~/Public folder and make a soft link back to ~/home. Then turn on Personal File Sharing. Now your home stereo and home office laptop can share the same music library.

Comments (16)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050123205848370