2011-04-14 61 views
0

我正在寫一個applescript來自動化Aperture中的一些繁瑣的工作,而且我有時間理解語言的一些特性。特別是,我在處理集合(列表和元素)方面遇到了很多困難。我被包含集合的對象說明符難住,並將命令應用於集合中的元素。此外,我對對象指定符中的奇怪差異感到困惑,這些差異在類中引用對象元素時表現得有所不同,好吧,讓我給你看。applescript元素,類和對象

這是一個腳本的開始。

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 

application對象在光圈包含image version秒的元素集合。 image version對象包含keyword s的元素集合。 keyword s是具有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) 

的錯誤,對我來說,聽起來像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 nameevery keyword in img,但是我不能在不同的上下文中嗎?有什麼我在這裏失蹤? keyword類是否在Aperture application對象內部?如果情況如此,我將如何指定類似application::keyword的東西?

UPDATE:

解決這個特定的問題,但我會很感激,如果有人可以確切地解釋爲什麼這解決它。我這樣做:

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"對象?

回答

2

如果我正確地理解了你的問題的核心 - 因爲你已經解決了實際問題,那麼對於蘋果手編寫者來說,這是一個古老的栗子 - 你只能在相關的tell塊中使用術語,所以如果你的應用程序(Aperture)提供術語「關鍵字」,除非您處於tell application "Aperture"區塊內,否則您不能引用「關鍵字」。

或者如果你想使用的術語從給定的應用程序,而無需使用訴說塊某種原因,你可以用你的代碼是這樣的:

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

'使用來自'的術語'通常用於可附着的應用程序 - 即那些可以從內部運行腳本'的應用程序',通常通過它們具有它們自己的腳本菜單來指示--BBEdit就是一個很好的例子。當您從BBEdit自己的腳本菜單運行腳本時,BBEdit是默認的告訴目標,所以嚴格來說,「告訴應用程序」對於腳本運行是不必要的,但是,您仍然需要告訴編譯器在哪裏找到術語,所以它可以編譯,因此需要使用塊來使用術語。它們也用於編譯未安裝應用程序的腳本。 – brennanyoung 2011-05-30 04:58:01

+1

謝謝布倫南。我想這是你在設計一門語言時需要做的事情,而且你不需要「導入」語句。 – 2011-05-30 19:58:38

0

不幸的是,這樣的applescript很滑稽,而且經常出現這種情況。在這種情況下,repeat with item in list,item是對內容的引用,而不是內容本身。通常情況下,AppleScript會爲您解除引用,但並非總是如此。通常我總是使用repeat with x from 1 to count方法來避免這個問題。

+0

這似乎不能解決問題。當我用'用movieind重複一次movieind'重複電影中的'repeat',然後'將電影設置爲電影中的項目movieind'時,循環在功能上是相同的。 IOW,當我用'電影中的每個關鍵字的名稱'對象說明符註釋掉循環的位時,它仍然有效,然後我將'每個關鍵字的名稱包含在電影'對象說明符中,它仍然給我提供相同的錯誤關於類/標識符的混淆。 – 2011-04-14 18:59:52

0

關於腳本光圈關鍵字

關鍵詞Aperture 3中的腳本編寫非常不直觀。以下是香港專業教育學院瞭解到:

  • 關鍵字的id是從關鍵字名的路徑達到其頂級父的字符串,由製表之前每個父母的名字。
  • parents屬性的值只是沒有名稱的標識,如果關鍵字不在頂層,則是名稱後面的標籤。
  • 即使它們的值相同,關鍵字也不會相等;實際上,keyword 1 of animage != keyword 1 of anImage
  • 如果圖像具有不同的父項,圖像可能具有多個具有相同名稱的關鍵字。但是,名稱只會在圖像元數據顯示的位置出現一次。
  • 的方式來添加關鍵字anImage與名aName和家長theParentstell anImage to make new keyword with properties {name:aName, parents:theParents, id:aName&tab&theParents}

一個更全面的解釋請參見my entry here加上添加和移除處理程序。