Last active
October 8, 2024 14:06
-
-
Save viktorklang/9140756 to your computer and use it in GitHub Desktop.
AppleScript to toggle Wi-Fi in OSX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
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*) |
Thanks for sharing! works like a charm!
Nice work. Thank you so much.
Well thought and nicely written.
Cheers!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works perfectly, thanks for sharing this.