Skip to main content
Solved

AppleScript help


easyedc
Forum|alt.badge.img+16
  • Esteemed Contributor
  • 629 replies

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.

1do shell script "networksetup -setsearchdomains 'Wi-Fi' empty" user name "ADMIN" password "PASSWORD" with administrator privileges
2
3
4do 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

1set 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.

Best answer by mm2270

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:

1set PORTS to do shell script "networksetup -listallnetworkservices | tail -n +2"
2set {oldtid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
3set newList to every text item of PORTS
4set chosenPort to choose from list newList
View original
Did this topic help you find an answer to your question?

5 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • Answer
  • June 5, 2015

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:

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

bentoms
Forum|alt.badge.img+35
  • Hall of Fame
  • 4331 replies
  • June 5, 2015

@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.


Forum|alt.badge.img+31
  • Hall of Fame
  • 920 replies
  • June 5, 2015

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_ethernet_interfaces

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
Forum|alt.badge.img+16
  • Author
  • Esteemed Contributor
  • 629 replies
  • June 8, 2015

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
Forum|alt.badge.img+16
  • Author
  • Esteemed Contributor
  • 629 replies
  • June 8, 2015

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:

1tell current application
2 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.
3
4On the next page, select the network connection that you are currently using.
5
6Click "Proceed" to fix your Mac's network settings.
7" with title ¬
8 "COMPANY IT DNS Repair" with icon note ¬
9 buttons {"Proceed", "Cancel"} default button 1
10
11
12
13 set PORTS to do shell script "networksetup -listallnetworkservices | tail -n +2"
14 set {oldtid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
15 set newList to every text item of PORTS
16 set chosenPort to choose from list newList with prompt "How are you connected to the internet?"
17
18 set NIC to item 1 of the result
19
20
21 do shell script "networksetup -setsearchdomains '" & NIC & "' empty" user name "ADMIN" password "PASSWORD" with administrator privileges
22 do shell script "networksetup -setdnsservers '" & NIC & "' empty" with administrator privileges
23 do shell script "networksetup -setsearchdomains '" & NIC & "' COMPANY" with administrator privileges
24 set the_result to do shell script "scutil --dns | egrep -wiA11 'DNS Configuration'"
25 display dialog the_result with title "Your Results"
26end tell

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings