Problem with evaluating Terminal result value

Hello, I am struggling with the script below, can anyone suggest why the “if result in window 1” does not work? The logic within the “if” condition never runs, yet the terminal has a “1” shown?? Any ideas?


	tell application "Terminal"
		(* initialize variables *)
		set exit1 to false
		
		launch
		do script "TELNET" in window 1
		delay 0.5
		do script "TELNET 192.168.1.22 9999" in window 1
		delay 0.5
		do script "RFW 16" in window 1
		delay 0.5
		if result in window 1 contains "1" then
			do script "WFW 9000, 999" in window 1
			delay 0.5
			do script "EXIT" in window 1
			delay 0.5
			set exit1 to true
		end if
	end tell

if you look at the terminal window, it tells you, at least it does for me, that you’re not using one of the commands correctly

I cut this snippet of code out of my larger code, I don’t get any errors when I compile?

i think i’m getting that error because of the ip

what do you get in your terminal window?

It now asks me for the Admin login credentials, once I enter those, the terminal app opens but nothing gets entered? Anyway to elliminate the logging in process?

do you need the terminal window open?

you can try this:

[code]set exit1 to false

set result1 to text returned of (do shell script “telnet 192.168.1.22 9999; rfw 16”)
if result1 contains “1” then
do shell script “telnet 192.168.1.22 9999; wfw 9000,999”
set exit1 to true
end if[/code]

Yes, I’d like the terminal window to be open. If I open the terminal window, anything using “do shell script” does not work, I am assuming this is only used when the terminal window is not open? It looks like all I can use is “do script xxxxx in window 1”, but then I cannot access the results displayed in the terminal window?

i believe so, that’s why i suggest doing my method. doing do shell script when opening the window will crash the system whereas doing my method should work. can you possibly do a combination of the two methods? what is it exactly you want to accomplish?

Contents of window x can give you the content of the window NOTE: only the content that is displaying!!!

Also you don’t need a terminal window open for do shell script. Terminal is an application that opens a shell and do shell script is part of an OSAX that opens a shell, executes and closes. These two have nothing in common with each other only that they both open a shell to the world of Darwin.

Do shell script can work but you need to use spawn and expect, same as like when you connect through SSH with another machine on through do shell script. Telnet commands mostly only useful with a complete session and not for every command a new session so what you’ll do with expect and spawn is what you do now in AppleScript. You do a command, expect some results and when it is true you do the next command until the session is complete.

thank you for clarifying that. i was wondering what the “do script” was and why the “shell” was missing

I would like to use the “do script “xxxx” in window 1”, since it seems to work the best for me. I will be opeing various other applications (itunes and a powerpoint presentation) which will be the front window. Having the terminal window open would be better for me since I don’t have to keep attaching to the ethernet device (camera).

I see the reply anout using the “contents of window x”, I am assuming that this will give me the entire content of the window, is there anyway to just evaluate the response to the last terminal command?

can’t you use my method in another try block in yours?

contents of… will give you what’s printed out

Ok, the script below works, BUT, I would like the “result1” variable to only contain the response to the last terminal command “RFW 16”, since searching the whole content of the “window 1” may contain the response I am looking to receive, yet is may be for a past command? Any ideas?


tell application "Terminal"
		(* initialize variables *)
		set exit1 to false
		
		launch
		do script "telnet" in window 1
		delay 0.5
		do script "telnet 192.168.1.22 9999" in window 1
		delay 0.5
		do script "RFW 16" in window 1
		delay 0.5
		set result1 to contents of window 1
		if result1 contains "1" then
			do script "WFW 9000, 999" in window 1
			delay 0.5
			do script "EXIT" in window 1
			delay 0.5
			set exit1 to true
		end if
end tell

The only problem with the method you mentioned is that I need to attach/detach from the camera for every command, I have a ton of commands to send. Also, the integration of this logic will be harder to do since I am not seeing any feedback because of the terminal not being open. I’d rather leave the terminal app open and issue “do script “xxxx” in window 1” commands, but I also need to be able to see each commands response? Is this a catch 22, it sounds like this can’t be done when the window is open? Is that correct?

I could search the whole terminal window, if I could include the last command as well. This does not work;


if result1 contains "RFW 16; 1" then

How could I write the above in order to capture the proper characters to do this?

Why does this not work properly;


set temp1 to "1" & return
if result1 contains temp1 then

The terminal window is displaying a “1” and a carriage return, how do I simulate this in a variable?

i would assume

set result1 to "1/n"

or

set result1 to "1 "

I got it to work! I used the following code;



set result1 to (last word of contents of window 1 as string) is equal to "1"


This retrieves the last word on the terminal screen, works perfectly. Thank you for your help folks!

i’m assuming you are using result 1 like this:

[code]if result1 is “1” then

end if[/code]
because wouldn’t that just set reult1 to 1?

glad you got it mate!