I have a shell script that restores a hard drive with a DMG image on the startup disk. I’d like to run it from the Applescript but have visual feedback. I normally do this in Disk Utility, but UI scripting seems fairly tedious (restore from image requires drag and drop). The advantage to opening Disk Utility and starting the restore would be that it would have a progress bar that visually tracks progress, but I have no clue how to do this.
So, for now I use ASR, but it’s still not quite working, I get various errors and I’m not sure how to go about it. The problem I run into is that I need to use a wild card in the name of the file, and if I do that with the quoted form of command it ignores the asterisk and doesn’t find the file!
The files are named Basic_US-Canada_EN.dmg or Pro_US-Canada_EN.dmg, I ave the little bit before it choose if it’s basic or pro. This is run on a computer while booted off of an external hard drive named FireWire HD, and it always going to restore an HD named Macintosh HD.
Help! Please!
Thanks in advance!
set mdlNme to (do shell script "system_profiler SPHardwareDataType | grep " & quoted form of "Model Name" & " | awk '{print $3, $4}' ") -- Get model name, strip it down to last two words returned.
repeat until mdlNme does not end with " " -- Remove any whitespaces it may contain.
set mdlNme to characters 1 through ((length of mdlNme) - 1) of mdlNme as string
end repeat
if mdlNme is in {"iMac", "Mac Pro", "MacBook Pro"} then
set imgTyp to "Pro"
else if mdlNme is in {"MacBook", "MacBook Air", "Mac Mini"} then
set imgTyp to "Basic"
end if
do shell script "sudo asr restore -source " & (the POSIX path of (path to startup disk)) & "Demo Assets/Configurations/" & imgTyp & "*EN.dmg" & " -target " & "/Volumes/Macintosh HD" & " -erase -noprompt -verbose | echo" password "****" with administrator privileges
To get the shell to expand a glob pattern but still protect the rest of the string use quoted form of to quote only the non-wildcard parts. The shell will concatenate multiple tokens (each quoted string and the asterisk are tokens) as long as there is no intervening field separator (usually any whitespace).
-- .
set pathPrefix to (the POSIX path of (path to startup disk)) & "Demo Assets/Configurations/" & imgTyp
set pathSuffix to "EN.dmg"
do shell script "sudo asr restore -source " & quoted form of pathPrefix & "*" & quoted form of pathSuffix & " -target " & "/Volumes/Macintosh HD" & " -erase -noprompt -verbose" password "****" with administrator privileges
The above code also takes out the " | echo" from the end of the command. If you are interested in throwing away the output of the command then “>/dev/null” is the normal way to do that. If you want to make sure that the exit code is zero, then “;true” is more idiomatic. Likewise, use “>/dev/null;true” for both effects. To my eye, " | echo" looks like a mistake and might be removed by future maintainers. The more explicit versions seem less likely, to me, to be taken as mistakes.
As for feedback during a shell script, that is harder to do. The problem is that the do shell script AppleScript command does not return until all possible stdout/stderr output has been collected (all the pipes have been closed). If the command provides progress output to stdout (it looks like the “–puppetstrings” option to asr is intended for this type of usage), you could redirect stdout to a file (be sure to send stderr somewhere, too, or do shell script will not return immediately). Then in a loop you could repeatedly analyze the text at the end of the file. Then you have to know when to stop polling the output file, too. Maybe you check for the PID of the spawned asr, maybe the program could recognize it based on the output of asr.
Anyway, I do not think there is a way of displaying a progress bar in plain AppleScript. You would need to write the program as an AppleScript Studio application or bundle an extension that allows for progress bars (I think there is one of these available, but I am not sure).
In addition to Chrys’ comments, do not use ‘sudo’ in do shell script.
You are using ‘with administrator privileges’ so that will take care of any authentication/privilege issues, so sudo can be omitted (and should be because it will fail on certain OS versions).