How to script Bluetooth on/off and Tethering on Snow Leopard

6

3

The Bluetooth / Tethering features in OSX are excellent. But they could be a little bit better. Is there a way to script (via AppleScript?) turning the bluetooth radio on and off, so that I could bind it into a key. No reason to drain battery while not tethered.

It would be even better if the connecting itself could be automated. Now I have to click the Bluetooth icon, then select my phone, and select connect. All unnecessary work I have to repeat many times every day. This could be so much more convinient.

tstm

Posted 2010-01-21T15:34:53.087

Reputation: 171

There are some Automator answers here which solve the BlueTooth part: https://apple.stackexchange.com/questions/152333/toggle-bluetooth-applescript-not-working-in-yosemite (for anyone still finding this question by search)

– AjahnCharles – 2018-01-13T01:06:33.277

Answers

2

I doubt the following helps, but I cannot test it. Still, maybe it helps you find a solution...

Maybe MarcoPolo can help. It can trigger an action (like: enable bluetooth) when some condition is met (like when some application starts running, or maybe when you are out of reach of some network).

So: if for tethering you start some specific program:

  • Start the tethering application
  • Open MarcoPolo's preferences
  • On the Contexts pane, add a new Context and give it a name like "Tethering active"
  • On the Rules pane, select "Add RunningApplication Rule". The dropdown will show you all applications that are currently running, so you can then select your tethering application. Link that to the Context you just created.
  • On the Actions pane, select "Add ToggleBluetooth Action" and link it to the "arrival" of the Context you just created.
  • Likewise: disable bluetooth for "leaving" that Context

I think there's not really such application, is there? :-(

Arjan

Posted 2010-01-21T15:34:53.087

Reputation: 29 084

There is no such application, it's just integrated into the OSX Bluetooth Preferences pane. The closest "Application" that does this is System Preferences. – tstm – 2010-01-22T08:37:29.743

But MarcoPolo does support toggling Bluetooth. It's pretty close already! Now all I need is the connection support. – tstm – 2010-01-22T08:41:43.477

1

@tstm, what happens to tethering when you disable bluetooth, and then later enable it again? And one can also enable bluetooth using blueutil, http://www.frederikseiffert.de/blueutil/ And after switching on bluetooth with blueutil, maybe you can control other details using networksetup, http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man8/networksetup.8.html On my Mac, sudo networksetup -listallnetworkservices does mention bluetooth.

– Arjan – 2010-01-22T09:38:09.370

Great tip! Unforutnately, digging into the utility, it only supports connecting ppp-connections, not PAN. – tstm – 2010-01-22T11:41:29.553

1

Sure, you can do this.

  1. Open up Automator, and create a service.
  2. Record yourself turnng on/off bluetooth.
  3. Save the service.
  4. Open up the Keyboard Preferences pane
  5. Click the Keyboard Shortcuts tab, and then select Services on the left, and scroll down to General on the right.
  6. Check the box next to your new service.
  7. Double-click to the right of your new service, near the scroll bar, to set a shortcut key.

Hope that helps!

pkaeding

Posted 2010-01-21T15:34:53.087

Reputation: 1 420

This kind of works, but the fastest I can get the automator workflow to do this is 5 seconds, and because it uses actual mouse clicks to do it, it's not terribly reliable.

It's a step in a good direction, but with all the scripting / object support in OSX, there must be a better way? – tstm – 2010-01-21T16:59:41.490

And there's also the problem of not being able to detect if BT is already on or not. The script should be a bit smarter, it could detect the state and only toggle it on if it was off etc. – tstm – 2010-01-21T17:02:32.090

It also steals my focus and mouse, thus rendering me unable to do anything else for the same 5 seconds. Not that useful, really. I can perform the action myself in less than that time. – tstm – 2010-01-21T17:10:02.713