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

问题描述 投票:0回答:1

我有一个脚本,它非常适合在 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 batch-processing photoshop-script
1个回答
0
投票

有两个小问题导致代码无法工作:

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

并且 PNG 质量与 JPG 不一样

pngSaveOptions.quality = 1;
© www.soinside.com 2019 - 2024. All rights reserved.