hohle
Automate iSync with AppleScript
by Jon on Saturday, July 22, 2006 file under: Technology
iSync

Its always bugged me that there wasn't a simple way to have iSync sync my phone on a regular interval. I don't want to manually sync the dumb thing, I thought it would be convenient if it was synched automatically.

Well, it turned out it was simple, a little sprinkling of AppleScript and I have a nice way to sync daily, without my intervention. After a little googling, my script got pretty fancy, here's a breakdown of what it does. I iSync isn't running, it will start iSync, then it starts the sync. When the sync is complete, if iSync wasn't running before the script started, it will quit iSync. If everything went accordingly and if Growl is installed, will notify me with a message saying when it synced and what the status of the sync was (if it failed or not, and why). If growl isn't installed, it will just pop up a message with the same info.

I was amazed at how easy it was to pull this together. This was the first AppleScript I had written and it met all the goals I was trying to meet.

Without further ado, here's the script:

-- This script will tell iSync to synchronize.  if there's more then one device
-- attached, I don't know what that means.
-- hints from http://growl.info/documentation/applescript-support.php and
-- http://www.macosxhints.com/article.php?story=20031201172150673

tell application "System Events"
	set growlIsRunning to (count of (every
	    process whose name is "GrowlHelperApp")) > 0
	
	set iSyncIsRunning to (count of (every
	    process whose name is "iSync")) > 0
end tell

if growlIsRunning then
	tell application "GrowlHelperApp"
		
		-- Make a list of all the notification types 
		-- that this script will ever send:
		set the allNotificationsList to {"Result Notification"}
		
		-- Make a list of the notifications 
		-- that will be enabled by default.      
		-- Those not enabled by default can be enabled later 
		-- in the 'Applications' tab of the growl prefpane.
		set the enabledNotificationsList to {"Result Notification"}
		
		register as application "iSyncScript"
		    all notifications allNotificationsList
		    default notifications enabledNotificationsList
		    icon of application "Script Editor"
	end tell
end if

tell application "iSync"
	activate
	synchronize
	-- wait until sync status != 1 (synchronizing)
	repeat while (syncing is true)
	end repeat
	
	set syncStatus to sync status
	set lastSync to last sync
end tell

set syncStatusText to ""

-- syncStatus = 2 -> successfully completed sync
if syncStatus = 2 then
	set syncStatusText to "Successfully Synced"
else
	if syncStatus = 3 then
		set syncStatusText to "Completed with Warnings"
	else if syncStatus = 4 then
		set syncStatusText to "Completed with Errors"
	else if syncStatus = 5 then
		set syncStatusText to "Last Sync Cancelled"
	else if syncStatus = 6 then
		set syncStatusText to "Last Sync Failed to Complete"
	else if syncStatus = 7 then
		set syncStatusText to "Never Synced"
	end if
	
end if

if syncStatus = 2 and not iSyncIsRunning then
	tell application "iSync" to quit
end if

set displayText to "Status: " & syncStatusText &
    " (" & syncStatus & ").  Synced on " & lastSync

if growlIsRunning then
	tell application "GrowlHelperApp"
		notify with name "Result Notification" title "iSyncScript"
		    description displayText application name "iSyncScript"
		    icon of application "iSync"
	end tell
else
	display dialog "syncStatus: " & syncStatus
end if

return syncStatus

I threw this in my ~/Library/Scripts directory, and can call it from the the command line or cron with `osascript Library/Scripts/iSync.scpt`.

Permanent link to Automate iSync with AppleScript

hohle.net | hohle.org | hohle.name | hohle.co.uk | hohle.de | hohle.info