lolofo777

Q: Turn on bluetooth with applescript !

Hi,

I don't know how to use Applescript, but i need to do a script that can turn on the bluetooth on my MCP (OS X 10.11.1). I can do a copy paste for the script ! Thanks a lot for the help !

MacBook Pro with Retina display, OS X El Capitan (10.11.1)

Posted on Jan 1, 2016 1:14 PM

Close

Q: Turn on bluetooth with applescript !

  • All replies
  • Helpful answers

  • by Roote,Solvedanswer

    Roote Roote Jan 2, 2016 12:29 AM in response to lolofo777
    Level 2 (417 points)
    Jan 2, 2016 12:29 AM in response to lolofo777

    Hi lolofo777. One option is to download and install Frederik Seiffert's command-line utility blueutil to run from Terminal or use in an AppleScript. Blueutil installs to /usr/local/bin/blueutil. In Terminal you can use one of the following three commands to check the status of Bluetooth to see if it is on or off, turn Bluetooth on, or turn Bluetooth off:

     

    blueutil status

    blueutil on

    blueutil off

    blueutilTerminal.png

    In an AppleScript you can use one of the following three commands to do the same:

     

    do shell script "/usr/local/bin/blueutil status"

    do shell script "/usr/local/bin/blueutil on"

    do shell script "/usr/local/bin/blueutil off"

     

    If you want an AppleScript to toggle between Bluetooth states on and off you can use:

     

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

     

    do shell script blueutil & " status"

    set statusResult to the result

     

    if statusResult is "Status: on" then

    do shell script blueutil & " off"

    else if statusResult is "Status: off" then

    do shell script blueutil & " on"

    end if

    blueutilApplescript.png

    You can download the above Applescript from this Dropbox link: blueutil.scpt. Tested with OS X El Capitan 10.11.2 and OS X Yosemite 10.10.5.

  • by Hiroto,

    Hiroto Hiroto Jan 2, 2016 1:49 AM in response to lolofo777
    Level 5 (7,281 points)
    Jan 2, 2016 1:49 AM in response to lolofo777

    Hello

     

    You might try the following pyobjc script to get and set bluetooth power state. It is indeed a pyobjc version of my rubycocoa script posted in the following thread.

     

    AppleScript Bluetooth on/off

    https://discussions.apple.com/thread/3179367

     

    Briefly tested with pyobjc 2.2b3 and python 2.6.1 under OS X 10.6.8.

     

     

    #!/usr/bin/python
    # coding: utf-8
    # 
    #   file:
    #       bluetooth_powerstate.py
    # 
    #   function:
    #       get or set bluetooth power state
    # 
    #   usaeg:
    #       ./bluetooth_powerstate [mode]
    #           mode :
    #               0  = off
    #               1  = on
    #               -1 = toggle
    #                  = query (print current state)
    #   version:
    #       0.10
    # 
    #   written by Hiroto, 2016-01
    #               
    import sys, objc
    import time
    from CoreFoundation import *
    
    IOBT_BRIDGESUPPORT = '''<?xml version="1.0" standalone="yes"?>
    <!DOCTYPE signatures SYSTEM "file://localhost/System/Library/DTDs/BridgeSupport.dtd">
    <signatures version="0.9">
        <function name="IOBluetoothPreferenceGetControllerPowerState">
            <retval type="i"></retval>
        </function>
        <function name="IOBluetoothPreferenceSetControllerPowerState">
            <arg type="i"></arg>
            <retval type="i"></retval>
        </function>
    </signatures>'''
    
    objc.initFrameworkWrapper(
        frameworkName="IOBluetooth",
        frameworkIdentifier="com.apple.Bluetooth",
        frameworkPath=objc.pathForFramework('/System/Library/Frameworks/IOBluetooth.framework'),
        globals=globals()
    )
    objc.parseBridgeSupport(
        IOBT_BRIDGESUPPORT, 
        globals(), 
        objc.pathForFramework('/System/Library/Frameworks/IOBluetooth.framework')
    )
    
    def set_ioblpstate(s):
        #   int s : 0 = off, 1 = on
        IOBluetoothPreferenceSetControllerPowerState(s)
        s1 = -1
        for i in range(50):
            s1 = get_ioblpstate()
            if s1 == s:
                break
            time.sleep(0.1)
        if s1 != s:
            sys.stderr.write('Unable to set bluetooth power state to %s\n' % ('off' if s == 0 else 'on').encode('utf-8'))
            sys.exit(1)
        return s1
    
    def get_ioblpstate():
        return IOBluetoothPreferenceGetControllerPowerState()
    
    def main():
        m = [ a.decode('utf-8') for a in sys.argv[1:] ]
        if m == []:
            print '%d' % get_ioblpstate()
        elif m == ['0']:
            print '%d' % set_ioblpstate(0)
        elif m == ['1']:
            print '%d' % set_ioblpstate(1)
        elif m == ['-1']:
            print '%d' % set_ioblpstate(1 if get_ioblpstate() == 0 else 0)
        else:
            sys.stderr.write('Usage: %s [mode]\n\t%s, %s, %s, %s\n' % (
                sys.argv[0], 'mode: 0 = off', '1 = on', '-1 = toggle', '(void) = query')
            )
            sys.exit(1)
        sys.exit(0)
    
    main()
    

     

     

     

    And in case you're not familiar with shell scripting, here's an AppleScript wrapper.

     

     

    --APPLESCRIPT
    --bluetooth_powerstate(0) -- off
    bluetooth_powerstate(1) -- on
    --bluetooth_powerstate(-1) -- toggle
    --bluetooth_powerstate(9) -- query
    
    on bluetooth_powerstate(m)
        (*
            integer m : operation mode
                0  = off
                1  = on
                -1 = toggle
                9  = query (print current state)
            return integer : resulting state
        *)
        if m = 9 then set m to ""
        do shell script "/usr/bin/python <<'EOF' - " & m & "
    # coding: utf-8
    # 
    #   file:
    #       bluetooth_powerstate.py
    # 
    #   function:
    #       get or set bluetooth power state
    # 
    #   usaeg:
    #       ./bluetooth_powerstate [mode]
    #           mode :
    #               0  = off
    #               1  = on
    #               -1 = toggle
    #                  = query (print current state)
    #   version:
    #       0.10
    # 
    import sys, objc
    import time
    from CoreFoundation import *
    
    IOBT_BRIDGESUPPORT = '''<?xml version=\"1.0\" standalone=\"yes\"?>
    <!DOCTYPE signatures SYSTEM \"file://localhost/System/Library/DTDs/BridgeSupport.dtd\">
    <signatures version=\"0.9\">
        <function name=\"IOBluetoothPreferenceGetControllerPowerState\">
            <retval type=\"i\"></retval>
        </function>
        <function name=\"IOBluetoothPreferenceSetControllerPowerState\">
            <arg type=\"i\"></arg>
            <retval type=\"i\"></retval>
        </function>
    </signatures>'''
    
    objc.initFrameworkWrapper(
        frameworkName=\"IOBluetooth\",
        frameworkIdentifier=\"com.apple.Bluetooth\",
        frameworkPath=objc.pathForFramework('/System/Library/Frameworks/IOBluetooth.framework'),
        globals=globals()
    )
    objc.parseBridgeSupport(
        IOBT_BRIDGESUPPORT, 
        globals(), 
        objc.pathForFramework('/System/Library/Frameworks/IOBluetooth.framework')
    )
    
    def set_ioblpstate(s):
        #   int s : 0 = off, 1 = on
        IOBluetoothPreferenceSetControllerPowerState(s)
        s1 = -1
        for i in range(50):
            s1 = get_ioblpstate()
            if s1 == s:
                break
            time.sleep(0.1)
        if s1 != s:
            sys.stderr.write('Unable to set bluetooth power state to %s\\n' % ('off' if s == 0 else 'on').encode('utf-8'))
            sys.exit(1)
        return s1
    
    def get_ioblpstate():
        return IOBluetoothPreferenceGetControllerPowerState()
    
    def main():
        m = [ a.decode('utf-8') for a in sys.argv[1:] ]
        if m == []:
            print '%d' % get_ioblpstate()
        elif m == ['0']:
            print '%d' % set_ioblpstate(0)
        elif m == ['1']:
            print '%d' % set_ioblpstate(1)
        elif m == ['-1']:
            print '%d' % set_ioblpstate(1 if get_ioblpstate() == 0 else 0)
        else:
            sys.stderr.write('Usage: %s [mode]\\n\\t%s, %s, %s, %s\\n' % (
                sys.argv[0], 'mode: 0 = off', '1 = on', '-1 = toggle', '(void) = query')
            )
            sys.exit(1)
        sys.exit(0)
    
    main()
    EOF"
        result + 0
    end bluetooth_powerstate
    --END OF APPLESCRIPT
    

     

     

     

    Good luck,

    H

  • by VikingOSX,

    VikingOSX VikingOSX Jan 2, 2016 5:09 AM in response to Hiroto
    Level 7 (20,606 points)
    Mac OS X
    Jan 2, 2016 5:09 AM in response to Hiroto

    Hiroto,

     

    I can confirm that your bluetooth_powerstate.py code, as submitted here, will turn off, and then turn on bluetooth on my 2011 Mac mini running El Capitan 10.11.2, with the default Python v2.7.10.