Wednesday, January 4, 2017

Applescript control of bluetooth and wifi

Here's a small script that will flip the state of both bluetooth and wifi radios on your Mac.
It is tested on a macOS Sierra 10.12.2, and the descriptions ("bluetooth" and "Wi-Fi") may be altered in previous (or future) versions. Read on below if you need to change this.

tell application "System Events" to tell process "SystemUIServer" to tell (menu bar item 1 of menu bar 1 whose description is "bluetooth") to {clickclick (menu item 2 of menu 1)}


tell application "System Events" to tell process "SystemUIServer" to tell (menu bar item 1 of menu bar 1 whose description starts with "Wi-Fi") to {clickclick (menu item 2 of menu 1)}

To learn the correct description terms open Accessibility Inspector and choose point to inspect (crosshair), then select the menubar item that should match the description. The description is the label name in Basic section. Be aware that the label may change depending on the state of the menubar item (e.g. Wifi changes the trailing string after "Wi-Fi" depending on the connection status, whereas "bluetooth" does not)

No comments: