Automatically sync when connecting an iPod

Oct 19, '10 07:30:00AM

Contributed by: david-bo

In older versions of iTunes you had the option to perform a sync, similar to clicking the Sync button in iSync in Mac OS X 10.5 and newer. For me this was a perfect solution to remember syncing my phones since I connected my iPod once a day to update some smart playlists. To mimic this functionlity I wrote an AppleScript and then attached it as a folder action to the /Volumes folder.

Here's the script:

global bluetoothActive
property blueutilPath : "$HOME/bin/blueutil"

on adding folder items to this_folder after receiving added_items
  set podName to "the name of your iPod"
  if podName is in (added_items as string) then
    if "on" is in (do shell script blueutilPath & " status") then
      set bluetoothActive to true
    else
      set bluetoothActive to false
      do shell script blueutilPath & " on"
    end if
    try
      tell application "Finder"
        if exists POSIX file "/tmp/sync" then
          set newAddresses to (do shell script "find $HOME'/Library/Application Support/AddressBook/Metadata' -newer /tmp/sync")
          set newEvent to (do shell script "find $HOME/Library/Calendars -newer /tmp/sync")
          if "ics" is in newEvent then
            display dialog "new event " & newEvent giving up after 5
            my sync()
          else if "abcd" is in newAddresses then
            display dialog "new contact: " & newAddresses giving up after 5
            my sync()
          end if
        else
          my sync()
        end if
      end tell
    on error errStr number errorNumber
      display dialog "find failed " & errStr & errorNumber giving up after 5
      my sync()
      
    end try
    if not bluetoothActive then
      do shell script blueutilPath & " off"
    end if
  end if
end adding folder items to

on sync()
  tell application "iSync"
    try
      synchronize
    on error
      display dialog "Sync failed"
    end try
    repeat while (syncing)
    end repeat
    if (sync status is 2 or 3) then
      do shell script "touch /tmp/sync"
    else
      do shell script "rm /tmp/sync"
    end if
  end tell
end sync
Some remarks: Now I just wonder if any of the Hints readers could tell me how do this over the network (my current setup is to sync my iPod on one machine and have my Address Book/iCal on another machine). I am aware of ssh machine osascript -e path/to/script but for academic purposes I want to use remote Apple Events.

[crarko adds: I haven't tested this one. The script compiles without error and I've mirrored it here.]

Comments (7)


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