This has been a particularly interesting challenge and may have implications for scripting a lot of other shells.
tell application "Keychain Scripting"
set s to system info
set myKey to s's computer name
tell keychain 1
set chosenKey to the first key whose name is myKey as string
set adminpwd to the password of chosenKey as string
end tell
end tell
set myScript to "echo " & adminpwd & " | sudo periodic daily echo job done"
tell application "Terminal"
activate
delay 1
do script myScript
delay 5
repeat
set myProcesses to (the count of processes of tab 1 of window 1)
if myProcesses is not equal to 3 then
set myDialog to the contents of tab 1 of window 1
set activeProcesses to (the processes of tab 1 of window 1)
exit repeat
end if
delay 5
end repeat
tell application "SystemUIServer"
activate
display dialog myDialog & return & "Active processes" & return & activeProcesses
end tell
quit
end tell
--note: You can set Terminal to close without complaint in its settings pane.