Skip to main content
Inspiring
December 15, 2023
Answered

Photoshop Script Selected Layers

  • December 15, 2023
  • 2 replies
  • 996 views

I have a script that automatically removes the background of the image
I now want to modify it and have him execute this script for all my selected layers. How can I do this and need your help



// 背景填充颜色
var colorRef = new SolidColor;
colorRef.rgb.red = 255;
colorRef.rgb.green = 255;
colorRef.rgb.blue = 255;

// 将以下设置为true以使背景透明。
var isTransparent = true;

// 将以下设置为true以使用图像作为背景
var isImageBg = true;

// 如果isImageBg设置为true,
// 则需要预先在Photoshop中打开背景图像
// 背景图像必须是活动文档
//-----------------------------------------------------------------------------------

// 检查是否选择使用图像作为背景
if (isImageBg) {
// 在变量中存储背景图像
var doc_bg = app.activeDocument;
}

// 现在将打开文件列表中的每个文件
// for (var a = 0; a < fileList.length; a++) {
// 在Photoshop中打开文件
// app.open(fileList[a]);

// 选择主体
var idautoCutout = stringIDToTypeID("autoCutout");
var desc01 = new ActionDescriptor();
var idsampleAllLayers = stringIDToTypeID("sampleAllLayers");
desc01.putBoolean(idsampleAllLayers, false);
try {
executeAction(idautoCutout, desc01, DialogModes.NO);
} catch (err) {}

// 反转选择
app.activeDocument.selection.invert();

// 现在背景已被选中。下一步是填充或清除选择。
if (isTransparent) {
// 使活动图层成为普通图层。
activeDocument.activeLayer.isBackgroundLayer = false;
// 使选择透明
app.activeDocument.selection.clear();
app.activeDocument.selection.clear();
} else {
app.activeDocument.selection.fill(colorRef);
}

// 取消选择
app.activeDocument.selection.deselect();

 

This topic has been closed for replies.
Correct answer Stephen Marsh

@h4ste338468947qjm 

 

You can use the following code.

 

I placed your code inside a function so that you can easily modify it without affecting the rest of the script.

 

/*
Remove background on selected layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-selected-layers/td-p/14300237
v1.0, 15th December 2023, Stephen Marsh
*/

#target photoshop

function main() {

    // Save the current dialog display settings
    var savedDisplayDialogs = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    // Capture the initial layer visibility and layer selection: courtesy of jazz-y
    var currentLayersState = getLayersVisiblity();

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var i = 0; i < lrs.count; i++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        // Call the function to remove the background
        removeLayerBackground();

    }
    // Finish the loop over selected layers

    // Restore the initial layer visibility and selection
    setLayersVisiblity(currentLayersState);

    // Restore the dialogs
    app.displayDialogs = savedDisplayDialogs;
}


//// Functions ////

function removeLayerBackground() {
    // 背景填充颜色
    var colorRef = new SolidColor;
    colorRef.rgb.red = 255;
    colorRef.rgb.green = 255;
    colorRef.rgb.blue = 255;
    // 将以下设置为true以使背景透明。
    var isTransparent = true;
    // 将以下设置为true以使用图像作为背景
    var isImageBg = true;
    // 如果isImageBg设置为true,
    // 则需要预先在Photoshop中打开背景图像
    // 背景图像必须是活动文档
    //-----------------------------------------------------------------------------------
    // 检查是否选择使用图像作为背景
    if (isImageBg) {
        // 在变量中存储背景图像
        var doc_bg = app.activeDocument;
    }
    // 现在将打开文件列表中的每个文件
    // for (var a = 0; a < fileList.length; a++) {
    // 在Photoshop中打开文件
    // app.open(fileList[a]);
    // 选择主体
    var idautoCutout = stringIDToTypeID("autoCutout");
    var desc01 = new ActionDescriptor();
    var idsampleAllLayers = stringIDToTypeID("sampleAllLayers");
    desc01.putBoolean(idsampleAllLayers, false);
    try {
        executeAction(idautoCutout, desc01, DialogModes.NO);
    } catch (err) {}
    // 反转选择
    app.activeDocument.selection.invert();
    // 现在背景已被选中。下一步是填充或清除选择。
    if (isTransparent) {
        // 使活动图层成为普通图层。
        activeDocument.activeLayer.isBackgroundLayer = false;
        // 使选择透明
        app.activeDocument.selection.clear();
        app.activeDocument.selection.clear();
    } else {
        app.activeDocument.selection.fill(colorRef);
    }
    // 取消选择
    app.activeDocument.selection.deselect();
}

function getLayersVisiblity() {
    // by jazz-y
    var s2t = stringIDToTypeID,
        t2s = typeIDToStringID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var targetLayers = executeActionGet(r).getList(p),
        seletion = [],
        visiblity = {};
    for (var i = 0; i < targetLayers.count; i++) seletion.push(targetLayers.getReference(i).getIdentifier());
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var len = executeActionGet(r).getInteger(p);
    for (var i = 1; i <= len; i++) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerSection'));
        r.putIndex(s2t('layer'), i);
        if (t2s(executeActionGet(r).getEnumerationValue(p)) == 'layerSectionEnd') continue;
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
        r.putIndex(s2t('layer'), i);
        var id = executeActionGet(r).getInteger(p);
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('visible'));
        r.putIndex(s2t('layer'), i);
        var visible = executeActionGet(r).getBoolean(p);
        visiblity[id] = visible;
    }
    return {
        selection: seletion,
        visiblity: visiblity
    }
}

function setLayersVisiblity(layersStateObject) {
    // by jazz-y
    var s2t = stringIDToTypeID;
    for (var a in layersStateObject.visiblity) {
        makeVisible = layersStateObject.visiblity[a] ? "show" : "hide";
        (r = new ActionReference()).putIdentifier(s2t('layer'), a);
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        executeAction(s2t(makeVisible), d, DialogModes.NO);
    }
    if (layersStateObject.selection.length) {
        var r = new ActionReference()
        for (var i = 0; i < layersStateObject.selection.length; i++)
            r.putIdentifier(s2t("layer"), layersStateObject.selection[i]);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        d.putBoolean(s2t("makeVisible"), false);
        executeAction(s2t("select"), d, DialogModes.NO);
    } else {
        (r = new ActionReference()).putEnumerated(s2t("layer"), s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        executeAction(s2t('selectNoLayers'), d, DialogModes.NO);
    }
}

// Single history stage undo
activeDocument.suspendHistory("Remove background on selected layers", "main()");

 

 

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
December 15, 2023

@h4ste338468947qjm 

 

You can use the following code.

 

I placed your code inside a function so that you can easily modify it without affecting the rest of the script.

 

/*
Remove background on selected layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-selected-layers/td-p/14300237
v1.0, 15th December 2023, Stephen Marsh
*/

#target photoshop

function main() {

    // Save the current dialog display settings
    var savedDisplayDialogs = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    // Capture the initial layer visibility and layer selection: courtesy of jazz-y
    var currentLayersState = getLayersVisiblity();

    // Get the selected layers: courtesy of jazz-y
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();

    // Loop over the selected layers: courtesy of jazz-y
    for (var i = 0; i < lrs.count; i++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t('select'), d, DialogModes.NO);

        // Call the function to remove the background
        removeLayerBackground();

    }
    // Finish the loop over selected layers

    // Restore the initial layer visibility and selection
    setLayersVisiblity(currentLayersState);

    // Restore the dialogs
    app.displayDialogs = savedDisplayDialogs;
}


//// Functions ////

function removeLayerBackground() {
    // 背景填充颜色
    var colorRef = new SolidColor;
    colorRef.rgb.red = 255;
    colorRef.rgb.green = 255;
    colorRef.rgb.blue = 255;
    // 将以下设置为true以使背景透明。
    var isTransparent = true;
    // 将以下设置为true以使用图像作为背景
    var isImageBg = true;
    // 如果isImageBg设置为true,
    // 则需要预先在Photoshop中打开背景图像
    // 背景图像必须是活动文档
    //-----------------------------------------------------------------------------------
    // 检查是否选择使用图像作为背景
    if (isImageBg) {
        // 在变量中存储背景图像
        var doc_bg = app.activeDocument;
    }
    // 现在将打开文件列表中的每个文件
    // for (var a = 0; a < fileList.length; a++) {
    // 在Photoshop中打开文件
    // app.open(fileList[a]);
    // 选择主体
    var idautoCutout = stringIDToTypeID("autoCutout");
    var desc01 = new ActionDescriptor();
    var idsampleAllLayers = stringIDToTypeID("sampleAllLayers");
    desc01.putBoolean(idsampleAllLayers, false);
    try {
        executeAction(idautoCutout, desc01, DialogModes.NO);
    } catch (err) {}
    // 反转选择
    app.activeDocument.selection.invert();
    // 现在背景已被选中。下一步是填充或清除选择。
    if (isTransparent) {
        // 使活动图层成为普通图层。
        activeDocument.activeLayer.isBackgroundLayer = false;
        // 使选择透明
        app.activeDocument.selection.clear();
        app.activeDocument.selection.clear();
    } else {
        app.activeDocument.selection.fill(colorRef);
    }
    // 取消选择
    app.activeDocument.selection.deselect();
}

function getLayersVisiblity() {
    // by jazz-y
    var s2t = stringIDToTypeID,
        t2s = typeIDToStringID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var targetLayers = executeActionGet(r).getList(p),
        seletion = [],
        visiblity = {};
    for (var i = 0; i < targetLayers.count; i++) seletion.push(targetLayers.getReference(i).getIdentifier());
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var len = executeActionGet(r).getInteger(p);
    for (var i = 1; i <= len; i++) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerSection'));
        r.putIndex(s2t('layer'), i);
        if (t2s(executeActionGet(r).getEnumerationValue(p)) == 'layerSectionEnd') continue;
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
        r.putIndex(s2t('layer'), i);
        var id = executeActionGet(r).getInteger(p);
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('visible'));
        r.putIndex(s2t('layer'), i);
        var visible = executeActionGet(r).getBoolean(p);
        visiblity[id] = visible;
    }
    return {
        selection: seletion,
        visiblity: visiblity
    }
}

function setLayersVisiblity(layersStateObject) {
    // by jazz-y
    var s2t = stringIDToTypeID;
    for (var a in layersStateObject.visiblity) {
        makeVisible = layersStateObject.visiblity[a] ? "show" : "hide";
        (r = new ActionReference()).putIdentifier(s2t('layer'), a);
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        executeAction(s2t(makeVisible), d, DialogModes.NO);
    }
    if (layersStateObject.selection.length) {
        var r = new ActionReference()
        for (var i = 0; i < layersStateObject.selection.length; i++)
            r.putIdentifier(s2t("layer"), layersStateObject.selection[i]);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        d.putBoolean(s2t("makeVisible"), false);
        executeAction(s2t("select"), d, DialogModes.NO);
    } else {
        (r = new ActionReference()).putEnumerated(s2t("layer"), s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        executeAction(s2t('selectNoLayers'), d, DialogModes.NO);
    }
}

// Single history stage undo
activeDocument.suspendHistory("Remove background on selected layers", "main()");

 

 

Inspiring
December 15, 2023

I appreciate your help

Stephen Marsh
Community Expert
Community Expert
December 15, 2023

You're welcome!

Bojan Živković11378569
Community Expert
Community Expert
December 15, 2023

I will tag some experts that can help you @c.pfaffenbichler @Stephen Marsh