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

如何在Apple Automator快速操作脚本中获取选中文件

Automator快速操作:AppleScript处理PDF裁剪的错误修复

问题背景

原本用于PDF裁剪的AppleScript通过choose file弹窗选择文件,现在要改成Automator快速操作,直接接收选中的文件作为输入。

原代码:

on run
    set sourceFile to POSIX path of (choose file with prompt "Choose a PDF file to crop" of type {"pdf"})
    set sourceFile to current application's |NSURL|'s fileURLWithPath:sourceFile
    set theDocument to (current application's PDFDocument's alloc()'s initWithURL:sourceFile)
end run

修改后的代码(执行出错):

on run {input, parameters}
    set sourceFile to POSIX path of input
    set sourceFile to current application's |NSURL|'s fileURLWithPath:sourceFile
    set theDocument to (current application's PDFDocument's alloc()'s initWithURL:sourceFile)
end run

执行时触发错误:

„POSIX path of {alias "Macintosh HD:Users:armin:Desktop:testfile.pdf", alias "Macintosh HD:Users:armin:Desktop:testfile.pdf"}“ can't be opened.

错误原因

Automator快速操作传递的input是列表类型,即便只选中一个文件,它也会被封装成包含单个元素的列表。而POSIX path of命令仅支持处理单个文件别名,直接作用于列表会导致语法错误。

修正方案

单文件处理

从input列表中取出第一个元素,再转换为POSIX路径:

on run {input, parameters}
    set sourceFile to POSIX path of item 1 of input
    set sourceFile to current application's |NSURL|'s fileURLWithPath:sourceFile
    set theDocument to (current application's PDFDocument's alloc()'s initWithURL:sourceFile)
    -- 在此添加PDF裁剪的后续逻辑
end run

多文件批量处理

如果需要支持一次性处理多个选中的PDF文件,可通过循环遍历input列表实现:

on run {input, parameters}
    repeat with aFile in input
        set sourceFile to POSIX path of aFile
        set sourceFile to current application's |NSURL|'s fileURLWithPath:sourceFile
        set theDocument to (current application's PDFDocument's alloc()'s initWithURL:sourceFile)
        -- 在此添加单个PDF文件的裁剪逻辑
    end repeat
end run

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 03:10:56