applescript元素,类和对象

时间:2011-04-14 18:07:48

标签: collections applescript

我正在编写一个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 version s的元素集合。 image version个对象包含keyword s的元素集合。 keyword是具有nameid属性的对象/列表。

因此,全局脚本属性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 version中的元素集合。当我这样做时:

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)

对我而言,错误听起来像对象说明符name of every keyword in movie混淆了applescript。但是,我对此感到困惑的原因是,如果我写这段代码:

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'.

...

有人能告诉我对象说明符有什么问题吗?当get name在一个上下文中时,为什么我可以基本上将every keyword in img应用于image version,但我不能在不同的上下文中?这里有什么我想念的吗?是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"对象?

3 个答案:

答案 0 :(得分:2)

如果我正确地理解了你的问题的核心 - 因为你已经解决了实际的部分,搞砸了dereferencing,这对于applescripters来说有点旧板栗 - 你必须只在相关的tell块中使用术语,所以如果你的应用程序(Aperture)提供术语“关键字”,除非您位于tell application "Aperture"块内,否则不能引用“关键字”。

或者如果由于某种原因你想在不使用tell块的情况下使用给定应用程序中的术语,你可以像这样包装你的代码:

using terms from application "Aperture"
  -- blah blah about 'keyword' and other Aperture terminology
end using terms from

答案 1 :(得分:0)

不幸的是,Applescript非常有趣,经常被它击中或错过。在这种情况下repeat with item in list,item是对内容的引用,而不是内容本身。 AppleScript通常会为您取消引用,但并非总是如此。通常我总是使用repeat with x from 1 to count方法来避免这个问题。

答案 2 :(得分:0)

关于脚本Aperture关键字

Aperture 3中的关键字脚本非常不直观。以下是我学到的东西:

  • 关键字的id是一个字符串,其中包含从关键字名称到其顶级父级的路径,每个父级的名称前面都有一个标签。
  • parents属性的值只是没有名称的id,如果关键字不在顶层,则名称后面的选项卡。
  • 即使关键字的值相同,关键字也不会相等;事实上,keyword 1 of animage != keyword 1 of anImage
  • 如果图像具有不同的父项,则该图像可能具有多个具有相同名称的关键字。但是,只有在显示图像元数据的任何位置时,名称才会显示一次。
  • 将关键字添加到名称为anImage且父级aName的{​​{1}}的方式为theParents

有关更全面的解释以及添加和删除处理程序,请参阅my entry here