Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Applescript to turn on Bluetooth and run iSync Apps
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.]
    •    
  • Currently 3.17 / 5
  You rated: 1 / 5 (6 votes cast)
 
[28,979 views]  

Applescript to turn on Bluetooth and run iSync | 20 comments | Create New Account
Click here to return to the 'Applescript to turn on Bluetooth and run iSync' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Applescript to turn on Bluetooth and run iSync
Authored by: inemo on May 02, '06 08:02:45AM

If anyone has trouble selecting it, click at the top of the top line, then hold shift and click at the end of the last line. That will select it all.

---
http://caius.name/

I'm just a mac baby :)



[ Reply to This | # ]
Applescript to turn on Bluetooth and run iSync
Authored by: JaxMyers on May 02, '06 08:03:41AM

Awesome. I've been using a script that I believe was posted on here that runs iSync ever since apple killed the iSync menu bar item for anything other than .mac. The problem was a lot of the time I forgot to turn bluetooth on. thanks!



[ Reply to This | # ]
Applescript to turn on Bluetooth and run iSync
Authored by: vgz on May 02, '06 08:59:51AM

Thanks, I've had a simple little script setup to run iSync every night for a while now, but I never found the time to make it quit iSync when it was finished. This takes care of that. ^_~

---
VGZ:
Otaku, Fanfic author, and Mac Fanatic



[ Reply to This | # ]
Applescript to turn on Bluetooth and run iSync
Authored by: triplef on May 02, '06 09:21:16AM

Is there any other way to turn Bluetooth on/off using AppleScript or the command line than scripting the Menu Extra? My BT Menu Extra tends to disappear.

Otherwise very nice script, except the missing error handling in case of a missing Bluetooth menu extra. :)



[ Reply to This | # ]
missing Bluetooth menu extra
Authored by: Hal Itosis on May 02, '06 03:14:27PM
triplef:
> My BT Menu Extra tends to disappear.

You can stop Bluetooth (and other extras) from disappearing,
by installing Unsanity's Menu Extra Enabler (ApE not required).

-HI-

[ Reply to This | # ]
Applescript to turn on Bluetooth and run iSync
Authored by: ever on May 02, '06 11:40:37AM
Cool! I recognize my bluetooth code from this script. Glad it found a new use. ;)

[ Reply to This | # ]
Applescript to turn on Bluetooth and run iSync
Authored by: svenbox on May 02, '06 11:53:55AM

It should be made clear that this hint only works for english/american systems - otherwise the script has to be edited so that the script looks for words in other languages in the Bluetooth menu.

Also some kind of error handling in case that the device to be synched has bluetooth turned off or on would be great.



[ Reply to This | # ]
Applescript to turn on Bluetooth and run iSync
Authored by: zionthelion73 on May 03, '06 03:28:55AM

I have an italian Tiger, I use "Attiva" instead of "ON" and "Disattiva" instead of "OFF"

You can activate the BlueTooth menu opening the following file

/System/Library/CoreServices/Menu Extras/Bluetooth.menu/
but I cannot do it via Applescript!

[ Reply to This | # ]
Applescript to turn on Bluetooth and run iSync
Authored by: markformac on May 03, '06 07:57:33AM

If after the script does its job and turns Bluetooth on after pluggin my laptop in, and then I manually turn it back off, is it suppose to stay off?

I did it as a test and it hasn't turned back on at all. I know the script works fun because I used the 'Run' option before saving it.

---
Mark Brooks



[ Reply to This | # ]
Multilingual version without using menu extra
Authored by: triplef on May 06, '06 04:41:49AM
After some poking around I've written a small command-line utility which uses private APIs in the IOBluetooth framework to switch Bluetooth on and off.
Download the utility and use the following modified script to sync like this article explains:

property blueutilPath : "/usr/local/bin/blueutil"


-- Check the current bluetooth status and turn it on if necessary.
set btWasOn to true
if execBlueutil("status") ends with "off" then
	set btWasOn to false
	execBlueutil("on")
end if

-- Do the sync and wait for it to finish
tell application "iSync"
	if not (synchronize) then
		display dialog "Failed to start sync"
	else
		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 "Sync completed: " & syncStatus
		end if
	end if
end tell


-- Set the bluetooth status to what it was before.
if not btWasOn then execBlueutil("off")


on execBlueutil(command)
	set res to do shell script blueutilPath & " " & command
	if res contains "Error" then
		display dialog res
		quit
	end if
	return res
end execBlueutil


[ Reply to This | # ]
Corrected link
Authored by: SeanAhern on Jun 02, '06 02:41:52AM
I think your link is borked. The correct link is [link:]http://www.frederikseiffert.de/blueutil/

[ Reply to This | # ]
Applescript to turn on Bluetooth and run iSync
Authored by: loekjehe on May 06, '06 04:44:34AM

I discovered some error in the AS code: the variable last_bluetooth_setting is not defined at the beginning of the script. So, please add a line: set last_bluetooth_setting to "off" at the beginning of the script and everything runs fine afterwards.

Loek



[ Reply to This | # ]
Applescript to turn on Bluetooth and run iSync
Authored by: jacobwegner on Aug 17, '07 10:07:00AM

Thanks for these tips–big help!



[ Reply to This | # ]
Workaround for Leopard
Authored by: henebry on Nov 24, '07 10:36:47AM

The "improved" version of this script (the one designed for non-English systems) doesn't function at all in Leopard.

And the original version has trouble identifying the bluetooth menu. I suspect that the name of the bluetooth menu has simply changed from "bluetooth menu extra" to something else. In any event, the script winds up opening whichever menu extra is furthest to the right (the menu extra just to the left of the Spotlight menu, which for some reason is treated by the Mac OS as different in kind from the other menu extras).

Until someone with better skills than I figures out how to fix the script, I've figured out a workaround: move the Bluetooth menu to the rightmost position, just left of Spotlight. You can change the order of menu extras by by holding down Command (apple) while dragging.



[ Reply to This | # ]
Workaround for Leopard—whoops!
Authored by: henebry on Nov 24, '07 10:54:42AM

I was wrong about the "improved" version of the script. It DOES work in Leopard, you simply have to install the utility linked in that post before trying to run the script.

Sorry for my confusion.

The workaround I posted for the original version is still relevant, but not really vital, given the superior design of the "improved" version.



[ Reply to This | # ]
Workaround for Leopard
Authored by: KuroRai on Apr 30, '08 10:08:59PM

you are actually correct about the menu being renamed. A still trial and error has revealed to me that the menu is now called "Bluetooth" no "menu extra" and a capital (may or may not make a difference, the capital).

Nonetheless there are some other bugs that still don't get it to work, It's already 1:07 AM, so I'll try tomorrow to get it working.

Thanks for the start of the script by the way! Total script n00b here. XD.



[ Reply to This | # ]
Workaround for Leopard
Authored by: KuroRai on Apr 30, '08 10:13:01PM

-- 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" then exit repeat
end repeat
tell menu bar item x
click
tell 2nd menu item of front menu
if name ends with "Off" then
-- If option allows for turning off; then turn off by clicking
set last_bluetooth_setting to "off"
click
end if
end tell
end tell
end tell

That's what I have. I don't need it to turn on, but rather off, so my script is different. Please note that the change is that the button says the action (if it's on it says "turn off" and vice versa) as such the code needs to be changed, modify and add to your needs! didn't think it would be this fast. XD.



[ Reply to This | # ]
Workaround for Leopard
Authored by: KuroRai on Apr 30, '08 10:19:01PM

Yet a final comment. Thanks to OS X: Leopard's new "record" ability in Automator, you can record yourself clicking on something, stop recording, and find out what the menu is called right then and there! Found that out now. XD.



[ Reply to This | # ]
Workaround for Leopard
Authored by: ccjensen on Nov 26, '08 12:59:31PM
figured I would throw in my solution which was based upon the original post.
on run argv
if length of argv is not 1 then
return
end if

set switch to item 1 of argv as string

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" then exit repeat
end repeat
tell menu bar item x
click
tell 2nd menu item of front menu
if name ends with switch then
set clicked to true
click
else
set clicked to false
end if
end tell
if not clicked then
click
end if
end tell
end tell
end run
This code takes a single argument: the desired action (ie On or Off)

It would be run from the terminal:
osascript /path/to/script/name_of_script_file.scpt On
osascript /path/to/script/name_of_script_file.scpt Off


[ Reply to This | # ]
sync status error value
Authored by: grav on Oct 08, '08 06:58:08AM

I tried running the script with my phone turned off, as to get iSync to fail, but the application quit anyways.

Seems that sync status equals 2 even when iSync couldn't connect to the phone, so if the sync fails this way, one might not notice it.

OS X 10.5.5



[ Reply to This | # ]