构建自己的 AppleScript 数字错误处理

Build own AppleScript numerical error handling

我想构建一个应用程序来使用我定义的类似于以下内容的自定义错误编号来验证项目:

try
    ## do something
on error number -2700
    display dialog "Foobar"
end try

借助 JSON Helper 将列表定义为:

tell application "JSON Helper"
    set myJSON to make JSON from {-1232:"missing definition", -123231:"foo", -1232314:"bar" }
    return myJSON
end tell

但是我在参考之后没有看到这样做的方法:

其他然后使用臃肿的条件,如:

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

经过研究我无法填充除“What techniques work to handle errors in AppleScript so I can place a dialog?”以外的任何内容,那么在 AppleScript 中是否有一种方法可以编写类似于错误编号和错误消息文档的错误处理?

使用 property list items 是可能的。

这个脚本在新的 property list item

中添加了一条记录

使用错误号作为字符串获取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的问题

这是一个将 property list items 保存到 PLIST 文件的脚本(在本例中位于 Preferences 文件夹中)。

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