Macro to Toggle Bluetooth and Wireless?

Our messages crossed. Yes. That was the previous poster's idea too. tx!

I did read GG's post, but I must have been really REALLY really dumb today because I didn't understand what he was saying.

@ gglick
Execute a Macro was the solution. Merci.

1 Like

Would you mind sharing your two macros? I would like to do the same thing.

I think you were talking to jipnet, because the two macros that I included screenshots of were just empty stubs.

Here are the ones I made and that seem to work just fine (both controlled by the Toggle Internet macro with one keystroke combination)

Toggle BT.kmmacros (2.4 KB) Toggle Internet.kmmacros (2.4 KB) Toggle WIFI ON:OFF.kmmacros (2.0 KB)

Here are screenshots with code:

ToggleWiFi:

Execute AppleScript
set status to do shell script "networksetup -getairportpower en0"
if status ends with "On" then
  do shell script "networksetup -setairportpower en0 off"
else
  do shell script "networksetup -setairportpower en0 on"
end if

Toggle BT:

Execute AppleScript
tell application "System Preferences"
   reveal pane id "com.apple.preferences.Bluetooth"
   -- activate

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

   try
       tell application "System Events" to tell process "System Preferences"
           click button "Turn Bluetooth Off" of window "Bluetooth"

           click button "Turn Bluetooth Off" of sheet 1 of window "Bluetooth" of application process "System Preferences" of application "System Events"
       end tell

       delay 1

   on error
       tell application "System Events" to tell process "System Preferences"
           click button "Turn Bluetooth On" of window "Bluetooth"
           quit
       end tell

   end try

end tell

ToggleInternet (commands both synchronously; NB: both BT and WiFi must be on or off for this command to turn off or on both)

To add this as a Better Touch Tool button on the Touch Bar (allows you to tap it to trigger the Super-Macro that triggers the two sub-macros), you simply configure a button like this (or as you wish):

2 Likes

If you @jipnet want to save the shortcut for your macro and want to trigger the macro via the Touch Bar with BetterTouchTool, then use the KM Macro AppleScript .
Could even reduce my shortcuts in KM by 90 percent.

Actually, that is what I did, sort of:

appears as a red button on the Touch Bar, acts as a toggle.

Not sure what "the KM Macro AppleScript" refers to, and not sure how that could reduce KM shortcuts by 90% (the space on the Touch Bar is quite limited).

1 Like

I shouldn't try speak for another person (that won't stop me from trying) but I think when he said "KM Macro AppleScript" he probably meant "the KM action Execute AppleScript", ie,

image

I'm not sure how he concludes that we can reduce shortcuts by 90%. Perhaps he meant if we code things in Applescirpt we can reduce our code in KM proportionately. Sure. If you eat more Apples (AppleScript) you can eat fewer Kiwis (Keyboard Maestro). But you still need to eat the same amount of fruit.

1 Like

Hi @jipnet and @Sleepy, at the beginning I also assigned shortcuts in KM and then used them in BTT to set up a touch gesture.
Since this has led to overlaps with the shortcuts over time, I also changed the use of the AppleScripts included in the macros. I now use it in BTT and save myself the trouble of working with random shortcuts :wink:

I had a post here once about it:

In my post I explain how to create BTT gestures with the mouse or trackpad. You @jipnet must select then in the BTT menu only the carrier for the Touch bar.

I downloaded your three macros from above, and they work fine here, too.

If you want to launch the master macro (Toggle Internet) from BTT, then just use the same method you are already using for launching the two sub macros from the master macro: via AppleScript.

You find the the premade AppleScript to launch your master macro in the “Or by AppleScript” popup:

19-pty-fs8

Then you paste that script to BTT here:

22-pty-fs8

…and set the BTT trigger you want.


BTW, to launch the sub macros from your master macro, you can also make use of the Execute Macro action:

59-pty-fs8
It is made for such cases, so no need to use an AppleScript action here.

1 Like

Hello @Tom, if you are interested, I have a macro with which you can save yourself the manual copy & paste of the AppleScript between KM and BTT.

In KM your macro manners (blue) and trigger the macro. If you have selected the AppleScript action in BTT, simply press the Space key and the AppleScript will be inserted.

2019_07_13_Support_1

UUID AS.kmmacros (24,6 KB)
UUID%20AS

Thanks for the idea. Currently I don’t use BTT, but this may change in the future.


BTW, there is an easier way to get the UUID of a macro:

tell application id "com.stairways.keyboardmaestro.editor"
  selectedMacros
end tell

35-pty-fs8

_[demo] Get ID(s) of Selected Macro(s).kmmacros (3.0 KB)

1 Like

As always many thanks for your tip @Tom :+1:
I've put this in my macro.

I know @Tom that you also use Launchbar to trigger KM macros. Can I improve my macro? Does using the KM URL or the KM AppleScript make a difference?

2019_07_13_Support_1

Actions.kmmacros (51,9 KB)

Thanks Tom. There are obviously many ways to achieve the same goal.

I just noticed that the flaw with this method (and any proposed so far) is that to toggle both on or off, both must be in the same initial state (either on or off). Otherwise, one will go on and the other off. In other words, the solution is really "analogue" and not fully "digital." (no doubt due to the scripts that do the toggling, which I found on the web ready made)

THanks. I will explore these tips in due time. Right now I have a fat red button on my control strip that performs the combined action as scripted in KM, making it a snap to turn on or off the WiFi and BT. (I read that thieves can "sniff" BT and WiFi signals in a laptop in a car, so I want to deprive them of this opportunity in as mindless a way as I can remember.)

I haven't played with BTT folders, but I imagine you can drill down several levels without leaving the keyboard. But at some point, a floating palette might be simpler (alas, not so useful in a laptop format where screen real estate is limited—though your disappearing palette may take care of that too)

1 Like

Indeed, I thought the same. Especially your “Toggle BT” macro is rather fragile, due to its heavy reliance on UI AppleScripting.

Once one macro off means you are out of sync. So maybe better to create macros to enable and disable the stuff. Not toggling.

Well, good catch, but that can be fixed by an IF statement, such as "If Wireless Network BELL101 is connected then DISABLE both networks else ENABLE both networks." That's probably an improvement. But some people might prefer toggling both, which can be done with two IF statements.

I know, you are a big fan of chained GUI actions (clicking buttons, selecting menu items, …). Me not so much :sweat:

What about this:

Not completely “automized”, but dead simple.

If you really want to create dedicated LB actions to launch KM macros, then I would simply duplicate an existing one (in Action Editor, right-click on an action), adapt the name and paste the UUID into the script.
Once again, not automized. But not much gain by “automizing” it. Especially not when automizing it with a fragile GUI macro. (I know, Action Editor is not scriptable, but there isn’t much reason to make it scriptable.)

1 Like