Collections applescript元素、类和对象

Collections applescript元素、类和对象,collections,applescript,Collections,Applescript,我正在写一个applescript来自动化Aperture中一些繁琐的工作,我花了很长时间来理解该语言的一些特性。特别是,我在处理集合(列表和元素)时遇到了很大的困难。我被对象说明符难住了,这些说明符包括集合并将命令应用于集合中的元素。此外,我对对象说明符中的奇怪差异感到困惑,这些对象说明符在类中按类引用对象元素时的行为似乎不同,好吧,让我向您展示一下 这是一个脚本的开头 prop Movies : {} tell Application "Aperture" set Movies

我正在写一个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
光圈中的
应用程序
对象包含
图像版本
s的元素集合<代码>图像版本对象包含
关键字
s的元素集合<代码>关键字s是具有
名称
id
属性的对象/列表

因此,全局脚本属性
Movies
现在包含我的光圈库中所有实际上是视频的
图像版本
对象。现在,当我尝试这样做时:

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'.
...
但是,我一直在研究如何访问那些
图像版本中的元素集合。当我这样做时:

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被电影中每个关键字的对象说明符
名称所混淆。但是,我对此感到困惑的原因是,如果我编写以下代码:

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

...
...
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.
...
谁能告诉我我的对象说明符有什么问题吗?为什么当
图像版本
在一个上下文中时,我基本上可以将
get name
应用于img
中的每个关键字,但我不能在不同的上下文中?这里有我遗漏的东西吗?是不是
关键字
类位于光圈
应用程序
对象的内部?如果是这样的话,我将如何指定类似于
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
给出了预期的输出:

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

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

这里似乎有一个非常特殊的范围问题。有人能给我解释一下,在这个新版本中,
关键字
对象是如何在范围内的,而在以前的版本中,我们不在
告诉
块中,它是如何在范围外的?我以为
tell
块只是用来指导没有直接参数的命令?它们也确定类型范围吗?或者,在对象说明符的构造/执行过程中,是否有一个隐藏在此处的命令,该命令取决于是否被发送到
应用程序“Aperture”
对象?

Applescript很有趣,不幸的是,它经常会被击中或未击中。在本例中,
对列表中的项重复,
,项是对内容的引用,而不是内容本身。通常,AppleScript会为您解除引用,但并不总是这样。通常,我总是使用从1开始重复x进行计数的
方法来避免这个问题。

如果我正确理解了你问题的核心-因为你已经解决了实际部分,扭曲的解引用,这对于applescripter来说有点老生常谈-你只能在相关的tell块中使用术语,因此,如果您的应用程序(光圈)提供术语“关键字”,除非您在
告诉应用程序“光圈”
块中,否则不能引用“关键字”

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

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

关于编写光圈关键字的脚本

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

  • 关键字的
    id
    是一个字符串,其中包含从关键字名称到其顶级父级的路径,每个父级的名称前面都有一个选项卡
  • parents
    属性的值只是没有名称的id,如果关键字不在顶级,则是名称后面的选项卡
  • 关键词值相同,但不进行相等比较;事实上,animage的关键字1anImage的关键字1
  • 如果图像具有不同的父级,则它们可能具有多个同名关键字。但是,无论在何处显示图像的元数据,该名称都只会显示一次
  • 向名为
    aName
    且父母
    theParents
    anImage添加关键字的方法是
    告诉anImage使用属性{name:aName,parents:theParents,id:aName&tab&theParents}创建新关键字

  • 有关添加和删除处理程序的详细说明,请参阅。

    这似乎无法解决问题。当我将
    repeat with movie in Movies
    替换为
    repeat with movieind from 1 to movielen
    ,然后
    将movie设置为item movieind in Movies
    时,循环在功能上是相同的。照此,当我用电影中每个关键字的
    名称对象说明符注释掉循环的一部分,然后我将电影中每个关键字的
    名称包含在对象说明符中时,它仍然有效,关于类/标识符混淆,它仍然给了我同样的错误。“using terms from”通常用于可附加的应用程序,即那些可以“从内部”运行脚本的应用程序,通常由它们有自己的脚本菜单这一事实表示,BBEdit就是一个很好的例子。当您从BBEdit自己的脚本菜单运行脚本时,BBEdit是默认的tell目标,因此严格地说,“tell应用程序”对于脚本的运行是不必要的,但是,您仍然需要告诉编译器在哪里可以找到术语,以便它可以编译,因此需要为块使用术语。它们还用于为未安装的应用程序编译脚本。谢谢Brennan。伊格