在 applescript 中访问 属性 值

Accessing property values in applescript

我有一些 AppleScript 代码;

tell application "OmniGraffle"
activate
tell canvas of front window
repeat with obj in graphics
set ObjName to id of obj
display dialog "This is the dialog " & ObjName
end repeat
end tell
end tell

这return是我的每张图的ID,但我真正想要return的是keypair中的一个数据项列表的值。我尝试了很多方法但没有成功。下面是我想要实现的(但不起作用)的示例。

tell application "OmniGraffle"
activate
tell canvas of front window
repeat with obj in graphics
set ObjName to user data value of obj
display dialog "This is the dialog " & ObjName
end repeat
end tell
end tell

感谢任何帮助。提前致谢。

我现在有一个不同的错误;

tell application "OmniGraffle"
    activate
    tell canvas of front window
        repeat with obj in graphics
            set test to user data in graphics
            repeat with value in (properties of test) as list
                display dialog value
            end repeat
        end repeat
    end tell
end tell

我收到的错误是;

无法获取 {{type:"YES"}、{type:"testg"}、{type:"mysql"}、{type:"linux" 的属性}}

我觉得我走在正确的轨道上,但我无法访问密钥对的值:-(

[参见下面的编辑

在 OmniGraffle 中,有一个名为 user data 的 属性,但如果它有缺失值,您将无法获取它(因为您可以获取其他属性) 通过

tell application "OmniGraffle"
  user data of item 1 of graphics
end tell

以下应该适用于获取任何对象的 class

tell application "OmniGraffle"
  class of item 1 of graphics
end tell

如果对象在user data中有值,你应该可以得到这个。问题是,如果您 运行 第一个示例 (user data of ...) 并且没有(丢失)数据,则不会返回任何内容,甚至 missing data 也不会返回,这意味着您的脚本将中断尝试设置然后访问此 属性 的变量;你会得到一个 "variable not defined" 错误。一种解决方法是首先获取对象的所有属性,如下所示:

tell canvas of front window
    set itemOneProps to properties of item 1 of graphics
    set uData to user data of itemOneProps
end tell

在此示例中,您可以随后检查 missing value (if uData = missing data) 并忽略(或更改)它。 您还可以像这样设置用户数据:

set user data of item 2 of graphics to {Status:"green", foobar:"pickle"}

编辑 这解释了您在访问用户数据时要处理的内容:

tell application "OmniGraffle"
    --activate
    tell canvas of front window
        repeat with obj in graphics
            set test to user data in graphics
            --as I mentioned, you don't want to be getting the "properties" of the list; it's just a list (actually, a record, which is a list of properties as key pairs)
            repeat with thisDatum in test --I'm calling it thisDatum; see below
                --you should coerce thisDatum to a list, 
                --then you can repeat loop through this list of values (you lose the keys) and do something with each item
                set thisDatumList to (thisDatum as list) --parentheses a MUST!
                --or you can use "items of thisDatum" to return list of values and also lose keys
                --now I'm using "thisValue" because of {key:value} structure, but NOT using "value" because
                -- that is a keyword and may conflict with code at some point
                repeat with thisValue in thisDatumList
                    thisValue
                end repeat
            end repeat
        end repeat
    end tell
end tell
thisValue

请注意,您正在使用这种强制列表方法丢失用户数据密钥对 ( {key:value} ) 中的密钥。如果你想保持密钥完好无损,你必须:

  1. 不强制列出 (duh)
  2. 在每个对象的密钥对中具有一致的密钥,或者
  3. 当您尝试 get 的 key/pair 没有那个特定的键时,使用 try 块来捕获。

换句话说,如果您执行 set x to myColor of thisKeyPair(在每个 thisDatum 的循环内),则 必须 是用户数据中的 myColor:"redOrSomeValue"。 如果某些对象有 myColor:"redOrSomeValue" 而有些对象没有,您将需要(我知道这很痛苦)在 try 块中捕获错误,例如:

try
    set x to myColor of thisKeyPair
on error
    --ignore or do something else
end try

我希望这有道理并且对您有所帮助。