Disk Utility - Restore

Hey everyone, I’m pretty new to applescript and have been working mostly with gui scripting. I’m trying to develop a script that will automate the restoring of a disk image to a volume after deciding if the machine is a “pro” model or not.

It is pretty crude but this is what I have so far


set theInfo to do shell script "/usr/sbin/system_profiler -detailLevel mini | egrep '(Model Name): ' | cut -d: -f2-"
set AppleScript's text item delimiters to return
set theMachine to text items of theInfo
set AppleScript's text item delimiters to "" -- get results
item 1 of theMachine -- Cpu Name

set pro to 0
if theMachine contains " iMac" then
	set pro to 1
else
	if theMachine contains " MacBook Pro" then
		set pro to 1
	else
		if theMachine contains " Mac Pro" then
			set pro to 1
		end if
	end if
end if

if pro = 1 then
	display dialog "Your machine is a Pro Machine."
else
	display dialog "Your machine is a Consumer Machine."
end if

tell application "Disk Utility"
	activate
end tell

delay 1

tell application "System Events"
	keystroke tab
	keystroke tab
	keystroke tab
	keystroke "m"
	tell process "Disk Utility"
		click radio button "Restore" of tab group 1 of splitter group 1 of window "Macintosh HD"
		click button "Image..." of tab group 1 of splitter group 1 of window "Macintosh HD"
	end tell
	keystroke "G" using command down
	keystroke "/Users/phoenixuprising/Desktop"
end tell

I’ve run into two problems which I can’t seem to figure out. The first one is, I can’t seem to get it to click the image button. It says it can’t get it and I don’t see what I am doing wrong. The second problem I’ve run into is, Disk utility seems to only allow the destination of the restore to be set by dragging and dropping a volume into the destination field. I’ve tried to find information on if it’s possible to drag and drop but I’ve come up empty handed. If anyone has any insight, it would be greatly appreciated as I image many computers for hours at a time and I want to put this script in the startup items of our external boot drive to start the process just by booting to it.

Hi,

I guess you don’t need Disk Utility.app, you can do it from command line using the asr command

http://developer.apple.com/macosx/backuponmacosx.html
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man8/asr.8.html

You can gather the machine information faster with


set pro to (do shell script "/usr/sbin/system_profiler SPHardwareDataType | egrep '(Model Name): ' | cut -d: -f2-") contains "Pro"
if pro then
	display dialog "Your machine is a Pro Machine."
else
	display dialog "Your machine is a Consumer Machine."
end if
.

I could do it with the terminal command but for the environment that I’m in I’d prefer to do it within disk utility if possible. I’d also like to know why my code won’t seem to work just for future knowledge. Thanks.

you mentioned the biggest problem.
Obviously you can only specify the destination with drag&drop.

The reason, why you can’t access the Image. button is that the dots aren’t dots but one single character called horizontal ellipsis (⌥.)
Here is a more robust version of your code, which waits until Disk Utility has gathered the disk information


property theDisk : "Macintosh HD"

activate application "Disk Utility"
tell application "System Events"
	tell process "Disk Utility"
		repeat until exists row 1 of outline 1 of scroll area 1 of splitter group 1 of window 1
			delay 0.5
		end repeat
		select (1st row of outline 1 of scroll area 1 of splitter group 1 of window 1 whose value of static text 1 of group 1 is theDisk)
		tell tab group 1 of splitter group 1 of window theDisk
			click radio button "Restore"
			click button "Image."
		end tell
	end tell
	keystroke "G" using command down
	keystroke "/Users/phoenixuprising/Desktop"
end tell

Wow, that makes so much sense now. Thank you. I’ve got two more questions and then I think I will be set for awhile. I can right click the volume and set it as the destination that way so is there a way to script that? then my last question would be if there’s a way to have some type of notification sent when the restore is complete that would initiate a second applescript once. Thanks for all your help.

I don’t know if you could script contextual menus. I doubt it.
I’d like to repeat, you can do the whole thing with the command line also called from an AppleScript, for example


set theSource to "/Users/phoenixuprising/Desktop/myImage.dmg"
set theTarget to "/Volumes/Macintosh\\ HD"
do shell script "/usr/sbin/asr restore --source " & theSource & " --target " & theTarget