Build own AppleScript numerical error handling

て烟熏妆下的殇ゞ 提交于 2019-12-24 00:44:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!