Photoshop 脚本更改为 .jsx 代码以存储具有透明背景的 PNG

Photoshop Scripting change to .jsx code to save a PNG with transparent background

提问人:Tali 提问时间:11/9/2023 最后编辑:Tali 更新时间:11/9/2023 访问量:25

问:

我有一个脚本,它非常适合在 photoshop 中批量自动化模型......但是我想知道如何更改它,以便它输出具有透明背景的 PNG 而不是 Jpeg。是的,我是脚本编写的新手,任何帮助将不胜感激。感谢您的阅读 ✌️

所以这就是尝试过的

// Replace SmartObject’s Content and Save as JPG
// 2017, use it at your own risk
// Via @Circle B: https://graphicdesign.stackexchange.com/questions/92796/replacing-a-smart-object-in-bulk-with-photoshops-variable-data-or-scripts/93359
// JPG code from here: https://forums.adobe.com/thread/737789

#target photoshop
if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    var theLayer = myDocument.activeLayer;
    // PNG Options;
    pngSaveOptions = new pngSaveOptions();  
    pngSaveOptions.embedColorProfile = true;  
    pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
    pngSaveOptions.matte = MatteType.NONE;  
    pngSaveOptions.quality = 11;   
    // Check if layer is SmartObject;
    if (theLayer.kind != "LayerKind.SMARTOBJECT") {
        alert("selected layer is not a smart object")
    } else {
        // Select Files;
        if ($.os.search(/windows/i) != -1) {
            var theFiles = File.openDialog("please select files", "*.png;*.psd;*.tif;*.jpg", true)
        } else {
            var theFiles = File.openDialog("please select files", getFiles, true)
        };
        if (theFiles) {
            for (var m = 0; m < theFiles.length; m++) {
                // Replace SmartObject
                theLayer = replaceContents(theFiles[m], theLayer);
                var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
                // Save png
                myDocument.saveAs((new File(thePath + "/"  + theNewName + ".png")), pngSaveOptions, true,Extension.LOWERCASE);
            }
        }
    }
};
// Get PSDs, TIFs and JPGs from files
function getFiles(theFile) {
    if (theFile.name.match(/\.(png|psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {
        return true
    };
};
// Replace SmartObject Contents
function replaceContents(newFile, theSO) {
    app.activeDocument.activeLayer = theSO;
    // =======================================================
    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
    var desc3 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    desc3.putPath(idnull, new File(newFile));
    var idPgNm = charIDToTypeID("PgNm");
    desc3.putInteger(idPgNm, 1);
    executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
    return app.activeDocument.activeLayer
};

PNG 批处理 photoshop-script

评论

0赞 Ghoul Fool 11/9/2023
欢迎。您需要将代码粘贴并格式化到您的问题中。
0赞 Tali 11/9/2023
谢谢你,对不起我的新奇

答:

0赞 Ghoul Fool 11/9/2023 #1

有两件小事会阻止代码工作:

 pngSaveOptions = new PNGSaveOptions(); // note capitals in PNGSaveOptions

并且PNG质量与JPG不同

pngSaveOptions.quality = 1;

评论

0赞 Tali 11/9/2023
非常感谢!