{"id":1258,"date":"2015-08-06T15:57:37","date_gmt":"2015-08-06T14:57:37","guid":{"rendered":"http:\/\/www.phillips321.co.uk\/?p=1258"},"modified":"2015-08-06T15:59:43","modified_gmt":"2015-08-06T14:59:43","slug":"auto-disableenable-os-x-wifi","status":"publish","type":"post","link":"https:\/\/www.phillips321.co.uk\/2015\/08\/06\/auto-disableenable-os-x-wifi\/","title":{"rendered":"Auto disable\/enable OS X WiFi"},"content":{"rendered":"<p>So those of you with a Mac OS X who also use the handoff feature will have possibly <a href=\"http:\/\/osxdaily.com\/2014\/10\/30\/use-handoff-mac-os-x-and-ios\/#comment-721717\" target=\"_blank\">experienced a bug<\/a> with trying to answer calls on the Mac when you have multiple interfaces configured.<\/p>\n<p>I connect to my home network using the MacBooks onboard airport card and also use a thunderbolt ethernet adapter when sat at my desk. The problem lies when I get back to my desk I have to manually disable the airport card and when leaving my desk I have to manually reenable it again&#8230; this drives me insane. Something this simple should be easy to configure on OS X, well it turns out it is!<\/p>\n<p>Using AppleScript it&#8217;s possible to detect the state of an ethernet adapter using a few shall scripts. As such you can then detect the state and use this to toggle the WiFi on\/off using <a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/Darwin\/Reference\/ManPages\/man8\/networksetup.8.html\" target=\"_blank\">networksetup command<\/a>.<\/p>\n<p>Here&#8217;s the code that&#8217;s required:<\/p>\n<div class=\"codecolorer-container applescript vibrant\" style=\"overflow:auto;white-space:nowrap;width:100%;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/><\/div><\/td><td><div class=\"applescript codecolorer\"><span class=\"kw3\">on<\/span> <span class=\"kw1\">idle<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">tell<\/span> <span class=\"kw1\">application<\/span> <span class=\"st0\">&quot;System Events&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">-- identifies the wifi device<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">set<\/span> airPortDevice <span class=\"kw3\">to<\/span> <span class=\"kw1\">do shell script<\/span> <span class=\"st0\">&quot;\/usr\/sbin\/networksetup -listallhardwareports | awk '{if($3==&quot;<\/span>Wi<span class=\"sy0\">-<\/span>Fi<span class=\"st0\">&quot;){getline;print}}' | awk '{print $2}'&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">set<\/span> airPortPower <span class=\"kw3\">to<\/span> <span class=\"kw1\">do shell script<\/span> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;networksetup -getairportpower &quot;<\/span> <span class=\"sy0\">&amp;<\/span> airPortDevice <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot; | awk '{print $4}'&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">-- Bashscript to check number of ethernet interfaces<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">set<\/span> NumOfEthernetInterfaces <span class=\"kw3\">to<\/span> <span class=\"st0\">&quot;EthernetInterfaces=`networksetup -listnetworkserviceorder | grep Ethernet | grep Device | cut -d: -f3 | awk '{$1=$1};1' | sed -e s\/<span class=\"es0\">\\\\<\/span>)\/\/`<br \/>\nfor i in ${EthernetInterfaces};<br \/>\ndo <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; networksetup -listallhardwareports | grep ${i} <br \/>\ndone<br \/>\nexit 0&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">set<\/span> EthernetInterfaces <span class=\"kw3\">to<\/span> <span class=\"kw1\">do shell script<\/span> NumOfEthernetInterfaces<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw1\">do shell script<\/span> NumOfEthernetInterfaces<span class=\"br0\">&#41;<\/span> &gt; <span class=\"nu0\">0<\/span> <span class=\"kw3\">then<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">if<\/span> airPortPower <span class=\"kw3\">is<\/span> <span class=\"kw2\">equal<\/span> <span class=\"kw3\">to<\/span> <span class=\"st0\">&quot;On&quot;<\/span> <span class=\"kw3\">then<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">set<\/span> <span class=\"br0\">&#123;<\/span>gave up:gaveUp<span class=\"br0\">&#125;<\/span> <span class=\"kw3\">to<\/span> <span class=\"kw1\">display dialog<\/span> <span class=\"st0\">&quot;Ethernet found, turning wireless off&quot;<\/span> <span class=\"kw3\">with<\/span> title <span class=\"st0\">&quot;InterfaceToggle&quot;<\/span> <span class=\"kw1\">buttons<\/span> <span class=\"br0\">&#123;<\/span><span class=\"st0\">&quot;OK&quot;<\/span><span class=\"br0\">&#125;<\/span> giving up <span class=\"kw2\">after<\/span> <span class=\"nu0\">1<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; say <span class=\"st0\">&quot;Ethernet found, turning wifi off&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">do shell script<\/span> <span class=\"st0\">&quot;networksetup -setairportpower &quot;<\/span> <span class=\"sy0\">&amp;<\/span> airPortDevice <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot; off&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">end<\/span> <span class=\"kw3\">if<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">else<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">if<\/span> airPortPower <span class=\"kw3\">is<\/span> <span class=\"kw2\">equal<\/span> <span class=\"kw3\">to<\/span> <span class=\"st0\">&quot;Off&quot;<\/span> <span class=\"kw3\">then<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">set<\/span> <span class=\"br0\">&#123;<\/span>gave up:gaveUp<span class=\"br0\">&#125;<\/span> <span class=\"kw3\">to<\/span> <span class=\"kw1\">display dialog<\/span> <span class=\"st0\">&quot;Ethernet not found, turning wireless on&quot;<\/span> <span class=\"kw3\">with<\/span> title <span class=\"st0\">&quot;InterfaceToggle&quot;<\/span> <span class=\"kw1\">buttons<\/span> <span class=\"br0\">&#123;<\/span><span class=\"st0\">&quot;OK&quot;<\/span><span class=\"br0\">&#125;<\/span> giving up <span class=\"kw2\">after<\/span> <span class=\"nu0\">1<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; say <span class=\"st0\">&quot;Ethernet not found, turning wifi on&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">do shell script<\/span> <span class=\"st0\">&quot;networksetup -setairportpower &quot;<\/span> <span class=\"sy0\">&amp;<\/span> airPortDevice <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot; on&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">end<\/span> <span class=\"kw3\">if<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">end<\/span> <span class=\"kw3\">if<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">return<\/span> <span class=\"nu0\">10<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">end<\/span> <span class=\"kw3\">tell<\/span><br \/>\n<span class=\"kw3\">end<\/span> <span class=\"kw1\">idle<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>But to make your life easier I&#8217;ve created an app for you here:<br \/>\n<a href=\"https:\/\/www.phillips321.co.uk\/downloads\/InterfaceToggle.app.zip\" target=\"_blank\">https:\/\/www.phillips321.co.uk\/downloads\/InterfaceToggle.app.zip<\/a><\/p>\n<p>When you&#8217;ve got the app running you can tell it to &#8220;Open at Login&#8221; using the dock option.<br \/>\n<a href=\"https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-19.14.21.png\"><img loading=\"lazy\" src=\"https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-19.14.21-300x227.png\" alt=\"Screen Shot 2015-08-05 at 19.14.21\" width=\"300\" height=\"227\" class=\"aligncenter size-medium wp-image-1260\" srcset=\"https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-19.14.21-300x227.png 300w, https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-19.14.21.png 696w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So those of you with a Mac OS X who also use the handoff feature will have possibly experienced a bug with trying to answer calls on the Mac when you have multiple interfaces configured. I connect to my home network using the MacBooks onboard airport card and also use a thunderbolt ethernet adapter when [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1262,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[435,440,441,439,438,433],"_links":{"self":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts\/1258"}],"collection":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/comments?post=1258"}],"version-history":[{"count":2,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts\/1258\/revisions"}],"predecessor-version":[{"id":1261,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts\/1258\/revisions\/1261"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/media\/1262"}],"wp:attachment":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/media?parent=1258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/categories?post=1258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/tags?post=1258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}