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 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")
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
.
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
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.