Apple Script for Updating DNS Servers / Search Domains

I have a simple Applescript I have written to try to update the DNS Servers in Network Preferences.

Right now the pane is set to the Network Preferences, I just need to select Built in Ethernet and then make Changes to the DNS Servers.

Anyone got any ideas of the best way to do this (other than manually entering them)?

Thanks,

Jeff

tell application “System Preferences”
activate
set current pane to pane “com.apple.preference.network”
display dialog “Please set your DNS Servers to 160.111.8.10 and 160.111.8.18”

end tell

hi jeff,

i asked about this last September:

http://bbs.applescript.net/viewtopic.php?id=18396

but never got an answer. i finally did as you are and displayed a dialog asking the user to do this. here is a link to my script for setting up a caching DNS server:

http://bbs.applescript.net/viewtopic.php?id=18422

in case any of that will help you.

cheers.

Hi VT

Someone was asking about this the other week, they where looking down the GUI route, i did a search but didn’t come up with much other than a small bit.
so i’ve added a bit more to it an have come up with this, GUI scripting isn’t the best and always depends on a few factors one being that i tested it on my mac
so yours should have the same menus etc… it might mean adding an extra tab here or a delay there but this works on my machine, with the LOCK unclicked in system prefs:

tell application "System Preferences"
	activate
	set current pane to pane "Network"
end tell
tell application "System Events"
	tell process "System Preferences"
		set frontmost to true
		tell window 1
			click pop up button 2
			tell pop up button 2
				tell menu 1
					click menu item "Built-in Ethernet"
					repeat 5 times
						keystroke tab
					end repeat
					keystroke "160.111.8"
					keystroke return
					keystroke "160.111.8.18"
				end tell
			end tell
		end tell
	end tell
end tell


Thanks Pidge,

I really appreciate your help… :smiley:

I tried your script but I’m getting an error.

Apple Script Error

System Events got an error.
NSReceiverEvaluationScriptError: 4

I’m running Apple Script 2.1.1 (81) / Tiger 10.4.6, not sure if that is a different from what AppleScript /MAC OS X version you are running.

~Jeff

Model: Tiger 10.4.6
AppleScript: 2.11 (81)
Browser: Internet Explorer 6.0
Operating System: Mac OS X (10.4)

Although I haven’t figured out how to do anything with it, I did find my DNS address in /Library/Preferences/SystemConfiguration.plist buried under a key called ServerAddresses. I don’t know whether changing that will alter the setting on the System Preferences pane, however - those are often loaded at login time so you’d probably have to logout/login to see the change.

Hi VT

That error just means that its trying to click on something but can’t cause it’s not in the same place probably has it is in mine.
i’m on 10.4.8 not sure that would make a difference…
try and break the script down a line at a time to see where it finds the error it might just mean adding another tab or removing it or something like that…

Pidge,

It’s failing at Click pop up button 2, which I assume is to select configure?

~Jeff

Model: Tiger 10.4.6
AppleScript: 2.11 (81)
Browser: Internet Explorer 6.0
Operating System: Mac OS X (10.4)

Hi VT

Click popup button 2 is “Show” on mine this is the popup menu with “Built-in Ethernet”
Try a different number.

Make sure you have GUI scripting switched on in the applescript utility…!

That was it pidge, in universal access had to turn on enable access for assistive devices.

Thanks for all your help.

Actually I spoke too soon. I thought it was running fine (I had the correct dns servers already inputted).

When I scrambled them and then ran the script I noticed it wasn’t changing them.

It doesn’t seem to be wanting to entering a tab keystroke anywhere.

I tried adjusting the repeat before keystroke tab (I assume that is trying to hit tab multiple times)

Is there another way to just click text box DNS Servers: or something like that?

~Jeff

I still was having problems with the tabs not being selected after built-in ethernet was selected… I found a utility called UI browser, so I tried adding some extra code to tell the tab groups and groups to see if I could enter the DNS servers and search domains…

Still not working though.

tell application “System Preferences”
activate
set current pane to pane “Network”
end tell

tell application “System Events”
tell process “System Preferences”
set frontmost to true
tell window “Network”
click pop up button 2
tell pop up button 2
tell menu 1
click menu item “Built-in Ethernet”
tell group 1
tell tab group 1
tell group 1
set value of text field 1 to “160.111.8.10”
set value of text field 2 to “domain.com
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell

If you need to change the dns servers in network preferences i found using a do shell script

do shell script “networksetup -setdnsservers Ethernet 192.168.0.1” with administrator privileges
just change the ip to the address of your dns server.
to change domain search use networksetup -setsearchdomains Ethernet domain1 domain 2
use the man networksetup in terminal to get more info…