Skip to content

Instantly share code, notes, and snippets.

@kayheunen
Last active November 2, 2019 17:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayheunen/6334194 to your computer and use it in GitHub Desktop.
Save kayheunen/6334194 to your computer and use it in GitHub Desktop.
AppleScript to Restart WiFi connection, in order to connect to closest AP in your network.
on run
try
set airport_status to do shell script "networksetup -getairportpower en1"
on error
display dialog "The script did't work, please check the 'networksetup' command (in terminal) works on your system. " buttons {"kthxbai"}
end try
if airport_status is "Wi-Fi Power (en1): On" then
do shell script "networksetup -setairportpower en1 off"
do shell script "networksetup -setairportpower en1 on"
else if airport_status is "Wi-Fi Power (en1): Off" then
do shell script "networksetup -setairportpower en1 on"
else
display dialog "The script did't work, please check the Wi-Fi interface name (e.g. en1) " buttons {"kthxbai"}
end if
end run
@MatzeKitt
Copy link

In order to get it working you may need to edit the interface name. To get the interface name, you can run the following command in the Terminal: networksetup -listnetworkserviceorder

You will receive a list that looks something like this:

n asterisk (*) denotes that a network service is disabled.
(1) LPSS Serial Adapter (1)
(Hardware Port: Modem (lpss-serial1), Device: lpss-serial1)

(2) LPSS Serial Adapter (2)
(Hardware Port: Modem (lpss-serial2), Device: lpss-serial2)

(3) USB 10/100/1000 LAN
(Hardware Port: USB 10/100/1000 LAN, Device: en5)

(4) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)

(5) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en7)

In my case, the interface name for the Wi-Fi is en0 (the name next to “Device:”).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment