【发布时间】:2011-09-25 00:23:30
【问题描述】:
我正在编写一个 applescript 来自动化 Aperture 中的一些乏味工作,而且我在理解该语言的一些特性方面非常费时。特别是,我在处理集合(列表和元素)时遇到了非常困难的时间。我被包含集合和将命令应用于集合中元素的对象说明符难住了。此外,我对对象说明符的奇怪差异感到困惑,这些说明符在类中的类引用对象元素时似乎表现不同,好吧,让我向您展示。
这是脚本的开头。
prop Movies : {}
tell Application "Aperture"
set Movies to every image version whose name of every keywords contains "Movie"
end tell
length of Movies -- result => 601
Aperture 中的application 对象包含image versions 的元素集合。 image version 对象包含 keywords 的元素集合。 keywords 是具有 name 和 id 属性的对象/列表。
因此,全局脚本属性 Movies 现在包含我的 Aperture 库中所有实际上是视频的 image version 对象。现在,当我尝试这样做时:
repeat with movie in Movies
log ("Movie name is '" & name of movie & "'.")
log (" the class of this object is '" & class of movie & "'.")
end
正如预期的那样,输出是:
Movie name is 'MOV03510.MPG'.
the class of this object is 'image version'.
Movie name is 'MOV00945'.
the class of this object is 'image version'.
Movie name is 'MOV03228.MPG'.
the class of this object is 'image version'.
Movie name is 'MOV02448'.
the class of this object is 'image version'.
...
但是,我不知道如何访问 image versions 中的元素集合。当我这样做时:
repeat with movie in Movies
log ("Movie name is '" & name of movie & "'.")
log (" the class of this object is '" & class of movie & "'.")
set kwnamelist to name of every keyword in movie
if kwnamelist contains "Movie"
log (" Yes, '" & name of movie & "' is indeed a video.")
end if
end
给我
find_videos.applescript:1089:1096: script error: Expected class name but found identifier. (-2741)
在我看来,这个错误听起来像是 applescript 被对象说明符 name of every keyword in movie 弄糊涂了。但是,我对此感到如此困惑的原因是,如果我编写此代码:
tell Application "Aperture"
repeat with imageind from 1 to 1000
set img to item imageind of image versions
tell me to log ("Image name is '" & name of img & "'.")
tell me to log (" the class of this object is '" & class of img & "'.")
set kwnamelist to name of every keyword in img
if kwnamelist contains "Movie"
tell me to log (" '" & name of img & "' is actually a video!")
end if
end
end tell
然后我得到预期的输出:
...
Image name is 'DSC_4650'.
the class of this object is 'image version'.
Image name is '104-0487_IMG'.
the class of this object is 'image version'.
Image name is 'MOV02978.MPG'.
the class of this object is 'image version'.
'MOV02978.MPG' is actually a video!
Image name is '108-0833_IMG'.
the class of this object is 'image version'.
...
谁能告诉我我的对象说明符有什么问题?为什么当image version 在一个上下文中时,我基本上可以将get name 应用于every keyword in img,但我不能在不同的上下文中?我在这里缺少什么吗? keyword 类是 Aperture application 对象的内部吗?如果是这种情况,我将如何指定 application::keyword 之类的内容?
更新:
我已经解决了这个特殊的问题,但是如果有人能准确地解释为什么这解决了它,我将非常感激。我这样做了:
tell Application "Aperture"
repeat with movie in Movies
tell me to log ("Movie name is '" & name of movie & "'.")
tell me to log (" the class of this object is '" & class of movie & "'.")
set kwnamelist to name of every keyword in movie
if kwnamelist contains "Movie"
tell me to log (" Yes, '" & name of movie & "' is indeed a video.")
end if
end
end tell
给出预期的输出:
...
Movie name is 'IMG_0359'.
the class of this object is 'image version'.
Yes, 'IMG_0359' is indeed a video.
Movie name is 'MOV02921.MPG'.
the class of this object is 'image version'.
Yes, 'MOV02921.MPG' is indeed a video.
Movie name is 'MOV02249'.
the class of this object is 'image version'.
Yes, 'MOV02249' is indeed a video.
...
这里似乎存在一个非常特殊的范围问题。有人可以向我解释一下keyword 对象在这个新版本中是如何在范围内的,但在之前版本中我们不在tell 块中的范围之外吗?我认为tell 块只是用于指导没有直接参数的命令?他们是否也确定类型范围?或者在对象说明符的构造/执行中是否隐藏了一个命令,该命令取决于发送到Application "Aperture" 对象?
【问题讨论】: