Applescript & Terminal - Display Message Depending on Terminal Output

Hey everyone,

I’ve written a very simple script to get me on to a SSH using Terminal commands.

It works fine, but I’d like to make it a bit more user friendly by displaying a message when, say, connection is successful and when connection has failed, or user password input has failed.

I’m not sure how to approach that part yet. I was thinking maybe look for a key phrase in the Termnial output and display different messages accordingly.

Here’s what I have so far (below) does anyone have any tips?

Thanks in advance for your help!

display dialog "Please Enter Password" default answer "" with hidden answer
set answer to text returned of result

tell application "Terminal"
	set currentTab to do script ("a terminal command")
	delay 3
	do script answer in currentTab
	delay 2
	do script ("connect to ssh command") in currentTab
end tell

Here is how your can print messages in the terminal without being evaluated

tell application "Terminal"
	-- create new window or tab
	set currentTab to do script ""
	--we're going to write data to TTY
	set theTTY to tty of currentTab
end tell

-- now send some text to the terminal window without executing it. 
do shell script "echo '

You can print messages by starting do script strings with an hash (#).

UPDATE: To make an script really interactive you have to create an bash script. It can read user input from the command line and pass it to the ssh command without messing up the entire terminal window with commands.

DJ, thanks so much for taking the time to reply and apologies for my very late response!

I found a solution to my problem after a bit of hacking around. I decided to post it here in case some/any of it is useful to someone in the future.

I realise that this is by no means a great solution, but I’m still pretty new to coding in general.

-- ask for password with hidden answer, store it in a variable
display dialog "Enter Password" default answer "" with hidden answer
set answer to text returned of result

-- tell terminal to run the login script in the current tab
tell application "Terminal"
	set currentTab to do script ("example login script")
	delay 3
	-- use answer supplied by user in the dialog box
	do script answer in currentTab
	delay 4
	-- copy the entire contents of terminal to a variable called tString
	tell application "Terminal"
		tell front window
			set the clipboard to contents of selected tab as text
		end tell
		set tString to the clipboard as text
	end tell
	-- search the text stored in the variable tString for the word 'failed'
	set errorSwitch to false
	set tNames to "failed"
	repeat with aName in paragraphs of tNames
		if aName is in tString then set errorSwitch to true
	end repeat
	-- if 'failed' is present in the variable, then show a 'password not accepted' message and close terminal.
	if errorSwitch is true then
		display dialog "Password Not Accepted." 
		quit application "Terminal"
		-- else show a confirmation notification
		display notification with title "Password Accepted." subtitle "Logging you in..."
	end if
end tell