Friday, October 23, 2020

#1 2020-02-05 04:32:07 pm

michaeljames
Member
Registered: 2020-02-04
Posts: 2

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

Here is part of my script:

Applescript:


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.

Offline

 

#2 2020-02-05 08:53:45 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 143

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

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:

Applescript:

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)

Offline

 

#3 2020-02-06 01:10:44 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1335

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

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:

Applescript:


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

Last edited by KniazidisR (2020-02-06 01:37:02 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#4 2020-02-06 10:27:51 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 484

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

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.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#5 2020-02-06 10:40:05 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1335

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

t.spoon wrote:

since you're new here, I thought I'd bring up "do shell script" just in case you weren't aware.



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.

Last edited by KniazidisR (2020-02-06 11:37:45 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#6 2020-02-06 11:13:55 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 484

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

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.

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.


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.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#7 2020-02-06 11:36:08 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1335

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

t.spoon wrote:

KniazidisR,

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


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.

t.spoon wrote:

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.


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

Applescript:


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

Last edited by KniazidisR (2020-02-06 12:38:28 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#8 2020-02-06 01:19:45 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 484

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

KniazidisR,

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

checking the integrity of a file with Terminal is safer



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?

But you are still wrong when advising against using Terminal.


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


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#9 2020-02-06 03:37:03 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 643

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

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:

New Text File 1.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=5/6)
New Text File 2.txt
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=4/6)
New Text File 3.txt
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=3/6)
New Text File 4.txt
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=2/6)
New Text File 5.txt
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=1/6)
New Text File.txt
           0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=0/6)

sent 381 bytes  received 152 bytes  1066.00 bytes/sec
total size is 0  speedup is 0.00


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.

Last edited by peavine (2020-02-06 06:39:49 pm)


2018 Mac mini - macOS Catalina

Offline

 

#10 2020-02-06 05:51:04 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 406

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

I have not test your code but this is what I do for my python script.
When it has finish I will get display notification with info, not before.

I use osascript -e 'display notification \"My message\"' and have that last in the line of my do script

Regards.

michaeljames wrote:


Applescript:


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 & " ; " & "osascript -e 'display notification \"Complete\"'" & ";" & "exit" in window 1
   end tell

Last edited by Fredrik71 (2020-02-06 05:57:16 pm)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Online

 

#11 2020-02-07 01:05:22 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1335

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

t.spoon wrote:



checking the integrity of a file with Terminal is safer



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?



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.

Last edited by KniazidisR (2020-02-07 01:37:58 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#12 2020-02-07 04:58:56 pm

michaeljames
Member
Registered: 2020-02-04
Posts: 2

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

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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)