Waiting until process is done

Is there a method for making an application wait until the previous process is complete before continuing with the next tell command?

I am trying to do this:

  1. Launch Terminal
  2. Have Terminal run a script from a plain-text file (copies several files/folders from one location to another)
  3. Have Terminal quit as soon as this script is finished (i.e. returns with a command prompt).

As it is now, the moment the copy command in the text-file script starts, Terminal quits. I had an earlier problem with mounting a disk image; the AppleScript would want to start copying folders from the disk image before it had finished mounting. I was able, with a repeat command and a variable, to halt the AppleScript until the disk finished mounting. However, here I am at a loss for what requirement to have a repeat command look for in order to be satisfied that the Terminal operation is complete (and back to the command prompt).

There wouldn’t happen to be any kind of “when done [action executed]” command, would there? :wink:

This is an ugly method of doing this, but it works…at least to find out if the terminal is not waiting at the prompt.

set isBusy to true

repeat until isBusy is false
	tell application "Terminal"
		tell window 1
			set isBusy to busy as boolean --> Test if busy
		end tell
	end tell
	delay 1 --> Check every second
end repeat

display dialog "Terminal is No Longer Busy!"
quit application "Terminal"

The ‘busy’ property checks to see if the window is doing something other than waiting for a command. This is a real memery hog, so you don’t wan to run it for very long. Perhaps you could work it into an idle handler so it releases it’s hold on the memory between ‘busy’ tests.

j

Thanks for the reply.

A few questions:

  1. I take it “window 1” is the first window that the application opens, not the active window? For some reason, when I run my Terminal script, Terminal opens one window by default and runs the script in a second window.

  2. Does “boolean” need to be replaced by me with a boolean operator, or is “boolean” part of the AppleScripting language as a value? I’ve yet to use any boolean values in my scripts thus far.

I’m just going on what the terminal dictionary tells me, and have little experience with terminal. The window “window 1” refers to whatever window is topmost. The best way would be to launch the app and persuade it to do it’s business in the open window. This worked for me…

set myScript to "man perl"

tell application "Terminal"
	activate
	do script myScript in window 1
end tell

set isBusy to true
repeat until isBusy is false
	tell application "Terminal"
		tell window 1
			set isBusy to busy as boolean --> Test if busy
		end tell
	end tell
	delay 1 --> Check every second
end repeat

tell me to activate
display dialog "Terminal is No Longer Busy!"
quit application "Terminal"

The terminal dictionary provides these parameters that might help you adjust this to work for you…

As far as the boolean issue goes, that is a coercion that needn’t be changed. You’re just telling the window that you want an answer as “true” of “false”.

Perhaps some of the guys familiar with scripting terminal could add to this or provide a better method?

Take care,
j

Thanks. That worked like a charm.