Johnny Hieu Le
Johnny Hieu Le

Reputation: 163

Applescript Shell Script Progress

I am creating an applescript that creates a backup image of the /Users folder in Mac Os X. I would like to do one of two things:

  1. Either display a barber pole progress bar while the shell script is running with an option to quit.
  2. Display a dialog box while the script is running with an option to quit.

I have tried doing the shell script with the /dev/null but that ignores all output. I would like to save the output and display it in a dialog for the user.

Here is my code:

set computername to (do shell script "networksetup -getcomputername")
set fin to ""
set theIcon to note
tell application "Finder"
    try
        set folderalias to "/Users"
        set foldersize to physical size of folder (alias ":Users") --(info for folderalias) 
        set foldersize to (round ((foldersize / 1.0E+9) * 100)) / 100
    on error
        tell application "System Events"
            set foldersize to (round (((get physical size of folder ":Users" as text) / 1.0E+9) * 100)) / 100
        end tell
    end try
end tell

display dialog "User profile backup utility" & return & return & ¬
    "The computer name is: " & computername & return & return & ¬
    "The '/Users/' directory size is: " & "~" & foldersize & " GB" & return & return & ¬
    "Would you like to backup the '/User' directory now?" & return ¬
    buttons {"Cancel", "Backup Now"} default button "Backup Now"
set comd to "hdiutil create ~/Desktop/" & computername & ".dmg -srcfolder /test/"
set fin to do shell script (comd) with administrator privileges
display dialog fin

Upvotes: 4

Views: 9352

Answers (2)

kopischke
kopischke

Reputation: 3413

Displaying a progress bar dialog is not possible with on-board AppleScript (i.e. Standard Additions), but this can be achieved using Shane Stanley’s ASObjC Runner, a scriptable faceless background application which provides, among many over useful functions, a set of progress dialog related commands. Once downloaded onto your system,

tell application "ASObjC Runner"
    reset progress
    set properties of progress window to {button title:"Abort Backup", button visible:true, message:"Backing up the '" & (POSIX path of folderalias) & "' directory.", detail:"There are " & foldersize & " GB of data to backup – this might take some time.", indeterminate:true}
    activate
    show progress
end tell
try -- to make sure we destroy the dialog even if the script error out
    <your backup operation here>
end try   
tell application "ASObjC Runner" to hide progress

will show an indeterminate progress bar (or “barber pole”) while the backup operation runs – at least if it is synchronous (as shell commands called from AS are). As to the output of your shell command, that is automatically returned by the do shell script command – in your code, it is assigned to fin [code lifted more or less wholesale from the ASObjC Runner documentation].

ASObjC Runner can be embedded into an AppleScript application (save your script as an application in AppleScript Editor) by putting it into the bundle’s Resources folder (in Finder, select Show Package Contents in the context menu) and using the path to resource command to call it inside a using terms from block – the documentation I linked to above has details and example code, but, crucially, contains one critical error: your tell statement needs to use the POSIX path to the Resources bundle (tell application (POSIX path of (path to resource "ASObjC Runner.app"))).

A few remarks on your code:

  • there is a more elegant way to get an alias to the /Users folder:

    path to users folder
    

    – no need for hardwiring and calls to Finder. You can then get the shell compatible path to that by using POSIX path of, or, if you need it quoted, quoted form of POSIX path of of it.

  • I’d recommend using only System Events to get the physical size – unlike Finder, it works in the background. That will allow you to get rid of the tell application "Finder" and try … catch blocks (not sure what you meant to achieve by that one anyway – if you were reacting to error -10004, that is because round does not like to be put inside a tell block).
  • No need to initialize fin to an empty string – you will get a return value from do shell script.
  • Speaking of your do shell script, you need to quote the computerName variable of it will break on spaces in that name.
  • theIcon is never used.
  • You might want to use display alert instead of display dialog for the user confirmation, as it has a nice emphasis on the first part of the message.
  • There are a lot of unnecessary parentheses in the code – AppleScript needs some of these to delimit semantic command blocks, but not all of them…

All together mean your code can be modified to:

set computerName to do shell script "networksetup -getcomputername"
set folderAlias to path to users folder
tell application "System Events" to set folderSize to physical size of folderAlias
set folderSize to round(folderSize / 1.0E+9 * 100) / 100

display alert "User profile backup utility" message ¬
  "The computer name is: " & computerName & return & return & ¬
  "The '" & (POSIX path of folderAlias) & "' directory size is: " & "~" & folderSize & " GB" & return & return & ¬
  "Would you like to backup the '" & (POSIX path of folderAlias) & "' directory now?" & return ¬
  buttons {"Cancel", "Backup Now"} default button "Backup Now"
set shellCmd to "hdiutil create ~/Desktop/'" & computerName & "'.dmg -srcfolder /test/"
-- progress bar dialog display initialization goes here
try
    set shellOutput to do shell script shellCmd with administrator privileges
end try
-- progress bar dialog hiding goes here
display dialog shellOutput

Upvotes: 5

adayzdone
adayzdone

Reputation: 11238

Although not as pretty as kopischke's suggestion, a quick and dirty way to get progress information is to run the command in the terminal itself.

set comd to "hdiutil create -puppetstrings '~/Desktop/" & computername & ".dmg' -srcfolder '/test/'"
tell application "Terminal" to do script comd

Upvotes: 1

Related Questions