Applescript to change WIFI networks

3,612 views
Skip to first unread message

nizer

unread,
Mar 29, 2017, 2:46:04 PM3/29/17
to QLab
I have to switch WIFI networks in my show and wanted to automate this.

I have searched online but had no luck pulling this off.

The closest I got was with this applescript.

tell application "System Events"

    tell process "SystemUIServer"

        click (menu bar item 1 of menu bar 1 whose description contains "Wi-Fi")

        click menu item "MY NETWORK HERE" of menu 1 of result

    end tell

end tell


It pulls up the menu but does't switch networks.

I am guessing a shell script could do it too, but none of my research has proved successful.

Thank you.

Sam Kusnetz

unread,
Mar 29, 2017, 4:05:54 PM3/29/17
to ql...@googlegroups.com
Mark

Leaving aside the question of whether or not this is a good idea, or whether or not switching wifi networks mid-show is failure prone (hint: it is!), you can instruct a Mac to join a specific wifi network using this shell command:

networksetup -setairportnetwork AirPort (SSID) (PASSWORD)


So in an AppleScript, it would be:

do shell script “networksetup -setairportnetwork AirPort myNetwork myPassword”

I know it’s not really your style to reduce complexity, but if you can find a way to put all your gear onto a single wifi network I think you’re likely to have far fewer problems.

Best
Sam

Sam Kusnetz | Figure 53
--
Contact support anytime: sup...@figure53.com
Follow Figure 53 on Twitter: http://twitter.com/Figure53
---
You received this message because you are subscribed to the Google Groups "QLab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qlab+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/qlab/78c698d9-cabc-4c8b-a0b5-3cd82a218fc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Charles Coes Lists

unread,
Mar 29, 2017, 4:22:35 PM3/29/17
to ql...@googlegroups.com
totally helpful to make sure that the wifi is off at top of show thought (or on the dedicated no internet network...)

johng...@mac.com

unread,
Mar 29, 2017, 8:39:27 PM3/29/17
to QLab
I would almost have considered this, though its really not a smart way to have to do things and it was a low budget show already using too many question marks.

The setup:
Small show, lots of sound cues and two phones.
1 iPhone 5
1 Android

Both specified in the script and both required to ring and make sounds.

I used Stagecaller to run the phones but ran into an issue, no surprise. The android version of stagecaller is an alpha, not even beta, but was still the only option available and in practice worked totally fine using OSC. In reh we used the rehearsal hall open wifi network to run the phone cues, which was mostly good, but occasionally wouldn't fire. I had another router I planned to use for a closed LAN, and the theatre also had a closed LAN for lighting we were allowed to use. The issue was that the android phone wouldn't recognize a network if it was closed and had no internet, so we had to keep that one on the open, theatre network. I could almost imagine using the script to switch networks in the show, to keep the iPhone on the closed network and the android on the open network, but would expect that reconnecting the networks would be way more flakey then using both phones on the theatre rental/cast/crew network. 

In practice we have about 90% accuracy, with a few critical cues run off of emergency hotkeys. We had backups for phone cues in speakers on hotkeys but it ended up being worth it have working phones for the rest of the show. 

There could be some uses for that script.....

Mark Nizer/Neisser

unread,
Mar 29, 2017, 11:18:28 PM3/29/17
to ql...@googlegroups.com
Andy Dolph has convinced me to get a second USB Wifi adapter and set it to another IP address. This may solve the issue without the script. But I may use this script this weekend. 

Complexity that works is not complex ;-)

Thanks Sam for the script. 

Mark Nizer/Neisser

On Mar 29, 2017, at 4:05 PM, Sam Kusnetz <s...@figure53.com> wrote:

You received this message because you are subscribed to a topic in the Google Groups "QLab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/qlab/wJ0g45ixzAE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to qlab+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/qlab/CAOOK5AYAS6RfLXcUmYWkfq%3D66iyN53utG4vAaEcz9jykEcX6Fg%40mail.gmail.com.

Sam Kusnetz

unread,
Mar 30, 2017, 9:23:52 AM3/30/17
to ql...@googlegroups.com
On March 29, 2017 at 11:18:28 PM, Mark Nizer/Neisser (freeun...@gmail.com) wrote:
Andy Dolph has convinced me to get a second USB Wifi adapter and set it to another IP address. This may solve the issue without the script. 

I think this is a terrific idea.

Complexity that works is not complex ;-)

I could not possibly disagree with this more.

Thanks Sam for the script. 

You are quite welcome!


Good luck!

Mark Nizer/Neisser

unread,
Mar 30, 2017, 11:03:40 AM3/30/17
to ql...@googlegroups.com
My QUOTE sounded smart when I first wrote it but the more I think about it I agree with you too. I've been trying to simplify all of my equipment and make it a no-brainer. Again thanks for your help and your wisdom.
--
Contact support anytime: sup...@figure53.com
Follow Figure 53 on Twitter: http://twitter.com/Figure53
---
You received this message because you are subscribed to a topic in the Google Groups "QLab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/qlab/wJ0g45ixzAE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to qlab+uns...@googlegroups.com.

nizer

unread,
Apr 1, 2017, 8:33:24 PM4/1/17
to QLab
For the life of me I can't get this to change networks. Does it have to be a Airport Network? My WIFI networks are "FU WIFI" and "AMG HOTSPOT"

I am not planning on making this permanent, as my new WIFI adapter will be here soon, this more about just confirming this as an option.

Here is one of the many attempts:

tell application id "com.figure53.QLab.4" to tell front workspace
 
set mynetwork to "FU WIFI"
 
do shell script "networksetup -setairportnetwork Airport mynetwork activemediagroup"
end tell


On Wednesday, March 29, 2017 at 4:05:54 PM UTC-4, sam kusnetz wrote:
Mark

Mac to join a specific wifi network using this shell command:

networksetup -setairportnetwork AirPort (SSID) (PASSWORD)


So in an AppleScript, it would be:

do shell script “networksetup -setairportnetwork AirPort myNetwork myPassword”

BTW: You would be proud of me, I eliminated 3 apps that I used to use for some MIDI and other control options. Down to just 3 main ones. QLab 4, Garageband and LXConsole. 

nizer

unread,
Apr 3, 2017, 3:09:32 PM4/3/17
to QLab
Network interfaces are strange. List out the networks via this shell script "networksetup -listallhardwareports" then you can see what the proper settings are for:
do shell script "networksetup -setairportnetwork PORT mynetwork password"

nizer

unread,
Apr 5, 2017, 8:28:30 AM4/5/17
to QLab
I totally agree with Sam Kusnetz that this is a terrible idea but I did it anyway, for the puzzle.

Here is a workspace (QLab4) that will switch WIFI networks and give you a little confirmation that it worked.


Switch WIFI Networks BAD IDEA.qlab4

nizer

unread,
May 4, 2017, 2:40:07 PM5/4/17
to QLab
After my disastrous attempt to use a external USB wifi connection, I am revisiting the applescript option.

I have had success with this, except if the WIFI network has a "space" in the name.

Is there way a to account for this in this script?

tell application id "com.figure53.qlab.4" to tell front workspace
set myPortFinal to q name of cue "myport"
set myWIFInetwork to q name of cue "myWIFI"
set MyPassword to q name of cue "myPwd"
set myPortNetwork to "networksetup -setairportnetwork " & myPortFinal & " " & myWIFInetwork & " " & MyPassword
do shell script myPortNetwork
delay 10
set mySSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'"
if mySSID is myWIFInetwork then
start cue "WIFIsuccess"
else
start cue "WIFIfailure"
end if
end tell


I know Sam Kusnetz says this is a terrible idea and I totally agree.

Sam Kusnetz

unread,
May 4, 2017, 2:46:17 PM5/4/17
to ql...@googlegroups.com
Mark

In shell scripting, you permit a space character by preceding it with a backslash. So if your network is called “FAILURE PRONE”, try naming your cue “FAILURE\ PRONE”

Might work?

I appreciate your determination to make this work!

Best
Sam

Sam Kusnetz | Figure 53

--
Contact support anytime: sup...@figure53.com
Follow Figure 53 on Twitter: http://twitter.com/Figure53
---
You received this message because you are subscribed to the Google Groups "QLab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qlab+uns...@googlegroups.com.

Eric Lott

unread,
May 4, 2017, 3:03:42 PM5/4/17
to nizer, ql...@googlegroups.com
Just had another thought on a possible way to solve your larger issue.  Theoretically you could get something like the TP-LINk N300 Nano Router useing it in Client Mode and connect to the second wireless network then through to the computer using the RJ45 port. (This may require a usb adapter depending on how new your computer is.) This is the same idea as a second wireless card, but the computer sees the second network as hardwired instead of  wireless. Im sure there is really good reason that this wouldn’t work, but I can’t think of it right now.

Good luck,

-- 
Eric Lott
--
Contact support anytime: sup...@figure53.com
Follow Figure 53 on Twitter: http://twitter.com/Figure53
---
You received this message because you are subscribed to the Google Groups "QLab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qlab+uns...@googlegroups.com.

Gareth Risdale

unread,
May 5, 2017, 12:27:47 PM5/5/17
to QLab
On Wednesday, 5 April 2017 13:28:30 UTC+1, nizer wrote:
"This is a terrible idea but I did it anyway..."

Sounds like a chapter in Mark's autobiography...
#FallingWithStyle

nizer

unread,
May 6, 2017, 9:57:55 AM5/6/17
to QLab
Excellent idea. I think is issue is in this line.

set myPortNetwork to "networksetup -setairportnetwork " & myPortFinal & " " & myWIFInetwork & " " & MyPassword

The variable myWIFInetwork is added to the shell script and made into the script that will run.

The final script for example might be:

networksetup -setairportnetwork es1 AMGhotspot 123456
or
networksetup -setairportnetwork es1 OTHER\ NERTWORK 123456

any spaces in the myWIFInetwork ends up skipping the password as the script thinks the word after the space is the password and not the rest of the wifi network name.

Unfortunately expressing the network with a space in the name with a "\ " still fools the shell script into thinking the second word in the network name is the password.

I think technically this is mute anyway as 99% of WIFI networks don't contain or allow spaces. I think Apple routers allow this but few others.

At this point it is really just the puzzle to me and I appreciate you playing with me.

Is there a solution ;-)

Rich Walsh

unread,
May 6, 2017, 10:22:44 AM5/6/17
to ql...@googlegroups.com
I don’t think you’ve tested what you think you have – this line won’t even compile:

set myWIFInetwork to "FAILURE\ PRONE"

“\” is an escape character in AS too – you use it for “\”” to mean “ in a dialog, for example.

Express your SSID as a string without trying to use \ in it and then use:

set myPortNetwork to "networksetup -setairportnetwork " & myPortFinal & " " & quoted form of myWIFInetwork & " " & MyPassword

Why exactly do you need 2 WiFi networks anyway? What can you possibly need to do that can’t be done by configuring a single network correctly?

Rich

nizer

unread,
May 6, 2017, 1:13:11 PM5/6/17
to QLab
That fixed the "Spaces in the WIFI name" issue. Very grateful.

The reason I need 2 WIFI networks is that every show I am in very different situation. I always run my own private/hidden show network without internet access for Lemur(OSC) and EpocCAM live cameras. I also have a Enttec ODE in there with a ethernet setup for DMX lighting. 

I have a single routine that requires that I have internet access. I don't have often have access to a ethernet cable with internet at these venues (and wouldn't know how to add that in without disrupting my other reserved IP addresses if they did). There is very often a WIFI network that I can log into to get to the internet. If there is not and I have stable cellular I use my iPhone's Hotspot feature. I don't have access to reconfigure their WIFI networks, join my Airport express to their network and don't want to use their potentially busy network for normal show control anyway.

With this script (and your helpful tweak), I can switch to the internet and back again with confirmation without more hoop jumping.

Thoughts. Glad to approach this in a different way. When I tried to add a USB WIFI dongle I had full computer freezes and crashes both times I used it. With my solution there is no more hardware to drag around.

Omar

unread,
Sep 20, 2017, 11:49:03 AM9/20/17
to QLab
I was looking for a solution similar to you... finally I found something basic that works for me at https://apple.stackexchange.com/questions/150288/toggling-wi-fi-on-and-off
this is the script, to which you just have to change the value of the menu item... to your desired network, which in my case was "6" after trying different values:

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 6 of menu of (first menu bar item whose value of attribute "AXDescription" contains "Wi-Fi") of menu bar 1

end try

end tell


hope it helps.

Jason M

unread,
Nov 20, 2018, 12:28:15 AM11/20/18
to QLab
I 99% agree with using networksetup.  There are a few edge cases where that doesn't work.  In particular if the WiFi credentials are more complex than a simple passcode, networksetup won't switch the network, even if it's been pre-setup.

The applescript solution above works reasonably well (well enough that Ops/Developer types could use it in a pinch.) but it's certainly not a bullet proof solution.  It would be best if networksetup allowed the same level of detail as setting up a WiFi connection via Network/System Preferences.
Reply all
Reply to author
Forward
0 new messages