AppleScript help

easyedc
Valued Contributor II

Trying to make something more glamorous than it should be. I have an apple script that's fairly simple for cleaning up wifi on our workstations when our VPN client mucks DNS/Search domains.

do shell script "networksetup -setsearchdomains 'Wi-Fi' empty" user name "ADMIN" password "PASSWORD" with administrator privileges


do shell script "networksetup -setdnsservers 'Wi-Fi' empty" with administrator privileges

Trying to make that infinitely more portable and glamorous and I reealise that I can make lists easily with something like

set PORTS to do shell script "networksetup -listallnetworkservices | tail -n +2"

but I'm scratching my head as to how to make my list become a string to enable buttons to accommodate various hardware models/port configs. Any help would be appreciated. And before you ask why an AppleScript, it's because we're deploying to both admins (who can elevate) and non-admins, and with the AppleScript I can pass elevated credentials and set as a run only application.

If you're an AppleScripter or have some other thoughts, greatly appreciated.

Oh... BTW.. It's 5 o'clock on a Friday and my brain has decided to turn off.

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

Whenever I've needed to make one off little double clickable apps like what you're describing, I usually turn to Platypus and a shell script. Platypus gives you a nice little platform for making simple applications like this that don't box you in to using only Applescript. It will accept pretty much any scripting language you would like to use, including of course Applescript itself, and has several default window formats you can use, or you can choose None as well so it just runs your embedded script.

But if you want to stick with Applescript for this, this link may help:
https://discussions.apple.com/thread/4889921?tstart=0
Based on the response there, it looks like to you can use the above example on that link to turn the shell script results into an Applescript list that can then be displayed for the user to select from.

Something like this may give you something to work off of:

set PORTS to do shell script "networksetup -listallnetworkservices | tail -n +2"
set {oldtid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
set newList to every text item of PORTS
set chosenPort to choose from list newList

View solution in original post

5 REPLIES 5

mm2270
Legendary Contributor III

Whenever I've needed to make one off little double clickable apps like what you're describing, I usually turn to Platypus and a shell script. Platypus gives you a nice little platform for making simple applications like this that don't box you in to using only Applescript. It will accept pretty much any scripting language you would like to use, including of course Applescript itself, and has several default window formats you can use, or you can choose None as well so it just runs your embedded script.

But if you want to stick with Applescript for this, this link may help:
https://discussions.apple.com/thread/4889921?tstart=0
Based on the response there, it looks like to you can use the above example on that link to turn the shell script results into an Applescript list that can then be displayed for the user to select from.

Something like this may give you something to work off of:

set PORTS to do shell script "networksetup -listallnetworkservices | tail -n +2"
set {oldtid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
set newList to every text item of PORTS
set chosenPort to choose from list newList

bentoms
Release Candidate Programs Tester

@easyedc Whenever I've need to do something like this, I've run it via self service & had a policy that just clears the domains.

Or we've employed a policy & cached it. That policy would then run @ login/logout.

Just putting alternative ideas out there.

rtrouton
Release Candidate Programs Tester

I recently solved a related problem of setting updated search domains on Ethernet interfaces, but as a shell script instead of AppleScript:

https://github.com/rtrouton/rtrouton_scripts/tree/master/rtrouton_scripts/set_dns_search_domains_on_...

In my case, I'm deploying the script using as a payload-free package.

The script linked below should work for Wi-Fi, assuming that all of the machines in question have Wi-Fi somewhere in the name of the network interface:

https://gist.github.com/rtrouton/cae95efd093c954c1c36

easyedc
Valued Contributor II

Wow... I got @bentoms, @rtrouton & @mm2270 to respond?? I sort of feel that is a win in it's own right. My issue with using a cached policy (and I've got limited experience caching them) is that our java-based VPN app will on-connection inject internal search domains and DNS entries into the network interface. That's fine until a graceful or ungraceful VPN disconnection occurs, and then it does not remove the settings that have been injected to the interface. If you try to browse to another site or back to our VPN portal, without or DNS/Domains available, you get stuck in limbo. The only resolution with this occurs is to clear out the entries manually. For our admins, this is easy, for our non-admins, this is a challenge.

Platypus looks promising, I was not familiar with that app. I'll also take a look at Rich's work since that's often quite nice, too. Thanks guys for all the responses/suggestions. In an ideal world, I'd push to get the VPN vendor changed out to a better vendor, but that's nothing I have control over. :(

easyedc
Valued Contributor II

I went with @mm2270's answer on this due to the fact that I already had the 2nd half of the applescript written, and just needed to link them. I was able to turn it into something more graceful that looks like this now:

tell current application
    display dialog "This repairs network settings (DNS entries & Search Domains) that may be corrupt as a result of the VPN.  You can view the results once completed.  You have the follwing network ports on your computer. 

On the next page, select the network connection that you are currently using.

Click "Proceed" to fix your Mac's network settings.
" with title ¬
        "COMPANY IT DNS Repair" with icon note ¬
        buttons {"Proceed", "Cancel"} default button 1



    set PORTS to do shell script "networksetup -listallnetworkservices | tail -n +2"
    set {oldtid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
    set newList to every text item of PORTS
    set chosenPort to choose from list newList with prompt "How are you connected to the internet?"

    set NIC to item 1 of the result


    do shell script "networksetup -setsearchdomains '" & NIC & "' empty" user name "ADMIN" password "PASSWORD" with administrator privileges
    do shell script "networksetup -setdnsservers '" & NIC & "' empty" with administrator privileges
    do shell script "networksetup -setsearchdomains '" & NIC & "' COMPANY" with administrator privileges
    set the_result to do shell script "scutil --dns | egrep -wiA11 'DNS Configuration'"
    display dialog the_result with title "Your Results"
end tell