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

Toggle Bluetooth using Quicksilver and AppleScript Network
I was looking for a way to reduce the amount of clutter in my menu bar and decided the Bluetooth icon was a suitable target. I wanted two things:
  1. The current Bluetooth status displayed on my Desktop
  2. An easy way to toggle Bluetooth on and off
An older hint suggested using blueutil for controlling the Bluetooth daemon in OS X. This nifty little program can both show and set the status of the Bluetooth radio receiver. To display the current Bluetooth status on the Desktop, I use GeekTool with the following shell command:
/usr/local/bin/blueutil status | awk '{print "BT: " $2}'
To toggle Bluetooth on and off, and show a Growl notification, I created this AppleScript. This script is then placed in a directory that is scanned by Quicksilver; I used ~/Library/Scripts. It can then be quickly called with a few keystrokes.

[robg adds: This hint doesn't require Quicksilver; you could use another launcher, or even just keep the script in your dock, toolbar, or sidebar.]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[23,418 views]  

Toggle Bluetooth using Quicksilver and AppleScript | 7 comments | Create New Account
Click here to return to the 'Toggle Bluetooth using Quicksilver and AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Toggle Bluetooth using Quicksilver and AppleScript
Authored by: chally on Mar 30, '07 12:00:03PM
I've been doing it this way:
on TurnOnBluetooth()
	-- 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 menuExtras to value of attribute "AXDescription" of menu bar items
		repeat with mExtras from 1 to the length of menuExtras
			if item mExtras of menuExtras is "bluetooth menu extra" then exit repeat
		end repeat
		tell menu bar item mExtras
			click
			tell 2nd menu item of front menu
				if name ends with "Off" then
					-- Current status is on; it says "turn bluetooth off"
					set prevBluetoothState to "on"
				else if name ends with "On" then
					-- Current status is off; it says "turn bluetooth on"
					set prevBluetoothState to "off"
					click
				end if
			end tell
			-- Unclick if it was on (do nothing but make the menu disappear)
			if prevBluetoothState is "on" then
				click
			end if
		end tell
	end tell
	return prevBluetoothState
end TurnOnBluetooth


[ Reply to This | # ]
Toggle Bluetooth using Quicksilver and AppleScript
Authored by: berdan on Mar 30, '07 12:29:58PM

If I'm not mistaken, that script requires the BT icon to still be in the menu bar, right?



[ Reply to This | # ]
Toggle Bluetooth using Quicksilver and AppleScript
Authored by: chally on Mar 30, '07 01:38:59PM
This script does, but you could use applescript to change it in the Bluetooth system preference. There are similar scripts posted, including one that turns the clock on and off. This is what I use for that, if it gives you an idea where to start:
-- This script tells the "Date & Time" preference pane to do the following:
--
-- if the clock is not showing in any way
-- or the clock is showing in the menu bar, then
--  Show the clock in the window the way I like it
-- else if the clock is showing in the window then
--  Show the clock in the menu bar the way I like it

tell application "System Preferences"
	activate
	set the current pane to pane "Date & Time"
	delay 1
end tell

tell application "System Events"
	tell process "System Preferences"
		-- define stuff
		set tabGroup to the first tab group of the window "Date & Time"
		set mainGroup to the first group of tabGroup
		set viewInRadioGroup to the first radio group of mainGroup
		set viewAsRadioGroup to the second radio group of mainGroup
		set showClockCheckbox to the checkbox 
			"Show the date and time" of mainGroup
		set showSecondsCheckbox to the checkbox 
			"Display the time with seconds" of mainGroup
		set showAmPmCheckbox to the checkbox 
			"Show AM/PM" of mainGroup
		set showDOWCheckbox to the checkbox 
			"Show the day of the week" of mainGroup
		set flashColonsCheckbox to the checkbox 
			"Flash the time separators" of mainGroup
		set twentyFourHourCheckbox to the checkbox 
			"Use a 24-hour clock" of mainGroup
		--do stuff
		if the value of showClockCheckbox is 0 then
			click showClockCheckbox
			set clockWasShowing to false
		else
			set clockWasShowing to true
		end if
		if the value of radio button "Window" of viewInRadioGroup is 0 
			or not clockWasShowing then
			click radio button "Clock" of tabGroup
			if the value of showClockCheckbox is 0 then 
				click showClockCheckbox
			click radio button "Window" of viewInRadioGroup
			click radio button "Analog" of viewAsRadioGroup
			if the value of showSecondsCheckbox is 0 then 
				click showSecondsCheckbox
		else
			click radio button "Clock" of tabGroup
			if the value of showClockCheckbox is 0 then 
				click showClockCheckbox
			click radio button "Menu Bar" of viewInRadioGroup
			click radio button "Digital" of viewAsRadioGroup
			if the value of showSecondsCheckbox is 1 then 
				click showSecondsCheckbox
			if the value of showAmPmCheckbox is 0 then 
				click showAmPmCheckbox
			if the value of showDOWCheckbox is 0 then 
				click showDOWCheckbox
			if the value of flashColonsCheckbox is 1 then 
				click flashColonsCheckbox
			if the value of twentyFourHourCheckbox is 1 then 
				click twentyFourHourCheckbox
		end if
		set visible to false
	end tell
end tell


[ Reply to This | # ]
Toggle Bluetooth using Quicksilver and AppleScript
Authored by: MagicMike on Mar 30, '07 01:14:07PM

Sorry, I cannot use the script part for Growl. Maybe it is because of German system. Can you save it as a script ?



[ Reply to This | # ]
Toggle Bluetooth using Quicksilver and AppleScript
Authored by: chally on Mar 30, '07 01:52:41PM

I neglected to add that the script I provided also only toggles Bluetooth between on and off. It doesn't effect any indication of Bluetooth's state (except on the menu, which you don't want--I should have read your hint closer about that.)

Nevertheless, I'm sure there are ways to show Bluetooth's status with only Applescript, depending on what kind of feedback you want. I don't have Quicksilver (and that's why I provided an Applescript-only fragment, which is actually only a handler portion of a script I wrote to set up Bluetooth internet for my phone and ping a server to keep me from getting dropped, then undo it all when I want to go offline), so I haven't tried your hint. I'm sorry if my comments were misleading or inapplicable.



[ Reply to This | # ]
Toggle Bluetooth using Quicksilver and AppleScript
Authored by: axonic labs on Apr 04, '07 04:13:20PM
i have always wanted something like this as well since i got my powerbook a couple of years ago. i actually wrote an application to toggle bluetooth and i just recently made it available on the web. i have been using it myself for a while now and you can get it at http://www.axoniclabs.com/Bluetoggle/

enjoy :)

[ Reply to This | # ]
Toggle Bluetooth using Quicksilver and AppleScript
Authored by: Viewer on Dec 06, '08 01:16:23PM

Excellent script. It should be noted that I had to to install blueutil I had to create the directory /usr/local/bin manually before running the install script, which wasn't noted in the documentation (OS 10.4.11).



[ Reply to This | # ]