Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Reply
Question marked as Best reply

Posted on Jan 2, 2016 12:29 AM

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

User uploaded file

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

User uploaded file

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.

3 replies
Question marked as Best reply

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

User uploaded file

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

User uploaded file

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.

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

Turn on bluetooth with applescript !

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.