我正在寫一個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是具有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 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 name
到every 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"
對象?
'使用來自'的術語'通常用於可附着的應用程序 - 即那些可以從內部運行腳本'的應用程序',通常通過它們具有它們自己的腳本菜單來指示--BBEdit就是一個很好的例子。當您從BBEdit自己的腳本菜單運行腳本時,BBEdit是默認的告訴目標,所以嚴格來說,「告訴應用程序」對於腳本運行是不必要的,但是,您仍然需要告訴編譯器在哪裏找到術語,所以它可以編譯,因此需要使用塊來使用術語。它們也用於編譯未安裝應用程序的腳本。 – brennanyoung 2011-05-30 04:58:01
謝謝布倫南。我想這是你在設計一門語言時需要做的事情,而且你不需要「導入」語句。 – 2011-05-30 19:58:38