Read a response string from terminal into AppleScript variable?

I’m writing a script that will open a remote server, enter my password, and then use the “echo” command to send a string to a file. After I send the command, the script uses the “ls” command on the server to see whether the file has in fact been written. Is there any way that AppleScript can see the result of the “ls” command and set that as an AppleScript variable?

Here is the script, edited for privacy:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

display dialog "Enter the string:" default answer ""
set theString to text returned of result

display dialog "Enter remote password:" default answer "" with hidden answer
set serverPassword to text returned of result

tell application "Terminal"
	set ServerAddress to ""
	set ServerUserName to "whoknkows"
	set currentTab to do script ("ssh " & ServerUserName & "@" & ServerAddress & ";")
	delay 2
	do script (serverPassword) in currentTab
	delay 2
	do script ("cd public_html") in currentTab
	delay 1
	do script ("echo a>" & theString) in currentTab
	delay 2
	do script ("ls " & theString) in currentTab
end tell

The login stuff is modified from a StackOverflow post. (I haven’t tested whether the semi-colon at the end of the first remote command is needed there, or needed in all commands.) And I know this is crude and mostly incompetent, but it works to let me know whether theString got echoed to a file, but it would be nice to have a dialog pop up to tell me whether it succeeded or failed, by testing the result of the “ls” command.

Probably there’s a much better way to do this anyway, and I’ll be grateful for any help.

You could get the contents of the Terminal tab and parse it yourself.

That works. It took me quite a bit of searching and testing to discover one way to get the contents:

tell front window to set myText to (contents of (contents of currentTab))

but that at least works. Thank you!

‘History’ of the currentTab contains the same info as the Contents but a little easier to work with:

set myText to history of currentTab

Yes, and easier to remember the next time I try to do something like this. Thank you!