【问题标题】:applescript elements, classes, and objectsapplescript 元素、类和对象
【发布时间】: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 是具有 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 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" 对象?

【问题讨论】:

    标签: collections applescript


    【解决方案1】:

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

    或者,如果出于某种原因您想使用给定应用程序中的术语而不使用 tell 块,您可以像这样包装您的代码:

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

    【讨论】:

    • 'using terms from' 通常用于可附加的应用程序——即那些可以“从内部”运行脚本的应用程序,通常通过它们有自己的脚本菜单这一事实来表示——BBEdit 就是一个很好的例子。当您从 BBEdit 自己的脚本菜单运行脚本时,BBEdit 是默认的 tell 目标,因此严格来说,脚本运行不需要“tell application”,但是,您仍然需要告诉编译器在哪里可以找到术语,所以它可以编译,因此需要使用块的术语。它们还用于为未安装的应用程序编译脚本。
    • 谢谢布伦南。我想这是您在设计语言并且不想要“导入”语句时需要做的事情。
    【解决方案2】:

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

    【讨论】:

    • 这似乎不能解决问题。当我用repeat with movieind from 1 to movielenset movie to item movieind in Movies 替换repeat with movie in Movies 时,循环在功能上是相同的。 IOW,当我用 name of every keyword in movie 对象说明符注释掉循环的位,然后我包含 name of every keyword in movie 对象说明符时,它仍然有效,它仍然给我关于类/标识符混淆的相同错误。
    【解决方案3】:

    关于脚本 Aperture 关键字

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

    • 关键字的id 是一个字符串,其中包含从关键字名称到其顶级父级的路径,每个父级的名称前面都有一个制表符。
    • parents 属性的值只是没有名称的 id,如果关键字不在顶层,则为名称后面的选项卡。
    • 关键字不比较相等,即使它们的值相同;事实上,keyword 1 of animage != keyword 1 of anImage
    • 一个图像可能有多个具有相同名称的关键字,如果它们有不同的父项。但是,该名称只会在显示图像元数据的任何位置出现一次。
    • anImage 添加关键字aName 和父母theParents 的方法是tell anImage to make new keyword with properties {name:aName, parents:theParents, id:aName&tab&theParents}

    请参阅my entry here 以获得更完整的说明以及添加和删除处理程序。

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 1970-01-01
      • 2011-04-05
      • 2011-10-21
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      相关资源
      最近更新 更多