BioRich

Q: AppleScript Bluetooth on/off

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!

Posted on Jul 15, 2011 9:04 AM

Close

Q: AppleScript Bluetooth on/off

  • All replies
  • Helpful answers

Page 1 Next
  • by Pierre L.,Solvedanswer

    Pierre L. Pierre L. Jul 15, 2011 9:31 AM in response to BioRich
    Level 5 (4,484 points)
    Jul 15, 2011 9:31 AM in response to BioRich

    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)

  • by BioRich,

    BioRich BioRich Jul 15, 2011 10:25 AM in response to Pierre L.
    Level 1 (72 points)
    Servers Enterprise
    Jul 15, 2011 10:25 AM in response to Pierre L.

    Works like a charm.  Thank you!

  • by Pierre L.,

    Pierre L. Pierre L. Jul 15, 2011 11:10 AM in response to BioRich
    Level 5 (4,484 points)
    Jul 15, 2011 11:10 AM in response to BioRich

    My pleasure.

  • by spazek,

    spazek spazek Jul 15, 2011 4:06 PM in response to BioRich
    Level 2 (215 points)
    Jul 15, 2011 4:06 PM in response to BioRich

    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

  • by willthefirst,

    willthefirst willthefirst Oct 24, 2013 1:45 PM in response to BioRich
    Level 1 (0 points)
    Oct 24, 2013 1:45 PM in response to BioRich

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

  • by Grom,

    Grom Grom Nov 4, 2013 2:52 AM in response to BioRich
    Level 1 (0 points)
    Nov 4, 2013 2:52 AM in response to BioRich

    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

  • by Pierre L.,

    Pierre L. Pierre L. Nov 4, 2013 8:08 AM in response to willthefirst
    Level 5 (4,484 points)
    Nov 4, 2013 8:08 AM in response to willthefirst

    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)

  • by willthefirst,

    willthefirst willthefirst Nov 4, 2013 3:00 PM in response to Pierre L.
    Level 1 (0 points)
    Nov 4, 2013 3:00 PM in response to Pierre L.

    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'?

  • by Pierre L.,

    Pierre L. Pierre L. Nov 4, 2013 4:22 PM in response to willthefirst
    Level 5 (4,484 points)
    Nov 4, 2013 4:22 PM in response to willthefirst

    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”.

  • by Pierre L.,

    Pierre L. Pierre L. Nov 4, 2013 5:44 PM in response to willthefirst
    Level 5 (4,484 points)
    Nov 4, 2013 5:44 PM in response to willthefirst

    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

  • by willthefirst,

    willthefirst willthefirst Nov 5, 2013 11:05 AM in response to Pierre L.
    Level 1 (0 points)
    Nov 5, 2013 11:05 AM in response to Pierre L.

    Merci Pierre!

  • by Pierre L.,

    Pierre L. Pierre L. Nov 5, 2013 3:05 PM in response to willthefirst
    Level 5 (4,484 points)
    Nov 5, 2013 3:05 PM in response to willthefirst

    You're welcome.

  • by jpkotor,

    jpkotor jpkotor Dec 8, 2013 12:46 AM in response to Pierre L.
    Level 1 (0 points)
    Dec 8, 2013 12:46 AM in response to Pierre L.

    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?

  • by Pierre L.,

    Pierre L. Pierre L. Dec 8, 2013 2:05 AM in response to jpkotor
    Level 5 (4,484 points)
    Dec 8, 2013 2:05 AM in response to jpkotor
Page 1 Next