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

时间:2016-03-01 22:16:07

标签: json macos applescript

我想构建一个应用程序,使用自定义错误号来验证项目我已经定义类似于:

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

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

1 个答案:

答案 0 :(得分:1)

可以使用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 文件的脚本(在此示例中位于偏好设置文件夹中)。

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