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

能否在JavaScript工作流中访问Image Events实现Automator图片缩放?

问题解答

可以在Automator的JavaScript(即JXA,JavaScript for Automation)中调用Image Events库,以下是具体操作步骤和可用的脚本示例:


操作流程
  1. 打开Automator,新建「快速动作」类型的项目
  2. 快速动作配置项设置:
  • 工作流接收:选择「图像文件」
  • 位于:选择「访达.app」(可根据你需要的触发场景调整)
  1. 从左侧动作库拖拽「Run JavaScript」动作到右侧工作流区域
  2. 替换动作内的默认代码为下面的示例脚本,你可以根据需要调整缩放逻辑

可用脚本示例
function run(input, parameters) {
    // 初始化需要用到的应用实例
    const app = Application.currentApplication()
    app.includeStandardAdditions = true
    const imageEvents = Application('Image Events')
    
    // 弹窗获取用户输入的缩放百分比
    const scaleInput = app.displayDialog('请输入缩放百分比(仅填数字,如80代表缩放为原尺寸的80%):', {
        defaultAnswer: '80',
        buttons: ['取消', '确定'],
        defaultButton: '确定'
    })
    if (scaleInput.buttonReturned === '取消') return input // 取消操作直接返回原文件
    const scaleFactor = Number(scaleInput.textReturned) / 100
    if (isNaN(scaleFactor) || scaleFactor <= 0) {
        app.displayDialog('输入的比例无效,请输入正整数')
        return input
    }

    // 遍历处理所有传入的图片文件
    input.forEach(filePath => {
        try {
            // 打开图片
            const img = imageEvents.open(Path(filePath.toString()))
            // 按比例缩放
            img.scale({ byFactor: scaleFactor })
            // 保存修改(如果需要另存为新文件,可以修改save的路径参数,避免覆盖原文件)
            img.close({ saving: true })
        } catch (err) {
            app.displayDialog(`处理文件 ${filePath.toString()} 失败:${err.message}`)
        }
    })

    return input
}

注意事项
  • macOS Big Sur对文件权限管控较严,首次运行时如果弹出权限申请,请授予Automator对应文件夹的读写权限
  • 如果不想覆盖原文件,可以修改img.close的保存逻辑,指定新的存储路径,比如在原文件名后加_scaled后缀再保存
  • 上述脚本支持批量选中多张图片同时处理

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 03:51:04