Skip to content

Instantly share code, notes, and snippets.

@viktorklang
Last active October 8, 2024 14:06
Show Gist options
  • Save viktorklang/9140756 to your computer and use it in GitHub Desktop.
Save viktorklang/9140756 to your computer and use it in GitHub Desktop.
AppleScript to toggle Wi-Fi in OSX
(*
Installation instructions
=========================
Run as an Application:
1) Open AppleScript Editor and create a new script
2) Paste this file into it
3) Save name it '§(Toggle Wi-Fi)'
- Or substitute '§' for a symbol that you can press with a single key
4) Put it in Applications/Utilities
5) Wait until Spotlight has indexed it
6) Launch using CMD+Space (spotlight) then the symbol you chose + Enter
7) Profit!
Run as a Service:
1) Go to Automator and create a new Service
2) Select Action "Run AppleScript"
3) Select that "Service receives no input in any application"
4) Replace "(* Your script goes here *)" with the contents of this file
5) Save the Service (CMD+S)
6) Enter a proper name for the service (how about "Toggle Wi-Fi")
7) Exit Automator
8) Open System Preferences/Keyboard/Shortcuts
9) Select Services
10) Find your Service in the list
11) Assign it a shortcut (I chose F5)
12) Profit!
MIT License
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*)
set device to do shell script "networksetup -listallhardwareports | awk '$3==\"Wi-Fi\" {getline;print}' | awk '{print $2}'"
set power to do shell script "networksetup -getairportpower " & device & " | awk '{print $4}'"
if power is equal to "on" then
set power to "off"
else
set power to "on"
end if
do shell script ("networksetup -setairportpower " & device & " " & power)
(*If running as an Application you'll want to uncomment the line below to exit the script after it is done*)
(*quit me*)
@GrfxGuru
Copy link

GrfxGuru commented Mar 5, 2018

Works perfectly, thanks for sharing this.

@Bergrebell
Copy link

Thanks for sharing! works like a charm!

@dinarajas
Copy link

Nice work. Thank you so much.

@mcgrk
Copy link

mcgrk commented Mar 6, 2022

Well thought and nicely written.
Cheers!

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