Applescript 在文件夹中搜索带有关键字的照片

问题描述 投票:0回答:1

我想写一个 Applescript 来搜索照片应用程序中的照片,这些照片由多个条件布尔“和”搜索返回,例如两个关键字,或相册位置和关键字。对于上下文,在脚本的后续部分,我将要求脚本将脚本返回的照片复制到脚本创建的相册中。

Photos AppleScript 字典有一个搜索命令,但它只响应文本字符串。似乎没有任何方法可以优化搜索以限制到特定文件夹或其他搜索条件。

我想知道是否有人知道如何将搜索限制为两个条件的布尔值?

我是 Applescript 的新手,感谢您的帮助!

tell application "Photos"
    set Srchres to search for "Defined Keyword" in album "Test album"
end tell
applescript photo
1个回答
0
投票

Photos.app 有 search 命令,但它设计用于在媒体项目名称中使用 子字符串搜索,而不是在媒体项目的关键字中搜索

要通过关键字搜索媒体项目,您应该使用whose子句。请注意,关键字是一个字符串列表(总是)。另外,请注意,您应该按照 Photos.app 中文件夹和相册的结构提供相册的完整参考

这是搜索示例,适用于我的 Photos.app(Catalina 操作系统):

tell application "Photos"
    set Srchres to media items of ¬
        (album "Untitled Album" of folder "Untitled Folder2" of folder "Robert") ¬
            whose keywords is {"Alexandra", "Untitled Album"}
end tell

--> Result: {media item id "0425380C-2C79-422F-90F8-0DFAB4FF3389/L0/001" of album id "F37876B4-6C89-4B78-90E8-78FF9287E199/L0/040" of folder id "22697ACA-E9AA-4720-BD03-8DF8B6D7E9DA/L0/020" of folder id "C8E0E9FC-57EB-4219-8EAB-BA4B3F34B5DC/L0/020"}

如果您只想搜索关键字列表中的某个关键字,您应该使用“重复循环”方法:

set mediaItems to {}

tell application "Photos"
    tell album "Untitled Album" of folder "Untitled Folder2" of folder "Robert"
        repeat with mediaItem in (get media items)
            if "Alexandra" is in (get keywords of mediaItem) then
                set end of mediaItems to contents of mediaItem
            end if
        end repeat
    end tell
end tell

return mediaItems

--> Result: {media item id "0425380C-2C79-422F-90F8-0DFAB4FF3389/L0/001" of album id "F37876B4-6C89-4B78-90E8-78FF9287E199/L0/040" of folder id "22697ACA-E9AA-4720-BD03-8DF8B6D7E9DA/L0/020" of folder id "C8E0E9FC-57EB-4219-8EAB-BA4B3F34B5DC/L0/020"}

处理程序形式的最后一个脚本

on searchByKeyword:theKeyword inAlbum:albumReference
    set mediaItems to {}
    tell application "Photos" to tell albumReference
        repeat with mediaItem in (get media items)
            if theKeyword is in (get keywords of mediaItem) then
                set end of mediaItems to contents of mediaItem
            end if
        end repeat
    end tell
    return mediaItems
end searchByKeyword:inAlbum:

tell application "Photos" to set albumReference to a reference to album "Untitled Album" of folder "Untitled Folder2" of folder "Robert"
my searchByKeyword:"Alexandra" inAlbum:albumReference
--> Result: {media item id "0425380C-2C79-422F-90F8-0DFAB4FF3389/L0/001" of album id "F37876B4-6C89-4B78-90E8-78FF9287E199/L0/040" of folder id "22697ACA-E9AA-4720-BD03-8DF8B6D7E9DA/L0/020" of folder id "C8E0E9FC-57EB-4219-8EAB-BA4B3F34B5DC/L0/020"}
© www.soinside.com 2019 - 2024. All rights reserved.