1

I have been trying to create a service for toggling ethernet on off, but can't manage to get it right.

It is for os x high sierra.

So far I have this, it runs with no error and it does appear on my services menu, but it does not toggle the ethernet connection.

I have tried some code for airport and it works. But I really need it for Ethernet 1, en0.

on run {input, parameters}

(* do shell script "networksetup -setnetworkserviceenabled Ethernet 1 off" user name “xxxx” password “xxxx” with administrator privileges
do shell script "networksetup -setnetworkserviceenabled Ethernet 1 on" user name "xxxx" password "xxxx" with administrator privileges *)

return input end run

I have done some workflows recording actions for this but it just a bit creepy and it really is a hassle this way.

Any help would be so much appreciated.

  • Creepy? What’s creepy about workflow recordings? – IconDaemon Nov 2 '18 at 22:20
  • it just keeps asking for privacy settings for letting automator run, it is just to slow for me. and Creepy as in: SLOW ... Anyway is there some one with a better input? – Carlos Jaramillo Nov 2 '18 at 22:23
  • did you try networksetup -setnetworkserviceenabled Ethernet 1 off in Terminal ? – Ruskes Nov 3 '18 at 3:55
0

You might not need administrator privileges (I don't need them in order to toggle Wi-Fi on/off). But the problem you're having is likely because you didn't put "Ethernet 1" in quotes. So, assuming your ethernet connection is, indeed, called "Ethernet 1", then the command to turn your ethernet off looks like this:

on run {input, parameters}

    do shell script "networksetup -setnetworkserviceenabled \"Ethernet 1\" off"

return input end run

If you want to toggle it on/off depending on its current state, then you can do this:

on run {input, parameters}

    do shell script ¬
        "[[ \"$(networksetup -getnetworkserviceenabled 'Ethernet 1')\" = \"Enabled\" ]] \
         && networksetup -setnetworkserviceenabled 'Ethernet 1' off \
         || networksetup -setnetworkserviceenabled 'Ethernet 1' on"

end run

But, since your entire AppleScript is, in fact, a shell command wrapped inside a do shell script command, then I'd recommend using an Execute Shell Script action in your Automator workflow instead of an AppleScript one. Then you can simply write the command out in its naked form, which is much more sensible:

[[ "$(networksetup -getnetworkserviceenabled 'Ethernet 1')" = "Enabled" ]] \
&& networksetup -setnetworkserviceenabled 'Ethernet 1' off \
|| networksetup -setnetworkserviceenabled 'Ethernet 1' on
  • I am new here ... mm how do I do that? – Carlos Jaramillo Nov 3 '18 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .