Wireless Profile push/automation

Discussion in 'Mac Programming' started by micro23, Sep 9, 2011.

  1. micro23, Sep 9, 2011
    Last edited: Sep 9, 2011

    micro23 macrumors newbie

    Joined:
    Sep 9, 2011
    #1
    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.
     
  2. rdo macrumors newbie

    Joined:
    May 24, 2011
    #2
  3. micro23 thread starter macrumors newbie

    Joined:
    Sep 9, 2011
    #3
    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
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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.
     
  5. micro23 thread starter macrumors newbie

    Joined:
    Sep 9, 2011
    #5

    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
     
  6. micro23 thread starter macrumors newbie

    Joined:
    Sep 9, 2011
    #6
    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.
     

Share This Page