Remove Wi-Fi Network daily

Hi everyone, the office Wi-Fi network that I log on to every day changes its password daily.

Because I sometimes lose the connection during the day, I’ve set MacOS to remember the connection as I wouldn’t want to find and retype the password every time that happens.

As a result I have to go through the following routine every morning:

  • Right-click the Wi-Fi icon
  • Go to Network Preferences
  • Choose Advanced
  • Select the Wi-Fi network in question
  • Click on the Remove Wi-Fi Network button
  • Return to Network Preferences
  • Select the Wi-Fi network in question
  • Enter the new password and connect

Would anyone have any ideas on how to automate at least part of that?

Perhaps there’s a way to make MacOS forget a network at a certain time (e.g. midnight) or on reboot? Thanks much for reading!

1 Like

Take a look at the commands listed here.

It’s a bit hard for me to give an answer that I know to work without being able to test it against your actual set-up, but if you look at the options, you should be able to create a script to run each morning at work to use airport to force WiFi to disconnect, then use networksetup to remove the network you want to forget, then use it again to add the network back that you want to use, and maybe even pass it in the new password you want it to use. You could tie that into a tool like Alfred to pass the password in as a parameter.

Hopefully that all makes sense and gives you the start for a workable solution.

Haven’t tested, but could keyboard maestro macro run all those steps to the point where you only need to type the password in?

I would also use KM for this.

  • Create a daily pw file, remove old one
  • Ask for daily pw
  • Type in the pass
  • connect to wifi using pw from file / clipboard (using command line)

This way you would only have to type it once, and it could be re-used all day until it is replaced the next day

Given the list of networks to forget and to connect to are not necessarily consistent, is there any limitation here for Keyboard Maestro being able to select the right one each time?

I know about the image based click, but if the network was sometimes shown and sometimes had to be scrolled to I think that would be unreliable. Is there another way to ensue the correct network is selected that Keyboard Maestro could utilise?

This seems like it might be possible through a combination of Keyboard Maestro, AppleScript, and shell scripting. Then again, I say that about almost everything. :wink:

Do you have administrator / sudo permission on the machine in question? That will determine whether or not I can suggest some things.

Edit 1

Looks like some good AppleScript to use as a starting point here:

How to forget Wi-Fi network in system preferences with Applescript - Ask Different

For example, this will open the “Advanced…” part of System Preferences’ “Network” pane:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "System Preferences"
	activate
	reveal anchor "Wi-Fi" of pane id "com.apple.preference.network"
	delay 1
	tell application "System Events"
		tell process "System Preferences"
			click button "Advanced…" of window "Network"
		end tell
	end tell
end tell

The full code on the linked page shows how to remove a Wi-Fi network from the list. You should be able to modify that fairly easily.

1 Like

As an example:

When I arrive in the office I use the key-combination [⌘⇧⌥⌃ W] (hyperkey-W) to start a work macro that:

  • connects to the office Wifi (if not already connected)
  • starts the VDI session to the work server
  • sets up my screen layout [3 screens, different apps left and right, VDI in center]
  • mutes audio

A similar setup in combination with maybe the scripts from @Tjluoma would make it really easy to do. something like this:

get to work -> press keycombo shortcut -> get prompted for password -> macro removes wifi, adds wifi with correct password - wifi connects -> done

keeping the pw around for a day in a textfile would also make it possible to automate that fully using a location trigger:

arrive (or be) at location -

  • if file exists with today;s date - read content - macro removes wifi, adds wifi with password from file - wifi connects -> done
  • if file does not exist - prompt for password - macro removes wifi, adds wifi with correct password - wifi connects -> done

Keyboard Maestro can automate this in so many ways, it’s all about your needs.

I am a KM user. My question was specifically about interacting with the selection in the networks listing. The scripting approach I suggested should allow it to be driven directly by name and I don’t follow yet how KM could achieve equivalent reliability on this. Can you clarify?

Thanks for the ideas everyone! And thanks much Tjluoma for this script, I’ve used it as below and it works great for forgetting the specific Wi-Fi network.

The next thing to figure out would be if it’s possible to tweak the end so it reselects the Wi-Fi network from the pulldown menu in the Network window and make it prompt for the password:

tell application "System Preferences"
    activate
    reveal anchor "Wi-Fi" of pane id "com.apple.preference.network"
    delay 1
    tell application "System Events"
        tell process "System Preferences"
            click button "Advanced…" of window "Network"
            delay 1
            -- sets focus to the table containing the available Wi-Fi networks
            set focused of table 1 of scroll area 1 of tab group 1 of sheet 1 of window "Network" to true
            delay 1
            keystroke "FIRSTLETTERSOFWIFINETWORKNAMEGOHERE"
            delay 0.2
            -- this clicks the button "-" to remove that Wi-Fi network from the list
            perform action "AXPress" of button 2 of group 1 of tab group 1 of sheet 1 of window "Network"
            delay 0.2
            perform action "AXPress" of button "Remove" of sheet 1 of sheet 1 of window "Network"
            delay 0.2
            perform action "AXPress" of button "Apply" of window "Network"
        end tell
    end tell
    quit
end tell

Does the name (aka “SSID”) of the Wi-Fi network change every day, or just the password?

Are you on a MacBook (“Adorable” or “Air” or “Pro”) or a desktop Mac? (Specifically: Does it have an Ethernet port on it?)

Assuming a MacBook, you’d use en0 (otherwise in 99% of cases you’d want en1 for a Mac mini or iMac):

networksetup -setairportnetwork en0 "YourSSIDhere" "${KMVAR_SSID_PASSWORD}"

You’d replace “YourSSIDhere” with the actual SSID and the last arg would be a variable with the password.

You could do this with a shell script in Keyboard Maestro after using a “Prompt User For Input” for the password (Use “SSID_PASSWORD” as the variable name in Keyboard Maestro’s “Prompt for User Input” if you want to use “$KMVAR_SSID_PASSWORD” in the shell script.

I’d assume that you could save use the previous SSID_PASSWORD as the default answer in the “Prompt for User Input” just in case you have to reconnect later. It’s not terribly secure, but that’s what happens when you have a stupid policy like changing the Wi-Fi password every day.