Я пишу 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
Объект application
в Aperture содержит набор элементов из image version
s. Объекты image version
содержат набор элементов из keyword
s. keyword
— это объекты/списки со свойствами name
и id
.
Таким образом, глобальное свойство скрипта Movies
теперь содержит все объекты image version
в моей библиотеке Aperture, которые на самом деле являются видео. Теперь, когда я пытаюсь сделать это:
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
s. Когда я делаю это:
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'.
...
Может ли кто-нибудь сказать мне, что не так с моим спецификатором объекта? Почему я могу применить 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"
?