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
Thanks in advance.