构建自己的 AppleScript 数字错误处理 [英] Build own AppleScript numerical error handling
问题描述
我想构建一个应用程序来使用我定义的类似于以下内容的自定义错误号来验证项目:
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
在 JSON Helper 的帮助下,将列表定义为:
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:
其他然后使用臃肿的条件,如:
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
经过研究,我无法填充除AppleScript 中哪些技术可以处理错误以便我可以放置对话框?" 那么 AppleScript 中是否有一种方法可以编写类似于 Error Numbers 和 Error 的错误处理消息文档?
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?
推荐答案
属性列表项
是可能的.
此脚本将记录放入新的属性列表项
使用错误号作为字符串获取属性列表项
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
<小时>
JMichaelTX的问题
这是将属性列表项
保存到PLIST文件(在本例中位于Preferences文件夹中)的脚本.
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
这篇关于构建自己的 AppleScript 数字错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!