我有一些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”}的属性
我觉得我在正确的轨道上,但我不能访问密钥对的值:-
发布于 2015-03-04 18:04:07
请参阅下面的编辑
在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 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} )中的键正在丢失。如果你想保持钥匙完好无损,你必须:
get的密钥/对没有该特定键时,请使用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我希望这是有意义的,也是有帮助的。
https://stackoverflow.com/questions/28848170
复制相似问题