问题
I'm wanting to build an app for validating projects using custom error numbers I've defined similar to:
try
## do something
on error number -2700
display dialog "Foobar"
end try
with the help of JSON Helper defining the list as:
tell application "JSON Helper"
set myJSON to make JSON from {-1232:"missing definition", -123231:"foo", -1232314:"bar" }
return myJSON
end tell
however I do not see a way to do this after referencing:
- Error Numbers and Error Messages
- Working with Errors
other then using a bloated conditional like:
try
open for access file "MyFolder:AddressData" with write permission
on error msg number n from f to t partial result p
if n = -49 then -- File already open error
display dialog "I'm sorry but the file is already open."
else
error msg number n from f to t partial result p
end if
end try
After researching I was unable to populate anything other than "What techniques work to handle errors in AppleScript so I can place a dialog?" so is there a way in AppleScript I can write error handling similar to Error Numbers and Error Messages documentation?
回答1:
It's possible with a property list items
.
This script put a record in a new property list item
Use the error number as string to get the value in the property list items
set myRecord to {|-1232|:"missing definition", |-123231|:"foo", |-1232314|:"bar", |-49|:"I'm sorry but the file is already open.", |-43|:"This file wasn’t found."}
tell application "System Events" to set myPlist to make new property list item with properties {kind:record, value:myRecord}
try
open for access file "MyFolder:AddressData" with write permission
on error number n
tell application "System Events" to set r to value of first property list item of myPlist whose its name is (n as text)
display alert r
end try
Question of JMichaelTX
Here's a script to save a property list items
to a PLIST file (In the Preferences folder in this example).
set plistPath to (path to preferences folder as string) & "errorsMsgs.plist"
tell application "System Events"
set myPlist to make new property list item with properties {kind:record, value:myRecord}
make new property list file with properties {contents:myPlist, name:plistPath}
end tell
来源:https://stackoverflow.com/questions/35734698/build-own-applescript-numerical-error-handling