首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问applescript中的属性值

访问applescript中的属性值
EN

Stack Overflow用户
提问于 2015-03-04 06:46:41
回答 1查看 3.5K关注 0票数 0

我有一些AppleScript代码;

代码语言:javascript
复制
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,但我真正想返回的是键盘中数据项列表的值。我试过很多种方法,但都没有成功。下面是我想要达到的目标(但不起作用)的一个例子。

代码语言:javascript
复制
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

任何帮助都是非常感谢的。提前谢谢。

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

代码语言:javascript
复制
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”}的属性

我觉得我在正确的轨道上,但我不能访问密钥对的值:-

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-04 18:04:07

请参阅下面的编辑

在OmniGraffle中,有一个名为user data的属性,但是如果该属性缺少一个值,您将无法通过以下操作获得它(就像您能够获得其他属性一样)

代码语言:javascript
复制
tell application "OmniGraffle"
  user data of item 1 of graphics
end tell

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

代码语言:javascript
复制
tell application "OmniGraffle"
  class of item 1 of graphics
end tell

如果对象在user data中有一个值,您应该能够得到这个值。问题是,如果您运行第一个示例(user data of ...),并且没有(缺少)数据,则不会返回任何数据,甚至不返回missing data,这意味着如果脚本试图设置然后访问该属性的变量,则脚本将中断;您将得到一个“变量未定义”错误。一个解决办法是首先获取对象的所有属性,如下所示:

代码语言:javascript
复制
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)并忽略(或更改)它。您还可以像这样设置用户数据:

代码语言:javascript
复制
set user data of item 2 of graphics to {Status:"green", foobar:"pickle"}

编辑--这说明了在访问用户数据时所处理的问题:

代码语言:javascript
复制
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. 不是强迫列入名单(杜恩)
  2. 在每个对象的密钥对中有一致的键,或者
  3. 当您想要get的密钥/对没有该特定键时,请使用try块捕捉。

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

代码语言:javascript
复制
try
    set x to myColor of thisKeyPair
on error
    --ignore or do something else
end try

我希望这是有意义的,也是有帮助的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28848170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档