4

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
5
  • What if I did the shell script with the output to /dev/null and grab the PID from the shell script. Then I can kill the process with a button?? How would I go about doing that (if possible)? Commented May 10, 2012 at 19:31
  • That is another question entirely – not to turn you down, but on Stack Overflow, you expected to ask different questions quite literally as different questions, so as not to turn questions into multi-issue discussion threads. Even if the questions all relate to the same code :). Commented May 10, 2012 at 20:08
  • quite willing to take a crack at it if you do ask, BTW – it’s an interesting question :). Commented May 10, 2012 at 20:32
  • I've written an tutorial here (well someone else posted it for me) macscripter.net/viewtopic.php?id=34563. It was originally written for Studio application but can be used with AppleScript's of course. Commented May 10, 2012 at 20:49
  • Comment thread morphed into this question. Commented May 10, 2012 at 22:59

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for the code suggestions. It does clean it up a significant amount. I am running this on numerous machines and do not want to have to copy over and run the ASObjC Runner app on each one. I just need a easy way to show that its working or at least have a dialog open while it is working.
@JohnnyHieuLe: you can embed the ASObjC Runner application in your AppleScript, if you distribute it as a Script application – the Readme to the application contains instructions for that. Also, it wil run all by itself when called via tell. If you don’t want that, you are out of luck as to AppleScript – Standard Additions dialogs are all blocking (i.e., they wait for a button click before relinquishing flow control back to the script). See this question – the situation hasn’t improved much since then.
Great if I can run it in the application than that will work for me. I will give it a try and see how it goes. Thanks so much!!! Now my problem is that I want to save the script as an application and be able to run it on a 10.5 machine. I am running 10.7 and when I save the script as a run only application it does not work on the 10.5 machine. Any suggestions?
@JohnnyHieuLe: Lion’s AppleScript Editor generates 64 bit executables. You’ll have to save the script as an application on the Leopard machine.
Comment thread morphed into this question.
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.