Testing the Terminal for activity

Dear All:

Part of my script runs a command in the Terminal, and I want my script to wait until the command has run its course. So I want to test for activity in the terminal window in which I’m executing the command. Here’s the script I have so far.

tell application "Terminal"
	activate
	set openWindows to (get name of windows)
	set currentWindow to item 1 of openWindows
	set busyTest to busy of window currentWindow
		do script "emacs" in window currentWindow
end tell
busyTest

emacs is just a dummy application I’m running for now.

My problem is that if I evaluate my variable busyTest right after the command to the terminal is issued (as I do when I run the script as presented here), it returns the value false. Then, if I evaluate the variable again a little later while emacs is still running in the same window, it returns the value true, just as it should.

I’ve tried placing a with timeout command in the script, like so:


tell application "Terminal"
	activate
	set openWindows to (get name of windows)
	set currentWindow to item 1 of openWindows
	set busyTest to busy of window currentWindow
	with timeout of 20 seconds
		do script "emacs" in window currentWindow
	end timeout
end tell
busyTest

Unfortunately, this does not have the desired effect.

Any thoughts would be much appreciated.

Thanks,
Bernhard.

Try something like this:

tell application "Terminal"
	set busyTest to true
	do script "/bin/sleep 2" in front window
	
	repeat until busyTest is false
		set busyTest to busy of front window
		delay 0.5 -- You can change this number
	end repeat
	
	-- whatever else
end tell

Is there an equivalent for a “do shell script …”? The terminal must be watching something to set a window’s “busy” property, and that can’t just be stout. What does the terminal know that an AppleScript can’t find out, I wonder?

Dear Bruce:

that’s beautiful. Your suggestion is much more elegant than my fumbling.

Thanks a lot,
Bernhard.

Hi Bruce,

some time ago I spent some time on the same problem. I tried solutions using the busy state like yours above but I’ve experienced that it is not 100% reliable (it works in most cases but sometimes it fails). Seems it fails on slower machines and/or under heavy processor usage since the teminal does not return it’s busy state immidiately after having received a command. (I tried with ‘lsof’ on a G4/400 and some processes running - it fails in about 50% of all attempts).
Maybe this could be solved with another delay before starting your repeat loop but I found this other solution (makes only sense if the Terminal window is no longer needed after the execution of the ‘do script’, of course):

tell application "Terminal"
	
	-- make a new window:
	do script ""
	
	-- store reference to this window
	set newWin to first window whose frontmost is true
	
	-- run the script (lsof as an example for a lengthy task) + a command to close the window after finishing:
	do script "lsof; osascript -l AppleScript -e " & quoted form of ("tell application \"Terminal\" to close (first window whose frontmost is true)") in newWin
	
	-- wait until the window is gone:
	repeat while (exists newWin)
	end repeat
	
end tell

-- should procede here after 'do script is done:
display dialog "done"

Note: I think I already posted this a while ago but I can’t remember where and when, sorry …

[Here’s the previous thread] ACB

D.