I figured out a command to enable/disable wifi.

networksetup -setairportpower en0 on
networksetup -setairportpower en0 off

Now, I was wondering how to combine it in AppleScript in a way that when Safari opens wifi is turned on and when Safari closes wifi is turned off?


Run EDITOR=nano crontab -e and add a line like this:

* * * * * pgrep -qx Safari&&networksetup -setairportpower en0 on||networksetup -setairportpower en0 off

The command is only run once every minute though.

You could also save this plist as ~/Library/LaunchAgents/safariwifi.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <string>while sleep 10;do pgrep -qx Safari&amp;&amp;mode=on||mode=off;[[ $last != $mode ]]&amp;&amp;networksetup -setairportpower en0 $mode;last=$mode;done</string>

Then run launchctl load ~/Library/LaunchAgents/safariwifi.plist.

  • Thanks Lri LaunchAgent script works great! Can you please ask you for one more small modification of script to put condition to check and execute only if Mac is on battery? Many thanks in advance! Joe – ivan Nov 27 '13 at 12:29
  • Try using while sleep 10;do if pgrep -qx Safari||pmset -g|grep -q 'AC Power.*\*$';then mode=on;else mode=off;fi;networksetup -setairportpower en0 $mode;done. – Lri Nov 27 '13 at 12:52
  • Hi Laury, possibly help with this one apple.stackexchange.com/questions/113519/… ? It should be a second issue for a guru :-) Thanks in advance! Joe – ivan Dec 12 '13 at 20:40

You must log in to answer this question.

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