Disconnect/connect Ethernet

Please can you check where I’m going wrong with this - I have one to turn off wifi when the ethernet cable is connected and one to do the opposite:

Turn off wifi when on ethernet
Triggered by any of the following:
Trigger iconUSB Device ‘Apple USB Ethernet Adapter’ is Attached
Will execute the following actions:
Action iconExecute AppleScript
do shell script "networksetup -setairportpower en0 off"
Display trimmed results in a window.

Macro iconTurn on wifi when unplug ethernet
Triggered by any of the following:
Trigger iconUSB Device ‘Apple USB Ethernet Adapter’ is Detached
Will execute the following actions:
Action iconExecute AppleScript
do shell script "networksetup -setairportpower en0 on"
Display trimmed results in a window.

1 Like

I realise I’m not supposed to ‘bump’ my own posts, but did anyone have any input on this one please?

Can you post the Macros you are using?

You can:

And then upload to your reply.

Also helpful is an image of the Macros, which you can copy here:

And paste into your reply.

@JMichaelTX has a Macro for uploading Macros, too.

My macro automates the use of the built-in Keyboard Maestro Editor Share Button:

See KM Wiki: How to Post/Upload Your Macro to the Forum

Keyboard Maestro “Global Macro Group Macros” Macro

Global Macro Group Macros.kmmacros (3.2 KB)

Hey There,

First – don’t run shell scripts from AppleScript unless there is a need. Keyboard Maestro has an Execute a Shell Script action.

Second – you don’t describe what your problem is. Give us some details.

-Chris

Oh! I didn’t know. I should’ve read your whole post thoroughly.

Sorry Chris - my problem is that neither action works. When I unplug my macbook from ethernet wifi doesn’t turn on, and vice versa when I plug it back in.

Please clarify your first note specifying what I need to do to rectify the problem.

Thanks

That helps narrow things down. :wink:

Have you run the scripts by themselves?

If so – do they work when run manually?

I don't know for certain, since I don't have a wired Ethernet connection to test with at the moment.

Are you using an Ethernet cable in an Ethernet Port? OR are you using a USB-to-Ethernet adapter?

In any case... The first thing to do is to NOT run a shell script from an AppleScript when it's not actually necessary.

Execute Shell Script → Example.kmmacros (1.7 KB)

If you don't know the difference between an AppleScript and a Shell Script you need to do a little reading.

https://wiki.keyboardmaestro.com/action/Execute_an_AppleScript
https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script

Search Google for:

difference between applescript and shell script

As this article mentions – you can run a shell script from an AppleScript – and you can run an AppleScript from a shell script.

But it is more efficient and easier to read IF you run just one kind of script natively (when that's possible).

Since Keyboard Maestro has an Execute a Shell Script action it is never necessary to run a shell script from an Execute an AppleScript action using a do shell script command.

The only time it's useful to mix and match AppleScripts and Shell Scripts is when one depends upon the output of the other.

Does this clarify things a bit?

-Chris

I guess the OP won't need the solution anymore after 553 days (as Discourse just let me know), but I ran into the exact same problem and maybe others find this helpful too:

It looks like Network Cards connected to USB never really get detached. Even when you unplug them, they remain "connected" until you delete them in System Preferences / Network.

The easiest way to check for connected USB devices is the following shell command:

ioreg -p IOUSB

In my case, I am using a thunderbolt dock that also has a sound device. And that sound device gets disconnected when I unplug my Mac, so I am just using that instead.

Julian

2 Likes

Here is my macro. Pretty straightforward, though:

Keyboard Maestro 8.1.1 “81) De/Activate Wifi when Thunderbolt un/plugs” Macro

81) De-Activate Wifi when Thunderbolt un-plugs.kmmacros (4.3 KB)

2 Likes

Hey mate, i was looking at this and figured it out. I am on MacOS Catalina too, i think things slightly changed. OK, here goes and hope it helps you and others.

Go to your Apple logo on your finder and click on 'About this mac'. Click on System report (towards the bottom) of that screen and copy/write the name of your USB ethernet device like for like.

Next, go to Keyboard Maestro and enter these settings as shown on the screenshots. To do the opposite, set the airpower en0 on and the device to not attached.

1 Like