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

AppleScript访问Photos App子文件夹触发Invalid Index错误求助

解决Photos AppleScript无法索引子文件夹的问题

问题原因

  1. 直接执行first container whose name is "Work Photos"时,Photos的AppleScript默认只扫描顶级容器(也就是My Albums下的直接子项),不会递归查找深层子文件夹,所以当Work Photos在Business子文件夹下时会提示索引无效。
  2. 第二个脚本的错误是语法逻辑错了:folder of "Business"里的"Business"是纯字符串,不是Photos里的文件夹对象,必须先拿到Business的对象引用,才能访问它的子文件夹。

解决方法

方法1:递归查找全层级文件夹(适用于层级不确定的情况)

写个递归函数遍历所有文件夹,精准定位目标:

tell application "Photos"
    on findFolderByName(targetName, parentFolder)
        set matches to folders of parentFolder whose name is targetName
        if matches is not {} then return item 1 of matches
        repeat with subFolder in folders of parentFolder
            set found to my findFolderByName(targetName, subFolder)
            if found is not missing value then return found
        end repeat
        return missing value
    end findFolderByName

    set workPhotosFolder to findFolderByName("Work Photos", container 1)
    if workPhotosFolder is not missing value then
        display dialog (id of workPhotosFolder) as text
    else
        display dialog "没找到Work Photos文件夹"
    end if
end tell

方法2:逐层定位(层级明确时更高效)

既然知道层级是My Albums > Business > Work Photos,就逐层获取文件夹对象:

tell application "Photos"
    set businessFolder to first folder of container 1 whose name is "Business"
    set workPhotosFolder to first folder of businessFolder whose name is "Work Photos"
    display dialog (id of workPhotosFolder) as text
end tell

额外说明

  • container 1就是界面里的My Albums,是所有自定义相簿/文件夹的顶级入口。
  • 访问子文件夹必须基于父文件夹的对象引用,不能直接用字符串名称操作。
  • 如果有同名文件夹,可以加额外筛选条件(比如创建时间)来避免匹配错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 15:00:59