我可以查看AppleScript对象是否具有某个属性?

时间:2011-08-04 08:27:13

标签: properties applescript bbedit

我正在尝试编写BBEdit脚本,让我感觉更像是来自TextMate。我需要做的一件事是看看我引用的对象是否具有特定属性。

例如:

tell application "BBEdit"
    tell front window
        get selected items
    end tell
end tell

这将在项目窗口上成功,但不会在磁盘浏览器窗口上成功,因为后者没有“选定项目”属性。如何查看对象中是否存在此类属性?

请注意:我知道如何在脚本编辑器中检查对象(获取属性)以查看它具有哪些属性,但我需要在运行时知道它们是什么。

4 个答案:

答案 0 :(得分:1)

我没有bbedit所以我无法检查,但是如果存在不同类型的窗口,并且每种类型的窗口都有不同的属性,那么你不能先检查窗口类型吗?然后你就会知道你可以获得什么类型的属性。必须有一个窗口的基本属性,告诉你它的类型或种类或任何可以帮助你做出决定。

答案 1 :(得分:1)

班级怎么样?

tell application "BBEdit"
  if class of window 1 is disk browser window then
    # ...
  else
    # ...
  end if
end tell

答案 2 :(得分:0)

到目前为止,我唯一的解决方案是将其包装在错误处理程序中:

try
    set sel to selected items
on error errMsg number errNum
    if errNum is -1700 then
        -- Code that handles no selected items attribute
        return
    end
    error errMsg number errNum
end try
-- Code that handles when selected items attribute exists

答案 3 :(得分:0)

BBEdit中的documentswindows之间存在差异。 Windowsdocuments的元素,但只有windows具有selection属性,因此您可以先检查窗口的类型,避免完全捕获错误(并使更清洁代码结果)。

此外,尝试使用selection属性,这是BBEdit中的硬属性而不是“选定项目”,因为selection将始终返回可用对象,即使只有insertion point