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:
- Either display a barber pole progress bar while the shell script is running with an option to quit.
- 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
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,
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 scriptcommand – in your code, it is assigned tofin[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
Resourcesfolder (in Finder, select Show Package Contents in the context menu) and using thepath to resourcecommand to call it inside ausing terms fromblock – the documentation I linked to above has details and example code, but, crucially, contains one critical error: yourtellstatement 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
/Usersfolder:– 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 ofof it.physical size– unlike Finder, it works in the background. That will allow you to get rid of thetell application "Finder"andtry … catchblocks (not sure what you meant to achieve by that one anyway – if you were reacting toerror -10004, that is becauserounddoes not like to be put inside atellblock).finto an empty string – you will get a return value fromdo shell script.do shell script, you need to quote thecomputerNamevariable of it will break on spaces in that name.theIconis never used.display alertinstead ofdisplay dialogfor the user confirmation, as it has a nice emphasis on the first part of the message.All together mean your code can be modified to: