Reciving Text realtime from terminal output file

Hey guys!
I got a little problem here. I have a shell command that is executed into the background through the “do shell script” command. I have designated a file to put the output of the command like if you were to run it in terminal. Anyway, I got the file and everything, and heres the problem, I want to display this text, but it is constantly changing for about a minute or so, and the text file itself receiving the output will not change unless you close out and open it again. The question is how do I constantly receive information from this file until a certain line comes up, say “Searching Complete”.


You can use Console to view the output as it is generated. Here’s a simple example that I recently wrote for someone else. It doesn’t use the shell to generate the output but hopefully you can adapt it.

set file_ to (path to desktop as Unicode text) & "rob's example log.txt"
set counter to 0

tell application "Finder" to ¬
	set console_ to (application file id "") as Unicode text

repeat 20 times
	set counter to counter + 1
	set content_ to ("test log " & counter & return)
	write_to_file(content_, file_, true)
	do shell script "sleep 1"
	if counter is 1 then
		tell application "Finder" to open alias file_ using console_
	end if
end repeat

to write_to_file(this_data, target_file, append_data)
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
			close access file target_file
		end try
		return false
	end try
end write_to_file

– Rob

Thanks! That worked perfectly! Here is the code for it.

[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
Now that it works, I would like it to wait for a specific line in the code, and then do something when it occurs, like "display dialog “Complete” or something like that. Is there any way to do a “repeat until” command?


It’s hard to offer further advice on this without seeing the code that you are using but it is possible to repeat until a condition is met.

set var1 to ""
set counter to 0

repeat until var1 is "specific content"
	set counter to counter + 1
	if counter is 5 then
		set var1 to "specific content"
		set var1 to counter
		display dialog "var1's value is " & var1 & ". Process will continue"
	end if
	-- update var1 through whatever means you are using
	-- write var1 to file
end repeat

display dialog "var1's value is "specific content". Process Complete"

– Rob

OK, here is a bit of code that might clear things up.

Here is what I want it to do, but not sure how using Console

Hopefully that makes things a bit more clear.


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

I won’t claim to understand what your script is doing but is there any way to save the data to a variable before writing it to disk? This might allow the script to inspect the contents of the variable. If that isn’t an option, maybe someone with shell skills can offer advice.

– Rob (who is not very talented when it comes to shell scripting)

No no no, you are misunderstanding. This has nothing to do with shell scripting. All that shell command does is start a process and gives the output to the file. Just like typing “top” into the terminal. When you type it in, it gives you process info. All i did was take the info it returns and puts it into a text file, which i then open with console. As you probably already know, top constantly changes information, so you would have it wait for, say, a certain program to open. To make it simple, I’m working with Console with this one, not shells. And I don’t think you can set it to a variable. Here is a web of what i want to happen

{BUTTON}-------Do the shell command ----------- Display dialog “Complete”
__________________ |
________output it written to file (By terminal)
___AppleScript Waits for certain line to appear (in the file itself or console)


I understand that you are directing the output to a file which is being read/displayed by Console. The (potential) problem is that the AppleScript script would have to read the file as it is being generated since, as far as I know, there’s no way to interact with Console via AppleScript. That’s why I wondered if there was a way to capture the output with a variable instead of directing it to the file. For instance, the following will return a result to the script and then display the result in a dialog.

set date_ to do shell script "date"
display dialog date_

In the simple example above, the script could evaluate the contents of date_ and then react accordingly. I suspect that the nature of the top command makes it impractical to try to capture it in a variable. The only thing that I know to try is to use a repeat loop to read the file via AppleScript (take Console out of the picture) and look for whatever it is that indicates that the process is complete. I just don’t know what the consequences might be and I don’t have the time to experiment. Here’s some (untested, possibly buggy) code to play with:

set file_ to (path to desktop as Unicode text) & "File_Output"
display dialog "starting server"
do shell script "cd " & "'" & thepath & "/system" & "'" & ";./server-bin" & " " & themode & ¬
	" " & themap & " > " & (quoted form of POSIX path of file_) & " 2>&1 & echo $!"

set content_ to ""
repeat until the last paragraph of content_ is "output to look for" -- I'm unsure of what it is that indictes that the process is complete
	set content_ to paragraphs of (read file file_)
end repeat

display dialog "Process Complete"

Good luck!

– Rob (who must soon prepare to go to work on the midnight shift)