tell Finder
Try
set TheProblem to “”
set ThePath to “Mac HD:Desktop”
tell me CreateFolder(ThePath & “ABC”)
tell me CreateFolder(ThePath & “DEF”)
…
on error
tell me TheError(TheProblem)
end try
end tell
On CreateFolder(FolderPath)
set TheProblem to "Creating folder " & FolderPath
tell Finder to make a new folder
End CreateFolder
On TheError
tell FileMaker set ErrorField to TheProblem
end TheError
I want to exit the script if anything goes wrong but I need to set a field in FileMaker to the problem. The problem is I don’t know how to exit the entire script from the “CreateFolder” sub-script.
I tried setting the “TheProblem” variable in the sub-script but that doesn’t seem to be the same variable as the one in the main script.
There are several ways to do this. If you just want to quit the script from a subroutine you can cause an error -128 and trap the error outside the subroutine.
Usually you want to use ‘return’ in your subroutines. This makes it more generic and you can use the same subroutine in other scripts. Something like this:
set main_folder to choose folder
set r to CreateFolder(main_folder)
if r is false then
beep 2
return – from script
end if
beep 3
– returns true if folder was created, false otherwise
on CreateFolder(at_folder)
set s_flag to true
tell application “Finder”
try
make new folder at at_folder with properties ¬
{name:“Sub Folder”}
on error err_mess
display dialog err_mess buttons {“OK”} default button “OK”
set s_flag to false
end try
end tell
return s_flag – from subroutine
end CreateFolder
I don’t think there’s a set best way to do this, but may be mistaken.
My apologies, I guess I wasn’t clear enough on this.
My concern isn’t how to create a folder or how to return data from a sub-script.
My problem is I may have to create a 100 folders which would make it a longs script if the parent script had to check for an error each time the sub-script was called.
I was hoping to stop the entire script dead in it’s tracks if something went wrong EXCEPT I need to get the error text back into FileMaker. This is why I need the sub-script to be able to stop itself and the parent script.
Assuming that couldn’t be done I thought maybe the sub-script could write error text to a variable that existed in the main script. I can’t figure out how to do this either.
I can’t just pass the folders as a list and have them all created at the same time since I need to do other stuff between folder creation.
I think I know what you mean. Something like this:
set main_folder to choose folder
set new_name to “Sub Folder”
try
CreateFolder(main_folder, new_name)
on error TheProblem from o
TheError(TheProblem, o)
end try
on CreateFolder(main_folder, new_name)
tell application “Finder”
try
set new_folder to (make new folder at main_folder with properties ¬
{name:new_name}) as alias
on error err_mess
error err_mess from new_name
end try
return new_folder
end tell
end CreateFolder
on TheError(TheProblem, o)
display dialog (TheProblem & return & “"” & o & “"”)
return
end TheError
I don’t have Filemaker, so had to use the display dialog.
Yes, your script seems to do exactly what I need.
I thought my understanding of apple script was descent but I can’t figure out how your script works. Specifically:
when you call the “on CreateFolder” sub-script and it runs into an error does the line “on error err_mess” put the error into the variable “err_mess”?
if the above is true then what does the line “error err_mess from new_name” do?
does the line “return new_folder” pass an error back to the parent script so that it knows something went wrong?
when the sub-script “CreateFolder” runs into an error, where does it exit the script at “error err_mess from new_name” or at “return new_folder”? If at “return new_folder” how does the line “TheProblem from o” manage to get the error and the folder name?
Thanks in advance.
set main_folder to choose folder
set new_name to “Sub Folder”
try
CreateFolder(main_folder, new_name)
on error TheProblem from o
TheError(TheProblem, o)
end try
on CreateFolder(main_folder, new_name)
tell application “Finder”
try
set new_folder to (make new folder at main_folder with properties ¬
{name:new_name}) as alias
on error err_mess
error err_mess from new_name
end try
return new_folder
end tell
end CreateFolder
on TheError(TheProblem, o)
display dialog (TheProblem & return & “"” & o & “"”)
return
end TheError
Usually, given a chance of creating a folder, it will error with a bad name being used. The name may be in use, too long, or contain bad characters. That’s all I can think of right now. Note that you won’t even have a chance of creating a folder if you try to place it somewhere that is not allowed.
You can read about the error handler in the AppleScriptLanguageGuide.PDF, but here I just want to get the bad name. The line:
on error err_mess
sets err_mess to the error message. I was thinking that the name would be created in the subroutine. For instance, the subroutine might add indices like:
00001-SubFolder
and eventually you would eliminate all errors. But since you wanted to send the error out of the subroutine I sent it out by causing an error:
error err_mess from new_name
err_mess is the error message and the value of new_name is “Sub Folder”. When this error occurs you jump out of the subroutine and the error is trapped in the other error handler outside the subroutine. When the error occurs the statement:
return new_folder
is never reached. I should have placed it outside the tell block. When no error occurs, the subroutine returns a reference to the new folder.
Note that you could have just trapped the error outside the subroutine but you couldn’t get the value of the variable that caused the error if you formed the name in the subroutine.
Eventually, I’d try to get no errors in the subroutine.
You cleared up a few things for me.
I’ve worked with apple script before and new about the “try” and “on error” commands but I didn’t know you could actually capture the error, thinking about it now that makes complete sense.
I wasn’t planning on randomly creating folders, I wanted to store a file structure as separate records in a database and store the actual files somewhere. When I clicked on a record, I wanted that folder structure created on my desktop and the files copied into the new structure so I can work with the files.
Anyway, long way of saying I have a few things to learn.