Toggle Bluetooth with Applescript

I’ve just written a little Applescript to toggle the Bluetooth power on my MacBook on or off. It doesn’t take any parameters or anything, it literally just toggles it off if it’s already on, or on if it’s already off. The script looks like this:

#! /usr/bin/osascript
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# *  Author:	Haydn Williams
# *  URL:	http://www.haydnwilliams.com
# *  Date:	March 2008
# *  Script:	toggleBluetooth.scpt
# *  Purpose:	Toggle bluetooth status
# *  Useage:	toggleBluetooth.scpt
#  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
tell application "System Preferences"
	activate
	set current pane to pane "com.apple.preferences.Bluetooth"
	try
		tell application "System Events"
			tell process "System Preferences"
				click checkbox "On" of window "Bluetooth"
			end tell
		end tell
	on error
		display dialog "Failed to toggle bluetooth."
	end try
	tell application "System Preferences" to close the first window
end tell


You can download the compiled script from here. Please note that you’ll need to ‘Enable GUI Scripting‘ in the AppleScript Utility (you can find it in the AppleScript folder of Applications). To run the script, just double-click on it. Please note that this will probably only work on English language systems, but should be easy enough to alter.