author: [email protected]
This script automatically switches between predefined Wi-Fi networks with retry logic in case of connection failure. It is designed for use with Hammerspoon, a powerful automation tool for macOS.
- Automatically switches between predefined Wi-Fi networks.
- Supports retrying the connection up to 3 times if the connection fails.
- Provides notifications to inform the user of the connection status.
- Simple hotkey (
Cmd + Alt + Ctrl + W
) to switch between Wi-Fi networks.
- macOS
- Hammerspoon installed: Download Hammerspoon
-
Install Hammerspoon: If you haven't installed Hammerspoon yet, download it from here and follow the installation instructions.
-
Clone or download the script: Save the Lua script from this repository to your computer.
-
Add the script to Hammerspoon:
- Open Hammerspoon and click the Hammerspoon icon in the top menu bar, then click
Open Config
. - Replace the contents of
init.lua
with the provided Lua code in this repository or add the code to your existing config file.
- Open Hammerspoon and click the Hammerspoon icon in the top menu bar, then click
-
Define your Wi-Fi networks:
- Update the
wifi_networks
table in the script to include your SSIDs and passwords:
local wifi_networks = { { ssid = "Your_SSID_1", password = "Your_Password_1" }, { ssid = "Your_SSID_2", password = "Your_Password_2" } }
- Update the
-
Reload Hammerspoon Configuration:
- After saving the script in
init.lua
, click the Hammerspoon icon in the top menu bar and chooseReload Config
. - You can also reload the configuration by opening the Hammerspoon Console (
Cmd + Alt + Ctrl + R
) and typing:hs.reload()
- After saving the script in
- Press the hotkey combination
Cmd + Alt + Ctrl + W
to switch between the predefined Wi-Fi networks. - The script will attempt to connect to the next Wi-Fi network in the list.
- If the connection fails, the script will retry up to 3 times, waiting 2 seconds between each attempt.
To check the output or debug the script, you can open the Hammerspoon Console with the following steps:
-
Open Hammerspoon Console:
- Click on the Hammerspoon icon in the macOS menu bar and choose
Console
from the dropdown menu. - Alternatively, use the hotkey
Cmd + Alt + Ctrl + R
to open the console.
- Click on the Hammerspoon icon in the macOS menu bar and choose
-
View logs:
- The script logs the current Wi-Fi network, the target Wi-Fi network, and the result of the connection attempts in the console.
Current WiFi Network: None
Switching to WiFi Network: Your_SSID_1
Successfully switched to Your_SSID_1