Skip to main content
Inspiring
July 11, 2023
Answered

Remove selected area from selected layers only

  • July 11, 2023
  • 1 reply
  • 948 views

with following script i can remove selected area from all layers: 

 

// 2023, use at your own risk;
if (app.documents.length > 0) {
    var theLayers = collectLayersNamesIndexAndIdentifier ();
    for (var m = 0; m < theLayers.length; m++) {
        selectLayerByID (theLayers[m][2], false);
        try {activeDocument.selection.clear()}
        catch (e) {}
        }
    };
////// collect layers //////
function collectLayersNamesIndexAndIdentifier () {
// the file;
var myDocument = app.activeDocument;
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
theLayers.push([theName, theIndex, theID])
};
}
catch (e) {};
};
return theLayers
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), id);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
        if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
        desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
    try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
}
};

 

 

but i need a script that remove selection from selected layers only (not all layers). in previous posts @Stephen Marshtell me that i must use following code to run any script on selected layers only but i don't know how to replace this code with above script. please help me to modify my script. 

#target photoshop

/* Start Process selected layers - from 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();

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);
/* End Process selected layers - from jazz-y */

// Your code here:
alert(activeDocument.activeLayer.name);

}

 

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

Yes, I saw your message, I'll look into this and reply when I have time.


By @Stephen Marsh

Please review ASAP if possible as this is extremely important to me. Probably my last request in this forum.


quote
quote

Yes, I saw your message, I'll look into this and reply when I have time.


By @Stephen Marsh

Please review ASAP if possible as this is extremely important to me. Probably my last request in this forum.


By @abolfazl29032603daba

 

Here you go:

 

/* https://community.adobe.com/t5/photoshop-ecosystem-discussions/remove-selected-area-from-selected-layers-only/td-p/13927687 */

#target photoshop

/* Check for selection - from jazz-y */
var selectionBounds = null;
try {
    selectionBounds = activeDocument.selection.bounds
} catch (e) {}
if (selectionBounds) {

        /* Start Process selected layers - from 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();
        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);
        /* End Process selected layers - from jazz-y */

        /* Clear selected layers */
        try {
            activeDocument.selection.clear()
        } catch (e) {}
    }

} else {
    alert("This script requires a selection!");
}

 

 

1 reply

Inspiring
July 11, 2023

@Stephen Marsh  maybe if i combine your code in following post this problem will solve 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-run-any-script-on-selected-layers-only-not-all-layers/m-p/13890599

 

I added my code to this but script not working! 

maybe i combine incorrect. please combine and share here if it possible. 

Inspiring
July 12, 2023
quote

@Stephen Marsh  maybe if i combine your code in following post this problem will solve 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-run-any-script-on-selected-layers-only-not-all-layers/m-p/13890599

 

I added my code to this but script not working! 

maybe i combine incorrect. please combine and share here if it possible. 


By @abolfazl29032603daba

@Stephen Marsh  

sir, do you read my message? or do you get notification of my reply in this forum?

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
July 12, 2023
quote

Yes, I saw your message, I'll look into this and reply when I have time.


By @Stephen Marsh

Please review ASAP if possible as this is extremely important to me. Probably my last request in this forum.


quote
quote

Yes, I saw your message, I'll look into this and reply when I have time.


By @Stephen Marsh

Please review ASAP if possible as this is extremely important to me. Probably my last request in this forum.


By @abolfazl29032603daba

 

Here you go:

 

/* https://community.adobe.com/t5/photoshop-ecosystem-discussions/remove-selected-area-from-selected-layers-only/td-p/13927687 */

#target photoshop

/* Check for selection - from jazz-y */
var selectionBounds = null;
try {
    selectionBounds = activeDocument.selection.bounds
} catch (e) {}
if (selectionBounds) {

        /* Start Process selected layers - from 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();
        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);
        /* End Process selected layers - from jazz-y */

        /* Clear selected layers */
        try {
            activeDocument.selection.clear()
        } catch (e) {}
    }

} else {
    alert("This script requires a selection!");
}