我不使用DEVON相关的软件,但是在处理AppleScript记录的普通1情况下,CRGreen的建议将不适用:exists不是一个命令属性理解,尤其是不存在的属性;并且引用不存在的属性会引发错误。
很高兴您正在寻找try...end try 的替代品。我已经看到您以前的代码示例被淹没在其中,当捕获到错误时,它们是昂贵的操作,因此不适合您尝试的操作。 try 在 AppleScript 中根本没有位置。
不是在record 中测试属性的存在,而是在一般情况下解决此问题的方法是创建一个包含所有需要的属性值的record 对象,并分配他们每个人的默认值。
在 AppleScript 中,观察到 record 遵循以下行为:
-
单个record 对象只能包含一个具有给定标识符的属性。如果您尝试插入两个标识相同的属性,编译器将保留第一个属性及其关联值,并清除其余的:
{a:1, b:2, a:3} # will resolve on compilation immediately to {a:1, b:2}
-
两个record 对象可以包含具有共享标识符的属性,如下所示:
set L to {a:1, b:2, c:3}
set R to {d:missing value, c:L}
与list 对象类似,两个record 对象可以合并为一个record,并且属性将被合并:具有每个record 唯一标识符的属性将简单地插入到结果数据结构。如果标识符在合并之前出现在两个 record 对象中,同样,优先级以从左到右的阅读顺序给出,因此以 record 为前缀(在左侧)中的属性将占上风,以 @987654339 为后缀@(右侧)将清除其非唯一的属性标识符(及其值):
L & R --> {a:1, b:2, c:3, d:missing value}
R & L --> {d:missing value, c:{a:1, b:2, c:3}, a:1, b:2}
您的代码 sn-p 包含以下内容:
repeat with g in (children of root of (think window 1))
set theAnnotation to annotation of g
end
因此,g 是包含在 children(list 对象)中的项目,g 的类型类是 record。根据正在检查children 的哪个项目,我假设其中一些项目是确实 包含annotation 标识的属性的记录,而其中一些项目是不包含这样的属性。
但是,请考虑由此合并产生的以下record:
g & {annotation:missing value}
以下是两种可能的情况:
-
g 是一个 record,它已经包含一个标识为 annotation 的属性,例如:
set g to {cannotation:"doe", bannotation:"ray", annotation:me}
g & {annotation:missing value} --> {cannotation:"doe", bannotation:"ray", annotation:«script»}
set theAnnotation to annotation of (g & {annotation:missing value})
--> «script» (i.e. me)
或:
-
g是一个record,其中属性标识符annotation不存在,例如:
set g to {doe:"a deer", ray:"a drop of golden sun"}
g & {annotation:missing value} --> {doe:"a deer", ray:"a drop of golden sun", annotation:missing value}
set theAnnotation to annotation of (g & {annotation:missing value})
--> missing value
因此,对于脚本中使用try...end try 来捕获record 数据结构中未出现的属性的每个位置,只需删除try 块,以及在您分配从推测读取的值的任何位置属性值,人为插入默认值,然后您可以测试并知道该值是来自您的 DEVONthink 源还是来自您的大脑:
tell application id "DNtp"
repeat with g in (children of root of (think window 1))
set theAnnotation to annotation of (g & {annotation:false})
if theAnnotation ≠ false then exit repeat
end
end tell
1这绝不意味着他的解决方案不可行。如果 DEVON 返回的集合没有被取消引用——它很可能会这样做——这些可以作为一个整体进行操作,而不需要遍历单个项目,当然,他使用 DEVON。但是我希望在上面解决的情况是一种更常见的情况,并且在这里也可以使用。