Waiting for Terminal Command....

I’ve got a Python script that I use to create user accounts but it only creates a single user at a time.

I’ve created an Applescript that reads a txt file and then passes the user info to a terminal command that launches the Python script. The only problem is that I cant get the applescript to wait for the terminal command to finish before sending the next user…

Does anyone know of a way to get Applescript to wait for a Terminal command to finish?


set fileRef to open for access alias "path:to:text:file"
set theList to read fileRef using delimiter ","
close access fileRef

tell application "Finder"
	
	repeat with theItem in theList
		tell application "Terminal"
			set myTab to do script "python createaugment.py -u " & theItem
			repeat until busy of myTab is false
				delay 1
			end repeat
			display dialog "finished"
		end tell
	end repeat
end tell

This might work. Note that you shouldn’t need the Terminal or the Finder to run your python script…

set fileRef to open for access alias "path:to:text:file"
set theList to read fileRef using delimiter ","
close access fileRef

repeat with theItem in theList
	-- let the process run on it's own but get the process ID of the spawned process
	set processID to do shell script "python createaugment.py -u " & theItem & " > /dev/null 2>&1 & echo $!"
	repeat
		delay 1
		-- in this repeat loop we check for that process ID
		-- when the process ID is no longer running we get "" returned
		-- so we check for that to indicate that the process has finished
		set processIDTest to do shell script "ps ax | grep " & processID & " | grep -v grep | awk '{ print $1 }'"
		if processIDTest is "" then exit repeat
	end repeat
end repeat

I used to do this with CVS. The reason I did it was so that the person running it would see the activity and would not sit there wondering if the script was working. Here’s an example:


property myCvs : "/usr/bin/cvs"
property CvsBase : " -d :pserver:user@mycvsserver.example.com:/home/cvs"

tell application "Terminal"
-- The following is to kill Terminal (if open) and restart to make sure our script only runs in 'window 1'
-- Far too many times I had the script not work because Terminal was already running.  YMMV.
	try
		quit
	end try
	delay 1
	activate

-- Here's where all the action is
	do script myCvs & CvsBase & " login" in window 1

-- This makes the script wait for Terminal
	set a to 0
	repeat until (a = 1)
		if (window 1 is not busy) then
			close window 1
			set a to 1
		end if
	end repeat
end tell


Of course then I’d do the checkout and make, blah, blah, blah. If you want to use the same window for multiple commands, just nix the ‘close window 1’ command from the loop and do the next command with it’s own repeat loop (staying within the ‘Tell’ for Terminal though :wink: ). On the final command you’ll want to use the close window command within the loop as noted above.

Hope that helps.

cheers for the help!!
I"ll give these a try next week…