访问applescript中的属性值

时间:2015-03-04 06:46:41

标签: 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

这将返回每个图形的ID,但我真正想要返回的是密钥对中数据项列表的值。我尝试了很多方法但没有成功。下面是我想要实现的一个例子(但不起作用)。

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”}}

的属性

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

1 个答案:

答案 0 :(得分:0)

[参见编辑,下方]

在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,这意味着如果尝试设置则脚本将中断访问此属性的变量;你会得到一个“未定义的变量”错误。一种解决方法是先获取对象的所有属性,如下所示:

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

在此示例中,您可以随后检查missing valueif 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. 使用try块来捕获您尝试get的密钥/对没有该特定密钥。
  4. 换句话说,如果你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
    

    我希望这是有道理的并且有所帮助。