0

I want to download different apps with a bash script on macOS.

As there are some larger downloads (e.g. Office 365) I would like to include a progress bar in a regular macOS Window.

The applications download+install script looks like this

cd ~/Downloads/ && { curl -O https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg ; cd -; }
sudo hdiutil attach ~/Downloads/googlechrome.dmg
sudo cp -r /Volumes/Google\ Chrome/Google\ Chrome.app /Applications/
diskutil unmount Google\ Chrome

But this does not show any progress for the user (script will run in background).

I found the following example and edited it a bit to my likings

chromedl() {
    osascript <<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 [url=https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg]https://dl.google.com/chrome/mac/stable/GGRO/googlechrome.dmg]"
   set progress completed steps to i
end repeat
display dialog "Progress bar is complete."
AppleScript

But this creates a curl execution error.

Please help me with a functioning Curl Download + Progress Bar script

LWun
  • 71
  • 1
  • 11
  • 1
    AppleScript doesn't know the progress of shell scripts - how are you getting values from the download, such as total and current sizes, to use with the progress indicator? Or are you just looking for an indeterminate indicator (spinner)? – red_menace Apr 14 '20 at 16:00
  • @red_menace a progress bar would be ideal, but a spinner would also be a solution. I just want the user to have some visual feedback that the script is working. – LWun Apr 15 '20 at 07:44

1 Answers1

0

A basic progress spinner window is easy enough to create using AppleScriptObjectiveC. Copy the following into Script Editor and run it in the foreground (command-control-R, or use the menu command Script→Run in Foreground, which appears when you hold down the control key).

use framework "AppKit"
use AppleScript version "2.4" -- Yosemite 10.10 or later required
use scripting additions

-- convenience properties to make ASOC more readable
property ca : current application
property NSPanel : class "NSPanel"
property NSView : class "NSView"
property NSProgressIndicator : class "NSProgressIndicator"
property NSTextField : class "NSTextField"

property HUD_mask : 8192
property nonactivating_mask : 128

global progress_panel, progress_indicator, label_field

make_progress_panel()
progress_indicator's startAnimation:me
progress_panel's orderFront:me

-- just hang it out there for 5 seconds as proof of concept
delay 5

progress_panel's |close|()

on make_progress_panel()
    -- make floating panel
    set content_rect to ca's NSMakeRect(200, 800, 140, 80)
    set progress_panel to NSPanel's alloc's initWithContentRect:content_rect styleMask:(HUD_mask + nonactivating_mask) backing:(ca's NSBackingStoreBuffered) defer:true
    set progress_panel's floatingPanel to true

    -- make progress indicator
    set progress_rect to ca's NSMakeRect(0, 0, 40, 40)
    set progress_indicator to NSProgressIndicator's alloc's initWithFrame:progress_rect
    set progress_indicator's indeterminate to true
    set progress_indicator's |style| to 1
    set progress_indicator's translatesAutoresizingMaskIntoConstraints to false

    --make text field
    set label_field to NSTextField's labelWithString:"Downloading"
    set label_field's translatesAutoresizingMaskIntoConstraints to false

    -- make container view, and add subviews
    set content_view to NSView's alloc's initWithFrame:content_rect
    content_view's addSubview:label_field
    content_view's addSubview:progress_indicator
    progress_indicator's sizeToFit()

    set progress_panel's contentView to content_view

    -- view constraints, for alignment
    set pi_y_centering to progress_indicator's centerYAnchor's constraintEqualToAnchor:(content_view's centerYAnchor)
    set lf_y_centering to label_field's centerYAnchor's constraintEqualToAnchor:(content_view's centerYAnchor)
    pi_y_centering's setActive:true
    lf_y_centering's setActive:true

    set lf_left_margin to content_view's leadingAnchor's constraintEqualToAnchor:(label_field's leadingAnchor) |constant|:-10
    lf_left_margin's setActive:true

    set pi_left_margin to label_field's trailingAnchor's constraintEqualToAnchor:(progress_indicator's leadingAnchor) |constant|:-10
    pi_left_margin's setActive:true

    set pi_right_margin to progress_indicator's trailingAnchor's constraintGreaterThanOrEqualToAnchor:(content_view's trailingAnchor) |constant|:10
    pi_left_margin's setActive:true
end make_progress_panel

The simplest way to make this work with curl, I think, is to detach the curl process entirely and recover its pid, like so (the code at the end detaches the process and sends the output to oblivion, then returns the pid):

set curl_pid to do shell script "curl -L [url=...] &> /dev/null & echo $!"

Once you have the pid you can set up a loop (or an idle handler, if you create an app) and periodically check to see if the process is still active:

try
    set b to do shell script "pgrep curl | grep " & curl_pid
on error
    progress_indicator's stopAnimation:me
    progress_panel's |close|()
end try

If you want a determinate progress bar, that's easy enough to manage, but you have to figure out a way to measure progress. This link suggests you can get the headers for a file first and extract the file size; you could compare that against the actual on-disk file size as the download progresses, and feed the ratio into the progress bar.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17