How can I tell when Terminal has finished a process so I can move on?

Here is part of my script:


                tell application "Terminal"
		set position of front window to {800, 900}
		set size of front window to {1000, 100}
		set custom title of tab 1 of front window to "Uploading In Progress..."
		tell front window to set tContents to (contents of tab 1)
		
		do script "rsync -avr -P " & quoted form of POSIX path of currentPath & " " & quoted form of POSIX path of localDestination & ";exit" in window 1
	end tell

After the copying of files has finished, I’d like to display a dialog letting the user know that it’s complete. I’ve tried a few things but to no avail. The dialog pops up immediately.
Any help would be greatly appreciated.

There is a property of a tab called busy, which indicates whether or not a tab is busy, i.e. currently executing a command. You can use it to do something like this:

tell application "Terminal"
				tell (a reference to window 1)
								if not (exists) then do script "clear"
								set _W to its id
				end tell
				activate
				tell window id _W
								set the position to {0, 0}
								set the size to {200, 900}
								set tab 1's custom title to "Uploading..."
								do script "clear; man rsync" in it
								
								repeat while tab 1 is busy
												delay 1
								end repeat
								
								set output to tab 1's contents
								do script "exit" in it
								if exists then close
				end tell
end tell

log "Script terminated at: " & text 12 thru -1 of ¬
				((current date) as «class isot» as string)

Method with the busy property works well. Thanks for the solution. Only I would build the ready for use script differently. Tested with the local items on the Mojave:


set aFilePath to quoted form of (POSIX path of (choose file))
set destinationFolderPath to quoted form of (POSIX path of (choose folder))

tell application "Terminal"
	activate
	do script "rsync -avr -P --progress " & aFilePath & space & destinationFolderPath
	close window 2
	tell window 1
		set bounds to {100, 200, 1100, 300}
		set custom title of tab 1 to "Uploading In Progress..."
		repeat while tab 1 is busy
			delay 1
		end repeat
		set tContents to contents of tab 1
	end tell
	quit
end tell

tell application "Finder"
	activate
	display dialog "The synchronization of the file is completed"
end tell

You may well have a good reason to be scripting the terminal instead of using “do shell script,” but since you’re new here, I thought I’d bring up “do shell script” just in case you weren’t aware.

Applescript can directly run shell commands without having to script terminal. When it’s used that way, unless you redirect the shell output, it will by default wait until the shell command is completed, and direct the shell results back to Applescript.

You could just display a progress bar or notification, run the shell script, then show your dialog that the rsync has completed.

No, I’m aware. Instead, you are not aware that the OP want to have the process visually, that is, using Terminal. But even doing all in the background I would not use do shell script, since there is a well-known technique for checking the file for completeness of changes.

KniazidisR,

I wasn’t saying you were new here, I was saying michaeljames is new here.

Obviously I know that you know about “do shell script,” you have over 800 posts, you’re here all the time, you’ve been in threads with me arguing about “do shell script” before. I didn’t disagree with, criticize, or even mention your post in any way.

New people show up here all the time asking about things like how to UI script interactions with applications that have scripting dictionaries… people who just found Applescript and don’t even understand that it’s possible to interact through scripting dictionaries.

I have no idea what michaeljames’s level of Applescript proficiency is. As I started off:
“You may well have a good reason to be scripting the terminal instead of using “do shell script.””
But there is no way to tell one way or the other from his post. Maybe he wants to script terminal, maybe he doesn’t know he has options. Unless he reports back, we don’t know.

I’m curious what this technique is for checking files that can be done by scripting terminal that can’t be accomplished by other means.

I apologize for accepting your remark to me. But you are still wrong when advising against using Terminal. There are 2 reasons why you are wrong: 1) you cannot use the progress bar for a shell command 2) checking the integrity of a file with Terminal is safer. For example, when the network connection is not of the best quality.

That’s right. This works fine, but I don’t know any way to show progress of downloading in this case:


set aFilePath to quoted form of (POSIX path of (choose file))
set destinationFolderPath to quoted form of (POSIX path of (choose folder))
set tContents to do shell script "rsync -avr -P " & aFilePath & space & destinationFolderPath

try
	set completed to word 2 of (paragraph -4 of tContents)
	if not (completed is "100") then
		display dialog "Something gone wrong"
	else
		display dialog "The synchronization of the file is completed"
	end if
on error
	display dialog "Something gone wrong"
end try

KniazidisR,

The progress bar would not show updates as the copy completes, only that the copy is being performed.

I still don’t understand what you’re talking about here. What’s this “safe way” of checking file integrity with Terminal that can’t be done via “do shell script” or other means?

It’s interesting I’m wrong about that, since I never advised against using terminal.

Out of curiosity, I ran the OP’s script to see what sort of information the rsync command outputs to the Terminal. With 5 files in a folder, the informative output was:

FWIW, I personally would :

  • run rsync by way of do shell script;
  • write the rsync output to a log file; and
  • show a dialog which reports whether rsync completed the copy successfully, and the dialog would include an option to view the log file.

Anyways, the OP’s original question has been answered, and the OP has several alternative approaches should he wish to consider them. That’s good IMO.

I like to see downloading progress. As with torrents. It is just nice. Not only to me, but for many others. So, assuming that OP like to see downloading progress, he has 2 options: 1) using Terminal + rsync utility method (described above) , 2) using well-known technique for checking the destination file for completeness of changes + knowing what is the size of source file + providing some AppleScript progress bar.

And, you can’t at all make downloading progress visible all time with do shell script method (that is, it is not even the option)!
That is why I don’t like do shell script method (here).

So, when I say “more safe”, I say that the 2-nd method fails when the internet connection is bad. For example, when the connection exists, but is bad, and sometimes has delays when transferring the bytes.

Thanks everyone for your input.
I will try the busy property. I did try before, but perhaps I just implemented it incorrectly.

I am aware of “do script” but I like the idea of the terminal window popping up to show the user what is happening.

rsync is a wonderful tool that has, among other qualities, the ability to show the progress of the synchronization process visually.

Therefore, I decided to improve my script above. To sync a folder (instead of a single file) and to improve stability. Here it is:

-- To test visually better, rsync some folder with movies in it
set sourceFolderPath to text 1 thru -2 of (POSIX path of (choose folder))
set destinationFolderPath to POSIX path of (choose folder)

tell application "Terminal"
	activate
	do script "rsync -avr -P --progress " & quoted form of sourceFolderPath & space & quoted form of destinationFolderPath
	tell window 1
		repeat until tab 1 is busy
			delay 0.02
		end repeat
		set bounds to {100, 200, 1100, 600}
		set custom title of tab 1 to "Uploading In Progress..."
		repeat while tab 1 is busy
			delay 0.02
		end repeat
		set tContents to contents of tab 1
	end tell
	display dialog "The synchronization of the folder is completed"
	quit
end tell