Disk Copy and User Input..

Hi all,

     I have a question, I would like to use someone else's script but add on to it. I would like to have a dialog box pop up stating "Name Of Disk?" and the user would type it in. Then I would like "Size of Disk?" to show up for the user to type in (Sparse Image if needed). Then I would like Disk Copy's shell coding to make the image on the desktop using the "cd" code. Here is the script...
set newDiskName to display dialog "Enter Disk Name?" default answer "" buttons {"OK"} default button 1
set newDiskSize to display dialog "Enter Disk Size?" default answer "" buttons {"OK"} default button 1
set shellCommand to "cd ~/Desktop; hdiutil create " & newDiskName & " -size " & newDiskSize & " -type SPARSE -fs HFS+ -volname " & newDiskName

do shell script shellCommand

I get this error when executed…

“AppleScript Error… Can’t make {text returned:“Test”, button returned:“OK”} into type string.”

I know I am doing something wrong but any help would be greatly appreciated.

Thank You,
Chris D.

Model: MacBook
AppleScript: 2.11
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Chris,

as you can see in the error message, display dialog returns a record (text returned and button returned), not a string
This does it:

set newDiskName to text returned of (display dialog "Enter Disk Name?" default answer "" buttons {"OK"} default button 1)
set newDiskSize to text returned of (display dialog "Enter Disk Size?" default answer "" buttons {"OK"} default button 1)
set shellCommand to "cd ~/Desktop; hdiutil create " & newDiskName & " -size " & newDiskSize & " -type SPARSE -fs HFS+ -volname " & newDiskName

What you have put down has solved the problem except for the “do shell script shellCommand” thing, that gets the thing going…

Take a look at my revised one…

set newDiskName to text returned of (display dialog "Enter Disk Name?" default answer "" buttons {"OK"} default button 1)
set newDiskSize to text returned of (display dialog "Enter Disk Size? Insert letter m after the numerical value entered. E.X. 200m." default answer "" buttons {"OK"} default button 1)
set shellCommand to "cd ~/Desktop; hdiutil create " & newDiskName & " -size " & newDiskSize & " -type SPARSE -fs HFS+ -volname " & newDiskName
do shell script shellCommand

What I would like to happen, by the way, yours worked perfectly when the “do shell script shellCommand” was added…, I would like when i put the disk size, say 300mb I have to prompt the user to put 300 and the letter “m” after it. It would come out to be 300m for the thing to work. Do you know of a way where it automatically inserts the letter “m” after the numerical value has been entered?

Thanks for your time and willingness to help me in this matter,

        Chris D.

P.S. - If the user doesn’t place the “m” after the numerical value, AppleScript shows an error "hdiutil: create: unable to read -size parameter in “10”. I chose 10mb as an example.

Model: MacBook
AppleScript: 2.11
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

This is quite easy:


...
set newDiskSize to text returned of (display dialog "Enter Disk Size? Insert letter m after the numerical value entered. E.X. 200m." default answer "" buttons {"OK"} default button 1)
if newDiskSize does not end with "m" then set newDiskSize to newDiskSize & "m"
...

StefanK,
Thank you for all your help along my path to learn how to script! I really appreciate the help you have provided. There is one last question I have. Lets say the user inputs “Test” as the disk image, and for some reason there already is a “Test” on the desktop, could there be someway to inform the user without the complication of the AS error like a friendly “There is already a folder named newDiskName on the desktop” (&foldername could be newDiskName). Or have AS overwrite it or something like append it??

Sorry if what I just typed is confusing…

Thanks again for all your help,

              Chris D.

Model: MacBook
AppleScript: 2.11
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Chris,

there are a few possibilities to do this
for example

repeat
	set newDiskName to text returned of (display dialog "Enter Disk Name?" default answer "" buttons {"OK"} default button 1)
	try
		((path to desktop as Unicode text) & newDiskName & ".sparseimage") as alias -- tests if file already exists
		display dialog "A File with name " & quote & newDiskName & quote & " already exists." & return & "Please choose a different one" giving up after 3
	on error
		exit repeat
	end try
end repeat
set newDiskSize to text returned of (display dialog "Enter Disk Size? Insert letter m after the numerical value entered. E.X. 200m." default answer "" buttons {"OK"} default button 1)
if newDiskSize does not end with "m" then set newDiskSize to newDiskSize & "m"
set shellCommand to "cd ~/Desktop; hdiutil create " & newDiskName & " -size " & newDiskSize & " -type SPARSE -fs HFS+ -volname " & newDiskName
do shell script shellCommand

StefanK,
One last question…
Could the value (size of the disk image) be made to an exact image or keep it at Sparse?

Here’s the finished code if you would like a copy :slight_smile:

repeat
	--Sets Name of disk that user picks
	set newDiskName to text returned of (display dialog "Enter Disk Name?" default answer "" buttons {"OK"} default button 1)
	try
		((path to desktop as Unicode text) & newDiskName & ".sparseimage") as alias -- tests if file already exists
		display dialog "A File with name " & quote & newDiskName & quote & " already exists." & return & "Please choose a different one" giving up after 3
	on error
		exit repeat
	end try
end repeat
set newDiskName to text returned of (display dialog "Enter Disk Name?" default answer "" buttons {"OK"} default button 1)
--Sets size of disk that user picks + "m"
set newDiskSize to text returned of (display dialog "Enter Disk Size?" default answer "" buttons {"OK"} default button 1)
--If user didn't put "m" after numerical value entered, "m" is added
if newDiskSize does not end with "m" then set newDiskSize to newDiskSize & "m"

--Makes image with specificied properties on the desktop
set shellCommand to "cd ~/Desktop; hdiutil create " & newDiskName & " -size " & newDiskSize & " -type SPARSE -fs HFS+ -volname " & newDiskName
do shell script shellCommand

Thanks,
Chris D.

Model: MacBook
AppleScript: 2.11
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

You already had your last question ;):smiley:

Sorry, I don’t know, my knowledge about handling images is not very good

To set it as a fixed size the image needs to be a dmg. to do that change -type SPARSE to -type UDIF

Also Since you sharing the script I took the liberty of adding a Gigabyte option.
And took out the extra “set newDiskName to text …” line you had.
**EDIT.
Just realised that the giving up after 3 let the script continue. so fixed that.

(* http://bbs.applescript.net/viewtopic.php?pid=79643*)
start()
on start()
	
	--Sets Name of disk that user picks
	set newDiskName to ""
	repeat until newDiskName is not ""
		try
			
			set newDiskName to text returned of (display dialog "Enter Disk Name?" default answer "" buttons {"OK"} default button 1)
			try
				((path to desktop as Unicode text) & newDiskName & ".dmg") as alias -- tests if file already exists
			on error
				get_size(newDiskName)
				exit repeat
				
			end try
			set disk_check to display dialog "A File with name " & quote & newDiskName & quote & " already exists." & return & "Please choose a different one" buttons {"Cancel", "OK"} default button 2 giving up after 3
			set the button_pressed to the button returned of the result
			if the button_pressed is "OK" then
				
				start()
			else
				exit repeat
			end if
		on error -128
			
			exit repeat
			
			
		end try
	end repeat
	
end start

on get_size(newDiskName)
	set newDiskSize to display dialog "Enter Disk Size?" default answer "10" buttons {"Cancel", "GigaBytes (GB)", "MegaBytes (MB)"} default button 3
	copy the result as list to {text_returned, button_pressed}
	
	if button_pressed is "MegaBytes (MB)" then
		set newDiskSize to text_returned & "m"
		my command(newDiskSize, newDiskName)
	else if button_pressed is "GigaBytes (GB)" then
		set newDiskSize to text_returned & "g"
		my command(newDiskSize, newDiskName)
	end if
end get_size
on command(newDiskSize, newDiskName)
	--Makes image with specificied properties on the desktop
	set shellCommand to "cd ~/Desktop; hdiutil create " & newDiskName & " -size " & newDiskSize & " -type UDIF -fs HFS+ -volname " & newDiskName
	do shell script shellCommand
end command

Thanks StefanK for walking me through the basics and intermediate of this problem, It works for making a sparse image!

Marke hunte, when I input “Test” in “Enter Disk Name?” dialog, the script ends… Any way to make the script continue? (BTW, there is no error message when the script ends or any result…)

Thanks,
Chris D.

Yer sorry my Bad,

I have made a quick edit to get you going, but I am not happy with all of it.
So I will re do it shortly. ( sat. night )

Edit
Ok I think I got the error checking ok now. the script above has been edited with the changes.