You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用AppleScript实现Photos应用多条件布尔“与”搜索?

在Photos中用AppleScript实现多条件布尔“与”搜索并复制到新相册

由于Photos的search命令不支持多条件逻辑“与”组合,也无法可靠实现复杂范围的限定,最直接的解决方案是手动遍历目标范围的媒体项,逐个验证条件。以下是完整的实现脚本,包含条件筛选、相册创建和结果复制功能:

-- 配置参数,按需修改
set targetAlbumName to "Test album" -- 要筛选的源相册名称
set requiredKeywords to {"Defined Keyword", "Second Keyword"} -- 需要同时匹配的关键词列表
set newAlbumName to "筛选结果相册" -- 存放结果的新相册名称

tell application "Photos"
    -- 获取目标源相册,不存在则提示并退出
    try
        set sourceAlbum to album targetAlbumName
    on error
        display dialog "源相册 '" & targetAlbumName & "' 不存在,请检查名称" buttons {"确定"} default button 1
        return
    end try
    
    -- 提取源相册内的所有媒体项(照片+视频)
    set allMedia to media items of sourceAlbum
    
    -- 筛选同时满足所有关键词的媒体项
    set filteredMedia to {}
    repeat with mediaItem in allMedia
        set itemKeywords to keywords of mediaItem
        set meetsAllConditions to true
        
        -- 检查是否包含所有要求的关键词(布尔“与”逻辑)
        repeat with kw in requiredKeywords
            if kw is not in itemKeywords then
                set meetsAllConditions to false
                exit repeat
            end if
        end repeat
        
        if meetsAllConditions then
            copy mediaItem to end of filteredMedia
        end if
    end repeat
    
    -- 创建或获取结果相册
    try
        set resultAlbum to album newAlbumName
    on error
        set resultAlbum to make new album with properties {name:newAlbumName}
    end try
    
    -- 将筛选结果复制到新相册(避免重复添加)
    repeat with mediaItem in filteredMedia
        if mediaItem is not in media items of resultAlbum then
            add mediaItem to resultAlbum
        end if
    end repeat
    
    display dialog "筛选完成,共找到 " & (count of filteredMedia) & " 项媒体,已复制到相册 '" & newAlbumName & "'" buttons {"确定"} default button 1
end tell

关键逻辑说明

  • 灵活适配条件:如果需要实现“指定相册+单个关键词”,只需将requiredKeywords改为单元素列表;如果要添加更多条件(比如拍摄日期范围),可以在meetsAllConditions的判断中补充逻辑,例如:
    -- 示例:同时满足关键词+2023年之后拍摄
    set captureDate to date of mediaItem
    set meetsAllConditions to (requiredKeywords is subset of itemKeywords) and (captureDate > date "2023-01-01 00:00:00")
    
  • 错误处理:通过try...on error捕获相册不存在的情况,避免脚本崩溃;添加媒体项时检查是否已存在,防止重复复制。
  • 范围扩展:如果需要搜索整个Photos库而非指定相册,只需将set allMedia to media items of sourceAlbum改为set allMedia to every media item。

内容的提问来源于stack exchange,提问作者ClaytonDeveloper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 18:47:16