Previous 1 2 Next 27 Replies Latest reply: Mar 17, 2015 11:55 AM by Hiroto
BioRich Level 1 Level 1 (65 points)

Hi folks.

 

Just looking for a way to toggle Bluetooth on and off using a single AppleScript.  Any takers?  I've tried some of the scripts posted here, but they were posted in 2006 or so, and all don't work...that I have found. 

 

Cheers


Mac Pro, Mac OS X (10.6.7), Hypercard UG!
  • Pierre L. Level 5 Level 5 (4,115 points)

    tell application "System Preferences"

        reveal pane id "com.apple.preferences.Bluetooth"

        -- activate -- optional, just if you want to display the window

        tell application "System Events" to tell process "System Preferences"

            click checkbox "On" of window 1

        end tell

        quit

    end tell


     

    Message was edited by: Pierre L. (“activate” commented out and “quit” added)

  • BioRich Level 1 Level 1 (65 points)

    Works like a charm.  Thank you!

  • Pierre L. Level 5 Level 5 (4,115 points)

    My pleasure.

  • spazek Level 2 Level 2 (215 points)

    command-line utility blueutil

     

     

    -- put "blueutil" in ressources folder of this script saved as Script-Bundle or Application

    set blueutilpath to quoted form of (POSIX path of (path to resource "blueutil"))

     

    -- or start with this line if "blueutil" installed:

    -- set blueutilpath to "usr/local/bin/blueutil"

     

    set sb to last word of (do shell script blueutilpath & " status")

    if sb is "on" then

      do shell script blueutilpath & " off"

    else

      do shell script blueutilpath & " on"

    end if

     

     

    Spazek

  • willthefirst Level 1 Level 1 (0 points)

    Can anyone offer an update to this for Mavericks? The System Preferences interface is different so this no longer works.

  • Grom Level 1 Level 1 (0 points)

    Thanks for the script. Here are some adjustments for Mountain Lion:

     

     

    tell application "System Preferences"

              reveal pane id "com.apple.preferences.Bluetooth"

      --activate -- optional, just if you want to display the window

              tell application "System Events" to tell process "System Preferences"

                   click checkbox 2 of window 1

                   -- checkbox 1: show in menu bar

                   -- checkbox 2: enabled

                   -- checkbox 3: detectable

              end tell

      quit

    end tell

  • Pierre L. Level 5 Level 5 (4,115 points)

    The following script seems to work flawlessly under OS X Mavericks:

     

    tell application "System Preferences"

        -- activate

        set the current pane to pane id "com.apple.preferences.Bluetooth"

        tell application "System Events" to tell process "System Preferences"

            click button 6 of window "Bluetooth"

        end tell

        quit

    end tell

     

    Message was edited by: Pierre L. (“activate” commented out)

  • willthefirst Level 1 Level 1 (0 points)

    Thanks, that almost works. The only problem is that it will turn bluetooth back on if it is disabled. How would you insert a 'check' that says: 'if it's on, turn it off, but if it's off, leave it off'?

  • Pierre L. Level 5 Level 5 (4,115 points)

    willthefirst wrote:

     

    Thanks, that almost works. The only problem is that it will turn bluetooth back on if it is disabled. How would you insert a 'check' that says: 'if it's on, turn it off, but if it's off, leave it off'?

     

    You are absolutely right when you say that the script “will turn Bluetooth back on if it is disabled”. But you are absolutely wrong when you say that it almost works. Actually, the script works perfectly well, since it does exactly what the OP asked for (“Just looking for a way to toggle Bluetooth on and off using a single AppleScript.”) and you were just asking for “an update to this for Mavericks”.

  • Pierre L. Level 5 Level 5 (4,115 points)

    Here's the script you are asking for:

     

    tell application "System Preferences"

        set the current pane to pane id "com.apple.preferences.Bluetooth"

        tell application "System Events"

            tell process "System Preferences"

                tell window "Bluetooth"

                    tell button 6

                        if name is "Turn Bluetooth Off" then click

                    end tell

                end tell

            end tell

        end tell

        quit

    end tell

  • willthefirst Level 1 Level 1 (0 points)

    Merci Pierre!

  • Pierre L. Level 5 Level 5 (4,115 points)

    You're welcome.

  • jpkotor Level 1 Level 1 (0 points)

    I get an error that says "AppleScript Error - System Events got an error: AppleScript Editor is not allowed assistive access."

     

    How can I fix this?

  • Pierre L. Level 5 Level 5 (4,115 points)
Previous 1 2 Next