A little help combining a few scripts

I have 2 scripts that I’ve found (thanks to Google and other forums) and they work as desired on their own, but I’d like to combine them rather than running one after the other.

The first turns Internet Sharing on or off depending on current status:


tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
tell application "System Events" to tell process "System Preferences"
	click checkbox 1 of row 10 of table 1 of scroll area 1 of group 1 of window "Sharing"
	delay 1
	if (exists sheet 1 of window "Sharing") then
		click button "Start" of sheet 1 of window "Sharing"
	end if
end tell
ignoring application responses
	tell application "System Preferences" to quit
end ignoring

The second kills the original natd process and then starts my custom one:

set app_name to "natd"
set the_pid to (do shell script "ps -ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid) with administrator privileges and password
delay 3
do shell script "/usr/sbin/natd -f /etc/natd/en2.natd.conf" with administrator privileges and password

I’ve tried combing them haphazardly to create the following:

tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
tell application "System Events" to tell process "System Preferences"
	click checkbox 1 of row 10 of table 1 of scroll area 1 of group 1 of window "Sharing"
	delay 1
	if (exists sheet 1 of window "Sharing") then
		click button "Start" of sheet 1 of window "Sharing"

		set app_name to "natd"
set the_pid to (do shell script "ps -ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid) with administrator privileges and password
delay 3
do shell script "/usr/sbin/natd -f /etc/natd/en2.natd.conf" with administrator privileges and password

	end if
end tell
ignoring application responses
	tell application "System Preferences" to quit
end ignoring

But Script Editor throws back an error “System Events got an error: A privilege violation occurred.” whilst highlighting:

do shell script ("kill -9 " & the_pid) with administrator privileges and password

That’s the main problem I’m having. Breaking it down to what I want it to do:

  • Turn Off Internet Sharing if it’s already on
  • Turn On Internet Sharing if it’s off
  • Once turned on, kill natd process, replace with my custom one.

The scripts currently do that but I want it to happen within 1 script.

Another smaller problem is that I’d like at the end of the 2nd script for it to press the return key (to OK the password window) since I use a blank password (yes, I know this is bad). I’ve tried adding:

tell application "System Events"
	tell process "SecurityAgent"
		delay 1
		keystroke return
	end tell
end tell

But nothing happens, the password window is still there and my cursor starts beach balling until I manually press the OK button on the password dialog window.

Can anyone help? I don’t think this should be too difficult to do, but I don’t know how to do it myself :slight_smile:

Thanks in advance.

Hi,

the privilege violation error occurs always, if a shell script with administrator privileges parameter is within an application tell block.
by the way: the syntax you use is wrong. Either you specify the password, then use this syntax

do shell script "whatevercommand" password "¢¢¢¢" with administrator privileges

if you want the security agent window, omit the password parameter.

Try this, the GUI script part is quite robust, it turns off (if it’s on) and turns on again Internet Sharing


tell application "System Preferences"
	activate
	reveal anchor "Internet" of pane id "com.apple.preferences.sharing"
end tell

tell application "System Events"
	tell process "System Preferences"
		tell table 1 of scroll area 1 of group 1 of window 1
			tell (1st row whose value of static text 1 is "Internet Sharing")
				if value of checkbox 1 is 1 then
					click checkbox 1
					delay 1
				end if
				click checkbox 1
			end tell
		end tell
		repeat until exists sheet 1 of window 1
			delay 0.5
		end repeat
		click button "Start" of sheet 1 of window 1
	end tell
end tell
tell application "System Preferences" to quit
set app_name to "natd"
set the_pid to (do shell script "ps -ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid) with administrator privileges
delay 3
do shell script "/usr/sbin/natd -f /etc/natd/en2.natd.conf" with administrator privileges

Ah, well blame whoever I copied that code from :slight_smile: Since that bit of code worked on it’s own, I never doubted that it was wrong in any way.

Thank you muchly for the revised code, it works perfectly. I’d been scratching my head and googling for a few hours trying to make it work on my own, but I conceded to my lack of coding knowledge and ended up posted here. Not 30 minutes later it’s fixed :smiley:

Kudos and thanks again!

and is always a boolean operator which doesn’t make sense at all

with administrator privileges and password

is the same as just


with administrator privileges

because the and operator has no effect in this case

Haha yes that doesn’t make sense, but like I say I copied the code and (foolishly) presumed it to be correct. Although googling “administrator privileges and password” turns up a fair few results so I guess they’re wrong too? :slight_smile:

yes, they are, but AppleScript is polite enough to ignore the wrong syntax silently :wink:

the authorization window appears always if no password is specified (or this and password syntax is used)