Applescript to turn on Bluetooth and run iSync

May 02, '06 07:30:00AM

Contributed by: Anonymous

Running iSync from an Applescript is almost trivial, but I wanted my computer to do the following:

  1. Turn on Bluetooth if necessary.
  2. Run iSync.
  3. Wait for iSync to finish, then quit it.
  4. Turn Bluetooth off if it was initially off.

I hacked together the following script from bits and pieces of things I found on on this site and elsewhere on the web. I don't really understand Applescript so if this is bad code, I appreciate any corrections.

-- Before you use this script, launch /Applications/AppleScript/AppleScript Utility.app and check "Enable GUI Scripting."
-- The Bluetooth menu item must be turned on in the Bluetooth system preferences pane.

-- Check the current bluetooth status and turn it on if necessary.
tell application "System Events" to tell the front menu bar of process "SystemUIServer"
	set menu_extras to value of attribute "AXDescription" of menu bar items
	repeat with x from 1 to the length of menu_extras
		if item x of menu_extras is "bluetooth menu extra" then exit repeat
	end repeat
	tell menu bar item x
		click
		tell 2nd menu item of front menu
			if name ends with "Off" then
				-- Current status is on; it says "turn bluetooth off"
				set last_bluetooth_setting to "on"
			else if name ends with "On" then
				-- Current status is off; it says "turn bluetooth on"
				set last_bluetooth_setting to "off"
				click
			end if
		end tell
		-- Unclick if it was on (do nothing but make the menu disappear)
		if last_bluetooth_setting is "on" then
			click
		end if
	end tell
end tell

-- Do the sync and wait for it to finish
tell application "iSync"
	synchronize
	repeat while (syncing is true)
		delay 5
	end repeat
	set syncStatus to sync status
	
	if syncStatus = 2 then
		-- Success
		quit
	else
		if syncStatus = 3 then
			set syncStatus to "completed with warnings"
		else if syncStatus = 4 then
			set syncStatus to "completed with errors"
		else if syncStatus = 5 then
			set syncStatus to "last sync cancelled"
		else if syncStatus = 6 then
			set syncStatus to "last sync failed to complete"
		else if syncStatus = 7 then
			set syncStatus to "never synced"
		end if
		display dialog "syncStatus: " & syncStatus
		syncStatus
	end if
end tell


-- Set the bluetooth status to what it was before.
tell application "System Events" to tell the front menu bar of process "SystemUIServer"
	set menu_extras to value of attribute "AXDescription" of menu bar items
	repeat with x from 1 to the length of menu_extras
		if item x of menu_extras is "bluetooth menu extra" then exit repeat
	end repeat
	tell menu bar item x
		click
		if last_bluetooth_setting is "off" then
			-- Turn it off again
			click 2nd menu item of front menu
		else
			-- Just close the menu
			click
		end if
	end tell
end tell


[kirkmc adds: I haven't tested this...

Update: A reader commented that this script doesn't work if the Bluetooth menu extra is not visible; this is noted at the beginning of the script. But he also pointed out that this script does not work on non-English language systems. So read the comments below for an improved version of the script that resolves both of these problems.]

Comments (20)


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