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

AppleScript操作Photos时文件夹引用在子程序中失效是什么原因?

问题根因

报错并非参数按引用传递导致,本质是AppleScript命令执行上下文不匹配:
你定义的createFolderInSubRoutine子程序默认运行在全局脚本上下文,子程序内的exists、make new folder都是Photos应用的专属命令,没有显式指定发给Photos处理时,系统无法识别传入的Photos文件夹对象类型,因此抛出-1700类型转换错误。

修复方案

只需在子程序内部显式指定Photos应用作为命令接收方即可,修改后的可运行代码如下:

on createFolderInSubRoutine(parentFolder)
    -- 子程序内指定命令发送给Photos应用处理
    tell application "Photos"
        -- 增加in parentFolder限定,避免误判根目录下的同名文件夹
        if not (folder named "bar" exists in parentFolder) then 
            make new folder named "bar" at parentFolder
        end if
    end tell
end createFolderInSubRoutine

tell application "Photos"
    set theParentFolder to folder "My Parent Folder"
    if not (folder named "foo" exists in theParentFolder) then 
        make new folder named "foo" at theParentFolder
    end if
    my createFolderInSubRoutine(theParentFolder)
end tell

如果需要复用子程序处理多个应用的文件夹操作,也可以把应用对象作为第二个参数传入子程序,灵活切换命令上下文。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 18:15:01