Creating a RAM Disk by choosing the size from dialog

Hello,

within the script editor the following script runs flawless. But starting it from the FastScript Menu results in error code -1. Any ideas?



set sizeInMB to choose from list {"512", "1024", "2048", "4096", "5120"} with title "Size RAMDisk" default items {"2048"}

on createRamDisk(sizeInMB, volumeName)

set sizeInSectors to round sizeInMB * 1024 * 1024 / 512 rounding up

set cmd to "diskutil erasevolume HFS+ '" & "RAMDisk" & "' `hdiutil attach -nomount ram://" & sizeInSectors & "`"

try
	do shell script cmd
	return true
on error
	return false
end try

end createRamDisk

log createRamDisk(sizeInMB, "RAMDisk")

I really doubt that the script run from the Script Editor.
Here, it fails logically with the error :

Résultat :
error “La variable sizeInMB n’est pas définie.” number -2753 from “sizeInMB”

I removed the passed parameter sizeInMB and now it works.

on createRamDisk(volumeName)
	
	set sizeInMB to choose from list {"512", "1024", "2048", "4096", "5120"} with title "Size RAMDisk" default items {"2048"}
	
	set sizeInSectors to round sizeInMB * 1024 * 1024 / 512 rounding up
	
	set cmd to "diskutil erasevolume HFS+ '" & "RAMDisk" & "' `hdiutil attach -nomount ram://" & sizeInSectors & "`"
	
	try
		do shell script cmd
		return true
	on error
		return false
	end try
	
end createRamDisk

log createRamDisk("RAMDisk")

Yvan KOENIG (VALLAURIS, France) dimanche 7 décembre 2014 10:34:39

Hello Yvan,

you are right. While debugging I made a mistake. The part

set sizeInMB to choose from list {"512", "1024", "2048", "4096", "5120"} with title "Size RAMDisk" default items {"2048"}

has to be placed before the handle “createRamDisk”.

I’ve changed the code in my initial post; now it should work - at least from the Script Editor.

Regards,

B.

Hi,

as always, choose from list returns a list.
It might work but it’s better to get the list item explicitly.
It’s also good habit to consider the case when the user presses Cancel


.
set sizeInMB to choose from list {"512", "1024", "2048", "4096", "5120"} with title "Size RAMDisk" default items {"2048"}
if sizeInMB is false then return
set sizeInSectors to round (item 1 of sizeInMB as integer) * 1024 * 2 rounding up
.

Hello Stefan,

thank you for your swift reply. I am getting now another error from FastScript. I expect its an issue from FastScript and not from AppleScript, I opened another thread here http://www.red-sweater.com/forums/discussion/3478/error-number-1717-the-handler-redsweaterscriptingprefersseparateprocess-is-not-defined#Item_1 at developers site.

Edit (after Yvan’s hint):
Saving the script with AppleScript-Editor makes the bet.

Regards,

B.

It seems that you have something wrong in your system.

Here I ran the version below from FastScript.
It behaved flawlessly.

my createRamDisk("RAMDisk")


on createRamDisk(volumeName)
	
	set maybe to choose from list {"512", "1024", "2048", "4096", "5120"} with title "Size RAMDisk" default items {"2048"}
	if class of maybe is not boolean then set sizeInMB to item 1 of maybe
	set sizeInSectors to round sizeInMB * 1024 * 1024 / 512 rounding up
	
	set cmd to "diskutil erasevolume HFS+ '" & "RAMDisk" & "' `hdiutil attach -nomount ram://" & sizeInSectors & "`"
	
	try
		do shell script cmd
		return true
	on error
		return false
	end try
	
end createRamDisk

Yvan KOENIG (VALLAURIS, France) lundi 8 décembre 2014 11:22:34

Hey B.,

It’s a good idea to mention what Script Editor you’re using if it’s not Apple’s Applescript Editor. Script Debugger and Smile each have their quirks, and people on MacScripter probably won’t consider them unless properly informed.

I’ve just run this successfully from FastScripts (on OSX 10.9.5) with the handlers hidden away in my SD test library.


redsweaterscriptingprefersseparateprocess()
createRamDisk("RAMDisk")

-------------------------------------------------------------------------------------------
--» HANDLERS { in a Script Debugger Library }
-------------------------------------------------------------------------------------------
on redsweaterscriptingprefersseparateprocess()
	return true
end redsweaterscriptingprefersseparateprocess
-------------------------------------------------------------------------------------------
on createRamDisk(ramDiskName)
	tell application (path to frontmost application as text)
		set ramDiskSize to choose from list {"512", "1024", "2048", "4096", "5120"} with title "Size RAMDisk" default items {"2048"}
	end tell
	
	if class of ramDiskSize is not boolean then
		set sizeInMB to item 1 of ramDiskSize
		set sizeInSectors to round sizeInMB * 1024 * 1024 / 512 rounding up
		set shCmd to "diskutil erasevolume HFS+ '" & "RAMDisk" & "' `hdiutil attach -nomount ram://" & sizeInSectors & "`"
		try
			do shell script shCmd
			return true
		on error
			return false
		end try
	else
		error "User Cancelled!"
	end if
	
end createRamDisk
-------------------------------------------------------------------------------------------

Pitfalls you need to be especially aware of with Script Debugger:

A) Leaving debugging [ON] in either a script (or attached library) and trying to run the script from somewhere other than Script Debugger or SD’s script menu.

B) Failing to attach the correct library to a script.

C) Failing to refresh a script after changing something in an attached library.

(A couple more I’m forgetting at the moment.)