4

I need an applescript that will toggle wi-fi off and on at set intervals, say every 10 or 15 minutes. I'm using a 2012 MacBookPro running 10.9.5.

1
  • Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results.
    – grg
    Oct 12, 2014 at 15:12

3 Answers 3

4

DISCLAMER: I can't check this (that it actually works) at the moment, but I will later.

This script to turn on/off wifi is taken from here:

try
    set makiaeawirelessstatus to do shell script "networksetup -getairportpower en1"
on error
    display dialog "The script did not work as intended, please check the networksetup command (in terminal) works on your system. It has been tested on mac os 10.7 (Build 11A459e). Other versions of mac os may not have this command available. Please open the applescript in applescript editor for more details." buttons {"kthxbai"}
end try

if makiaeawirelessstatus is "Wi-Fi Power (en1): On" then
    do shell script "networksetup -setairportpower en1 off"
else if makiaeawirelessstatus is "Wi-Fi Power (en1): Off" then
    do shell script "networksetup -setairportpower en1 on"
else
    display dialog "The script did not work as intended, please check your wireless connection is specified correctly. The default in this script is en1 (please open the applescript in applescript editor for more details)" buttons {"kthxbai"}
end if

You'd need to edit it to make it accept arguments (I would, but it'd turn out something like javascript)

Then, if you want the script to run reaccuringly, use something like LaunchAgent or cron. I prefer LaunchAgent, because it's more forgiving.

2

Edit the root's crontab (by for example running EDITOR=nano sudo crontab -e) and add a line

*/15 * * * * ifconfig en1 down;ifconfig en1 up

where en1 is the identifier shown by networksetup -listallhardwareports|awk '/^Hardware Port: (Wi-Fi|Airport)/{getline;print $2}'.

The ifconfig commands require superuser privileges.

0
tell application "System Events" to tell process "SystemUIServer"
    click (first menu bar item whose value of attribute "AXDescription" contains "Wi-Fi") of menu bar 1
    try
        click menu item 2 of menu of (first menu bar item whose value of attribute "AXDescription" contains "Wi-Fi") of menu bar 1
    end try

end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .