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

批量导出Pages文件为PDF时提示「无效密钥格式」问题求助

解决Pages批量导出PDF时的「无效密钥格式」错误

你的脚本出现「Erreur dans Pages : Forme de clé invalide.」错误,核心原因是导出PDF时传入的路径格式不符合Pages的要求。Pages的export命令仅接受alias类型或POSIX路径作为目标路径参数,而你当前代码中生成的pdfPath是纯文本格式,导致Pages无法解析,从而抛出错误。

另外,脚本还有两个潜在问题:

  • 手动截取文件名去除.pages后缀的方式不可靠:如果文件名包含多个点(如project.v1.pages)或文件名长度不足6字符,会触发字符串截取错误
  • 固定delay 1的等待逻辑不够稳健:不同文档打开速度差异大,可能导致文档未完全加载就执行导出操作

修正后的完整脚本

set docsFolder to choose folder with prompt "Select the folder containing Pages documents:"
set pdfFolder to choose folder with prompt "Select the folder to save PDFs:"

tell application "Finder"
    set pagesFiles to every file of docsFolder whose name extension is "pages"
end tell

repeat with pagesFile in pagesFiles
    tell application "Finder"
        set docPath to pagesFile as alias
        set baseName to name without extension of pagesFile
    end tell
    -- 生成符合Pages要求的alias路径
    set pdfPath to (pdfFolder as text) & baseName & ".pdf" as alias

    tell application "Pages"
        open docPath
        -- 循环等待文档完全打开,替代固定延迟
        repeat until document (name of docPath) exists
            delay 0.2
        end repeat
        tell document (name of docPath)
            export to pdfPath as PDF
            close saving no
        end tell
    end tell
end repeat

关键修正点

  • 路径格式修正:将pdfPath转换为alias类型,直接匹配Pagesexport命令的参数要求
  • 文件名处理优化:使用Finder的name without extension属性直接获取无后缀文件名,避免手动截取的各种异常场景
  • 等待逻辑优化:通过循环检查文档是否已加载完成,确保导出操作在文档就绪后执行,比固定延迟更可靠

内容的提问来源于stack exchange,提问作者Jean-François MARQUIS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 09:56:16