Wednesday, May 17, 2006

Copying tracks to remote iTunes


We really enjoy having the Mac Mini in our entertainment center to play music and videos with FrontRow.  There was is one problem with our setup.  I only purchase, or rip music on my laptop.  When I want to add music to the Mac Mini's iTunes, I need to mount the laptop's drive on the mac mini, and then manually add the tracks to iTunes, with VNC or with the bluetooth keyboard


I decided to write an applescript to automate this.  So with the applescript posted below, I am able to sit on my laptop, and "send" tracks to the Mac Mini.


Here are the steps that are needed for setup:




  1. Create a playlist in iTunes that contains all of the tracks that have already been copied to the other computer. (Or the ones you don't want copied over)

  2. Create a smart playlist that contains all of the tracks that are not in the above playlist.  (I also don't include any track with "Podcast" as the genre, as the remote mac can manage downloading podcasts)

  3. On the remote mac mount the "local" mac's hard drive, and create a alias to the drive.  (You should also store the password needed to mount the drive in the keychain)

  4. Modify the applescript below to include the names of the playlists created on the "local" mac, and the path to the alias created in step 3.

  5. On the remote mac, in the Sharing Preference Panel, enable "Remote Apple Events"

  6. Make sure that in the Advanced tab of the remote iTunes, that "Copy files to iTunes Music folder when adding to library" is checked


Then when you run the script, it will ask you to select iTunes and Finder on the remote mac.  (Make sure that iTunes is already running.)


This is what the script will do:


  1. Tell the remote mac to mount the local mac's drive

  2. Gather the path to all of the tracks in the smart playlist.

  3. Tell the remote iTunes to add the tracks to it's library

  4. Tell the local iTunes to move the tracks to the "already copied playlist"




set remoteiTunes to ""

property remoteAlias : "Media Mini:Users:media:Desktop:Paul's Laptop drive"

-- name of the playlist
property toBeTransferredPlaylist : "Tracks for Mac Mini"
property transferredPlaylist : "Added to Mac Mini"

set remoteiTunes to choose remote application with title "Choose iTunes on the remote Mac"

set remoteFinder to choose remote application with title "Choose Finder on the remote Mac"

tell application "iTunes"
    set mySource to container of view of front window
    set transfer_playlist_id to playlist toBeTransferredPlaylist of mySource
    
    set trackList to {}
    if duration of transfer_playlist_id is greater than 0 then
        set trackList to location of every file track of transfer_playlist_id
    end if
end tell

-- Now mount this computers drive on the remote mac
tell remoteFinder
    open remoteAlias
end tell


-- Now add the specified tracks to the remote iTunes
repeat with localFile in trackList
    set stringPath to localFile as string
    
    tell remoteiTunes
        open stringPath as alias
    end tell
end repeat

-- now add the tracks to the "already copied" playlist
tell application "iTunes"
    -- get the playlist
    set new_playlist to user playlist transferredPlaylist
    set trackList to (file tracks of transfer_playlist_id)
    set trackCount to count of trackList
    repeat with i from 1 to trackCount
        set a_track to item i of trackList
        duplicate a_track to new_playlist
        
    end repeat
end tell


Technorati Tags: , , ,

2 comments:

  1. Now I can listen to music we recently added and have a dance party. Without going to mac mini and weeping as I get my hopes up, then remembering the tunes were on the computer.... thank you for this.

    ReplyDelete
  2. This is an awesome script. Exactly what I needed before making the same setup.
    Although it worked at first, but now I'm getting the following error message: "Can't make some data into the expected type."
    I've tried with different mp3 files but I always get the same message. Just wondering if you had any experience with that.

    ReplyDelete

Empowering Family Legacy: How I Transitioned to Self-Hosting with Gramps Web

For several years now, I've been maintaining a genealogy website containing information from both my and my wife's family history. O...