Tuesday, June 27, 2017

#1 2017-05-31 09:53:00 pm

rubik
Member
Registered: 2017-05-31
Posts: 3

Can SKProgressBar show the progress of a "do shell script" command?

I have an AppleScript file that uses curl to download the raw source code data of a website.

I understand that it is possible to use AppleScript's built-in progress bar for a do shell script command. Here's what I mean:

Applescript:

set progress description to "Loading"
set progress additional description to "Please wait"
set progress total steps to 3
repeat with i from 1 to 4
   set theSourceCode to do shell script "curl -L -m 30 seconds http://www.google.com/"
   set progress completed steps to i
end repeat
display dialog "Progress bar is complete."

However, I'd prefer to use the third-party application SKProgressBar, because the SKProgressBar dialog disappears after the progress bar is complete.

Can I use SKProgressBar to properly monitor the state of an instance of do shell script, as above?

Thank you.

Last edited by rubik (2017-05-31 09:58:01 pm)

Offline

 

#2 2017-05-31 11:49:24 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11451
Website

Re: Can SKProgressBar show the progress of a "do shell script" command?

Hi,

Yes it can.

SKProgressBar can be used for any repeat loop with an incrementing counter.

progress total steps corresponds to maximum value
progress completed steps corresponds to current value


regards

Stefan

Offline

 

#3 2017-06-17 03:15:36 am

rubik
Member
Registered: 2017-05-31
Posts: 3

Re: Can SKProgressBar show the progress of a "do shell script" command?

Do you know what I'm doing wrong? I cannot get this code to work as desired:

Applescript:

tell application "SKProgressBar"
   activate
   ---------------------------------------------------
   --    SETUP WINDOW THAT CONTAINS ALL PROGRESS BARS --
   ---------------------------------------------------
   set title to "Downloading source code..."
   
   set floating to false --> default is true
   set position to {450, 650} --> default is {1000, 750}, origin point is bottom left
   set width to 400.0 --> default is 500.0
   set currentValue to 0.0
   set maxValue to 10.0
   
   ---------------------------------------------------
   --    SETUP MAIN PROGRESS BAR --
   -- (progress bar 1 by definition)
   ---------------------------------------------------
   --    It already exists, so no need to create
   
   tell main bar --main bar
       set minimum value to 0.0 --> default is 0.0
       set maximum value to maxValue --> default is 100.0
       set current value to currentValue --> default is 0.0
       -- header / footer properties
       set header to "Please wait" --> default is empty string
       set header alignment to center --> default is left
       set header size to regular --> default is small
       set footer to "footer" --> default is empty string
       set footer alignment to center --> default is left
       set footer size to small --> default is small
   end tell
   set show window to true --> default is false
   tell main bar
       set indeterminate to false
       start animation
   end tell
   set maxValue to 5
   repeat with i from 1 to 5
       set currentValue to i
       tell main bar to set footer to "Getting " & i & " of " & maxValue
       tell main bar to increment by 1
       my resetMainBar(maxValue, currentValue)
               
       set theFullSourceCode to do shell script "curl -L -m 30 seconds " & "http://macscripter.net/"
       
   end repeat
   quit -- This quits the SKProgressBar app, closing all Progress Bars
end tell

on resetMainBar(maxValue, currentValue)
   tell application "SKProgressBar"
       tell main bar
           set maximum value to maxValue
           set current value to currentValue
       end tell
   end tell
end resetMainBar

The issue is that this code makes the curl process take many times longer to complete.

I don't want the SKProgressBar to lengthen the duration of my script at all. I just want it to accurately reflect the do shell script progress in real time, just like AppleScript's built-in progress bar does in my original post.

Thank you.

Offline

 

#4 2017-06-17 03:54:47 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11451
Website

Re: Can SKProgressBar show the progress of a "do shell script" command?

First of all it's not possible (with vanilla AppleScript).

Imagine how progress indicators work. You have a minimum, maximum and current value. To move the bar you change the current value and the framework shows the fraction current / maximum. To be able to change the current value you must have a number of work units and increment the value on completion.

The `curL` operation is such a (single) unit.

However you want to get the progress within this unit. This is not possible because there is no report. Paste the line as literal string (without the concatenation) into Terminal.app and you will get the source code as a single operation but no report about the progress. Reporting the progress dynamically is only possible if the unit runs asynchronously. Due to AppleScript's single-threaded design AppleScript is not able to handle the callbacks.

The Apple progress bars are based on asynchronous tasks providing a mechanism to report the progress dynamically.


regards

Stefan

Offline

 

#5 2017-06-17 02:33:51 pm

rubik
Member
Registered: 2017-05-31
Posts: 3

Re: Can SKProgressBar show the progress of a "do shell script" command?

Okay, thanks for the explanation.

But, if it is "not possible", then why did your first reply say, "Yes it can."?

Offline

 

#6 2017-06-18 11:46:33 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 5023

Re: Can SKProgressBar show the progress of a "do shell script" command?

rubik wrote:

The issue is that this code makes the curl process take many times longer to complete.

No it doesn't. You've increased the number of iterations from 4 in your original script to 5, and you've changed the URL from http://www.google.com/ to http://macscripter.net/, which is much, much slower. Compare like with like.

If you allow for all your set-up code, the difference is tiny. Clean up your code by getting rid of the redundant increment command and stop resetting the maximum value to the same value every time through the loop to reduce the number of Apple events, and stop calling do shell script within the app's scope to avoid an unnecessary error and retry, and I suspect the difference will be all but unnoticable.

Last edited by Shane Stanley (2017-06-18 11:47:06 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#7 2017-06-19 01:36:41 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11451
Website

Re: Can SKProgressBar show the progress of a "do shell script" command?

I wrote:

SKProgressBar can be used for any repeat loop with an incrementing counter.

A single curL operation is a single task and can not be embedded in a repeat loop.


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.066 seconds, 11 queries executed ]

RSS (new topics) RSS (active topics)