Sudo and Apple Script

so I am using terminal with apple script. Here is the script:

tell application "Terminal"
	activate
end tell
delay 1
tell application "System Events"
	keystroke "sudo passwd root"
	keystroke return
	delay 0.5
	keystroke "asdf"
	delay 0.5
	keystroke return
	delay 1.0
	keystroke "asdf"
	delay 1.0
	keystroke return
end tell

I know I need sudo. How do i prompt for an admin password, and then have it input that into terminal?

Yes yes. I know I shouldn’t be using root. And all the stuff like that. Just answer the question. The Reason why I need Root on my computer doesn’t concern you.

Hello.

This at least lets you achieve whatever commands you wanted to execute with sudo, observe: you don’t use the sudo command at all.

do shell script "ls ~/bin" with administrator privileges

Edit

Actually, If you really want to like you described, I believe you can “login root <passwordfromfile” or use a pty program (pseudo terminal program to possibly fix it, but not if login flushes /dev/tty before it starts reading your password.

So what you’d do, is to “do sciript” such a command in the Terminal window,
then keystroke your commands,
and then logout in the end.

But, maye be the “do script” command of a terminal window can help you?

I have an example of a script below, with some code snagged from Stack Overflow, for toggling a top-window between maximized and minimized as long as that window is the fronmost Terminal window.

property fullscreen : false
set thePid to do shell script "ps -ax |grep top |grep -v \"grep top\" || echo \"false\""
if thePid is "false" then
	tell application "Terminal"
		my newTerminal("Solarized Dark xterm-256color", false)
		tell its front window
			set size to {800, 800}
			if fullscreen then
				set zoomed to true
			end if
		end tell
		do script "top " in front window
		if fullscreen then
			set theTTy to tty of its front window
			set theProcesses to processes of tab 1 of its front window
			set thePid to first word of (do shell script "sleep 3 ;ps -ax |grep top |grep -v \"grep top\"")
			do shell script "export TERM=xterm-color;sleep 10 & kill -3 " & thePid & "; clear"
			set zoomed of its front window to false
			do script "top " in front window
		end if
	end tell
else
	tell application "Terminal"
		tell its front window
			set theProcesses to processes of tab 1
			if (last item of theProcesses) is "top" then
				set thePid to first word of (do shell script "ps -ax |grep top |grep -v \"grep top\"")
				do shell script "export TERM=xterm-color; kill -3 " & thePid & "; clear"
				if zoomed is true then
					set zoomed to false
					set size to {800, 800}
				else
					set zoomed to true
				end if
			end if
		end tell
		do script "top " in front window
	end tell
end if
# songdogtech
# http://stackoverflow.com/questions/1794050/applescript-to-open-named-terminal-window
to newTerminal(settingSetName, asTab)
	tell application "Terminal"
		if asTab is true then
			set countRef to a reference to tabs of first window
			set newKey to "t"
		else
			set countRef to a reference to windows
			set newKey to "n"
		end if
		set originalDefault to name of default settings
		set default settings to settings set settingSetName
	end tell
	try
		set initialCount to count of countRef
		tell application "System Events"
			-- keystrokes always go to the frontmost application
			set frontmost of application process "Terminal" to true
			keystroke newKey using command down
		end tell
		repeat until (count of countRef) > initialCount
			beep
			delay 0.1
		end repeat
		
		tell application "Terminal" to set default settings to settings set originalDefault
	on error m number n
		try
			tell application "Terminal" to set default settings to settings set originalDefault
		end try
		error m number n
	end try
end newTerminal