Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppleScript to toggle Bluetooth

In OS X 10.8 I had a neat little AppleScript that I used to toggle Bluetooth quickly without using the mouse.

I updated to 10.9 which added several UI changes to System Preferences. Among other things it replaced the element that toggles Bluetooth from a checkbox to a button. My script is now broken, and consequently so is my workflow.

The problem is that the button's name changes from "Turn Bluetooth On" to "Turn Bluetooth Off" depending on its status. I don't have a sufficient grasp in AppleScript to figure out a workaround, and was wondering if you guys could help me out.

like image 358
user1892304 Avatar asked Dec 16 '22 04:12

user1892304


2 Answers

This worked for me in 10.9:

tell application "System Preferences"
    reveal pane "com.apple.preferences.Bluetooth"
end tell
tell application "System Events" to tell process "System Preferences"
    click button 6 of window 1
end tell
quit application "System Preferences"

You could also use blueutil:

/usr/local/bin/blueutil|grep -q 'Power: 1';/usr/local/bin/blueutil power $?
like image 76
Lri Avatar answered Dec 24 '22 15:12

Lri


tell application "System Preferences"
    reveal pane "com.apple.preferences.Bluetooth"
end tell
tell application "System Events" to tell process "System Preferences"
    repeat until exists window "Bluetooth"
    end repeat
    try
        click button "Turn Bluetooth Off" of window "Bluetooth"
        do shell script "networksetup -setairportpower airport off"
    on error
        click button "Turn Bluetooth On" of window "Bluetooth"
        do shell script "networksetup -setairportpower airport on"
    end try
end tell
tell application "System Preferences" to quit
like image 27
AndrewK Avatar answered Dec 24 '22 16:12

AndrewK