Would like to display a progress bar in an apple script while a shell script is running

rdagel
New Contributor II

I have an AppleScript that I am using to install High Sierra from a disk image. I would like to display a progress bar (just to show work is being done, not to show how far along the process is.

I had tired making my own progress bar using apple script, but it keeps running and then will not let the shell script run. Here is my shell script

display dialog "Please Make Sure You Have Installed the FirmwareUpdateStandalone Package on the Computer Before You Continue"
display dialog "Click Ok to Install macOS High Sierra"
set thePassword to "password" as string

do shell script ¬ "sudo asr restore -s /Users/macadmin/Documents/10.13.2.apfs.dmg -t /dev/disk0s2 --erase --noprompt" password thePassword with administrator privileges

do shell script "diskutil mount /dev/disk1s1"

display dialog "High Sierra has been Installed. Go to Startup Disk in System Preferences and select the Macintosh HD as the Startup Disk and Restart"

quit

9 REPLIES 9

alexjdale
Valued Contributor III

I use CocoaDialog for this. You can create the progress bar and then send it updates to modify the display text as the script proceeds. I do it in bash but you can certainly send the commands from any shell, I think.

rdagel
New Contributor II

I found That in a google search but the documentation link was broken so I did not know how to use it.

bartlomiejsojka
Contributor

Here's a sample from one of my scripts, that should give you an idea. Note that it's for version 3.0 beta 7.

#!/bin/bash

ICON="yourPathToIcon.icns"
CD="yourPathTo/CocoaDialog.app/Contents/MacOS/cocoaDialog"
STEPS=yourIntegerNumberOfSteps

for (( i = 1; i <= $((STEPS+1)); i++ )); do
    if (( $i <= $STEPS )); then
        PERCENT=$(echo "scale=1;$i*100/$((STEPS+1))" | bc)
        echo "$PERCENT Executing step $i of $((STEPS))"

        # Your code for step $i goes here. Do not echo to stdout within it.

        sleep 0.2 # Optional sleep, for the progress to be noticeable in case of fast code execution: 
    else
        echo "100 All steps completed."
        sleep 0.5 # Recommended sleep, for the 100% state to be noticeable as well:
    fi
done > >($CD progressbar --icon-file "$ICON" --icon-size 32 --width 500 --no-float --title "Progress bar title")

marklamont
Contributor III

we use a little python script to display one, written by my colleague @kapakra you feed a txt file with the text you want to display and a percentage progress and the bar moves across. setting percentage to more than 101 kills the bar.
first line is title
second line percentage
third is text to display.
just keep updating the values required as you go along.
Make sure you populate the file before you call the script

#!/usr/bin/env python import os, sys class ProgressBar: #path to cocooadialog CD_PATH = "/Applications/Utilities/cocoaDialog.app/Contents/MacOS/CocoaDialog" def init(self, title="Progress", message="", percent=0): """Create progress bar dialog""" template = "%s progressbar --posX 'left' --posY 'top' --title '%s' --text '%s' --percent %d --icon-file '/mycustomicons/myicon.icns'" #you can use any icns file. self.percent = percent self.pipe = os.popen(template % (ProgressBar.CD_PATH, title, message, percent), "w") self.message = message def update(self, percent, message=False): """Update progress bar (and message if desired)""" if message: self.message = message # store message for persistence self.pipe.write("%d %s " % (percent, self.message)) self.pipe.flush() def finish(self): """Close progress bar window""" self.pipe.close() if name == "main": import time, os percent = 0 TMP_FILE = "message.txt" # get the prams file in the scripts working dir. f = open(TMP_FILE,"r") #opens tmp file tmptitle = f.readline().split(',') bar = ProgressBar(title=tmptitle[0]) #gets first line for title. while (percent < 101): # set to 101 to allow for a 100% full bar. f = open(TMP_FILE,"r") tmpvals = f.readline().split(',') percent=int(tmpvals[2]) # convert the txt on the 3rd line to an number. bar.update(percent, tmpvals[1]) time.sleep(.5) #this is the update speed, this gives as good as real time. bar.finish()

rdagel
New Contributor II

One more question. I found the Cocadiglog get hub. Do you have to build it in Xcode then to made it into an app?

marklamont
Contributor III

@rdagel looks like you do now, the v2 download link has gone. I have put a zip of the one I use currently (10.12 and down, not tried 10.13) on my github if you want it

swapple
Contributor III

could this be applied to a script that uses curl to download a file and show the end user the progress of the download?

joshuasee
Contributor III

For the benefit of those stumbling over this in the future, AppleScript in macOS 10.10 gained the ability to display progress bars, which Apple has given the level of publicity one would expect of a CIA black ops mission. Save the following as an AppleScript applet and run it. Note that it doesn't produce the same result in osascript or Script Editor.

set progress description to "Isn't stuff great?"
set progress total steps to "2"
set progress additional description to "Starting stuff."
delay 1
set progress completed steps to "0"
set progress additional description to "Doing stuff."
delay 1
set progress completed steps to "1"
set progress additional description to "Doing more stuff."
delay 1
set progress completed steps to "2"
delay 2

FritzsCorner
Contributor III

@joshuasee

Thanks for this! I will have to mess around with it a bit.