Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

micro23

macrumors newbie
Original poster
Sep 9, 2011
4
0
Hey guys, I'm really glad there is somewhere I can come for help. I don't know anything about Applescript but I'm SO CLOSE! Quick idea, then problem.

Working at a location that has WPA2E wireless. I have setup the script so that it auto makes a new location and auto adds the profile. The problem we have is that people already have a new location or already have the profile so I need to delete it first before I add it.

Unfortunately the Applescript ends when it does not find the network. How can I have the applescript say ok fine and just continue on?



do shell script "/usr/sbin/networksetup -removepreferredwirelessnetwork en1 NETWORKNAME"
do shell script "/usr/sbin/networksetup -removelocation en1 LOCATIONNAME"
do shell script "/usr/sbin/networksetup -createlocation LOCATIONNAME populate"
do shell script "/usr/sbin/networksetup -switchtolocation LOCATIONNAME"
do shell script "/usr/sbin/networksetup -addpreferredwirelessnetworkatindex en1 NETWORKNAME 0 WPA2E"


If the NETWORKNAME in step one does not exist it fails and errors out. How do I get it to continue even with an error?

It will also fail on step 2 because the LOCATIONNAME might not exist.

Thank you guys so much in advance this is going to be so huge if I can get this working perfectly.
 
Last edited:
i am not an applescript dev but i think you can put it in a
try / catch block.

Here is a link I just found that might help:
http://macscripter.net/viewtopic.php?id=28573

Hey thanks for the help. I tried 3 different variations but none of them worked and it errors out. Here is the script I tried....


tell application "Terminal"
try
do shell script "/usr/sbin/networksetup -createlocation xxxxx populate"
end try
try
"/usr/sbin/networksetup -createlocation xxxxx populate"
end try
try
/usr/sbin/networksetup -createlocation xxxxx populate
end try
end tell
 
One solution is to put multiple commands in a single do shell script. The shell itself doesn't normally halt when a command fails, so the commands should execute in sequence, even if one of them fails.

The last command to execute will have its success/fail status returned as the status of the entire command sequence. This means that the last command to execute must succeed. Fortunately, there is a shell command for that: the 'true' command.

Also, I don't see a -removelocation action for networksetup. I do see -deletelocation, but it doesn't have a hardwareport parameter (en1), only a locationname parameter. If you really intend to delete the entire location, you need to use the correct action and parameters.

So putting all this together:
Code:
[COLOR="Blue"]do shell script "networksetup -removepreferredwirelessnetwork en1 NETWORKNAME;" & ¬
	"networksetup -deletelocation LOCATIONNAME;" & ¬
	"networksetup -createlocation LOCATIONNAME populate;" & ¬
	"/usr/bin/true;"
[/COLOR]
do shell script "networksetup -switchtolocation LOCATIONNAME"
do shell script "networksetup -addpreferredwirelessnetworkatindex en1 NETWORKNAME 0 WPA2E"
The blue-hilited multi-command line is shown with & joining multiple strings, to make it easier to edit in AppleScript Editor.

The multi-command line should have semicolons exactly as shown. These are what separate a single line into multiple separate commands.

I have tested the above script and it runs to completion. If you execute the steps one at a time, the multi-command step's output says it can't delete LOCATIONNAME, but I don't know if that's significant to solving the problem. After completing all steps, there is a wireless network named NETWORKNAME in the LOCATIONNAME location's list of preferred networks upon completion. I have no real wifi network of that name, so it obviously can't connect.

And you can use /usr/sbin/networksetup for each command-name where I used only networksetup. A full path would make sense if you're unsure which command will be found. Since I know what's on my machine, I used the shorter form.
 
One solution is to put multiple commands in a single do shell script. The shell itself doesn't normally halt when a command fails, so the commands should execute in sequence, even if one of them fails.

The last command to execute will have its success/fail status returned as the status of the entire command sequence. This means that the last command to execute must succeed. Fortunately, there is a shell command for that: the 'true' command.

Also, I don't see a -removelocation action for networksetup. I do see -deletelocation, but it doesn't have a hardwareport parameter (en1), only a locationname parameter. If you really intend to delete the entire location, you need to use the correct action and parameters.

So putting all this together:
Code:
[COLOR="Blue"]do shell script "networksetup -removepreferredwirelessnetwork en1 NETWORKNAME;" & ¬
	"networksetup -deletelocation LOCATIONNAME;" & ¬
	"networksetup -createlocation LOCATIONNAME populate;" & ¬
	"/usr/bin/true;"
[/COLOR]
do shell script "networksetup -switchtolocation LOCATIONNAME"
do shell script "networksetup -addpreferredwirelessnetworkatindex en1 NETWORKNAME 0 WPA2E"
The blue-hilited multi-command line is shown with & joining multiple strings, to make it easier to edit in AppleScript Editor.

The multi-command line should have semicolons exactly as shown. These are what separate a single line into multiple separate commands.

I have tested the above script and it runs to completion. If you execute the steps one at a time, the multi-command step's output says it can't delete LOCATIONNAME, but I don't know if that's significant to solving the problem. After completing all steps, there is a wireless network named NETWORKNAME in the LOCATIONNAME location's list of preferred networks upon completion. I have no real wifi network of that name, so it obviously can't connect.

And you can use /usr/sbin/networksetup for each command-name where I used only networksetup. A full path would make sense if you're unsure which command will be found. Since I know what's on my machine, I used the shorter form.


You are the man :) I can't wait to give this a shot. This is what I ended up doing and it seems to be working. In regards to the command I have no idea why/how i had it set as remove location. Thanks for the pickup on that.


try
do shell script "/usr/sbin/networksetup -removepreferredwirelessnetwork en1 xxxxx"
end try
try
do shell script "/usr/sbin/networksetup -deletelocation xxxxx"
end try


try
do shell script "/usr/sbin/networksetup -createlocation xxxxx populate"
end try

try
do shell script "/usr/sbin/networksetup -switchtolocation xxxxx"
end try

try
do shell script "/usr/sbin/networksetup -addpreferredwirelessnetworkatindex en1 xxxxx 0 WPA2E"
end try
 
Added a few more commands and really cleaned it up nice. As of right now it seems like it's going to totally solve our problem. Thank you so much to everyone that helped me.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.