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

如何通过AppleScript/JavaScript调用Photoshop裁剪预设?

解决方案:调用Photoshop裁剪预设实现非破坏性裁剪

一、AppleScript直接调用裁剪预设

Photoshop的AppleScript接口支持直接应用裁剪预设,同时可配置非破坏性裁剪(保留像素不删除),替代模拟按键的不稳定实现。示例代码如下:

tell application "Adobe Photoshop 2024" -- 替换为你的PS版本号
    activate
    set docRef to current document
    -- 开启非破坏性裁剪模式(仅隐藏像素,不删除)
    set delete cropped pixels of docRef to false
    -- 应用指定名称的裁剪预设,替换为你的预设名称
    apply crop preset "你的目标裁剪预设名" to docRef
end tell

结合批量处理需求的完整脚本示例:

set targetFolder to choose folder with prompt "选择待处理图片文件夹"
set cropPresetName to "你的目标裁剪预设名" -- 硬编码预设名称

tell application "Adobe Photoshop 2024"
    activate
    set delete cropped pixels to false -- 全局启用非破坏性裁剪
    repeat with imgFile in (get every file of targetFolder whose kind is "JPEG" or kind is "PNG")
        open imgFile
        set docRef to current document
        try
            apply crop preset cropPresetName to docRef
            -- 可添加保存、关闭等后续步骤
            save docRef in imgFile as JPEG with options {quality:100}
            close docRef saving yes
        on error errMsg
            display dialog "处理" & (name of imgFile) & "出错:" & errMsg
            close docRef saving no
        end try
    end repeat
end tell

二、JavaScript方案(AppleScript可调用)

若偏好JS实现,可编写PS JSX脚本调用预设,再通过AppleScript触发执行:

1. 编写JSX脚本文件(命名为ps_crop_preset.jsx)

// 硬编码目标裁剪预设名称
var targetPreset = "你的目标裁剪预设名";
var activeDoc = app.activeDocument;

// 开启非破坏性裁剪
activeDoc.deleteCroppedPixels = false;

// 查找并应用指定预设
var presetMatched = false;
for (var i = 0; i < app.cropPresets.length; i++) {
    if (app.cropPresets[i].name === targetPreset) {
        app.cropPresets[i].apply();
        presetMatched = true;
        break;
    }
}

if (!presetMatched) {
    alert("未找到指定裁剪预设:" + targetPreset);
}

2. 用AppleScript调用该JSX文件

set jsxFilePath to POSIX path of (choose file with prompt "选择JSX脚本文件" of type "jsx")
tell application "Adobe Photoshop 2024"
    activate
    do javascript file jsxFilePath
end tell

关键注意事项

  • 替换代码中的你的目标裁剪预设名和PS版本号为实际使用内容;
  • delete cropped pixels设为false是实现“保留像素”的核心,与PS手动使用裁剪工具的非破坏性效果一致;
  • 直接调用PS预设接口比模拟按键更稳定,避免界面交互导致的错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 16:37:17