Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Select layer by index doesn't work correctly when a background layer is in your document.

Explorer ,
Nov 03, 2021 Nov 03, 2021

Copy link to clipboard

Copied

I came across a strange "bug" when incorrect layers were selected using select by ID methode.

The following code should simply alert the currently selected layer and index of it, than using the select by index function, select that same layer, and than reports back the selected layers index. Both should be the same if it's working correctly, and in some psd's its working perfect, select a layer with index 10, and after selecting, layer index 10 is still selected. But some psd's were not working correctly. And I discovered that when a background layer is present, the select by ID isn't working correctly anymore, you select 10, and than it selects 11 instead.

Here is the test code I used...

sTT = stringIDToTypeID;

function actionManagerSelectLayerByIndex(itemIndex, makeVisible){

    var d1 = new ActionDescriptor();
    var r1 = new ActionReference();

    r1.putIndex( sTT('layer'), itemIndex );
    d1.putReference( sTT('null'), r1 );
    d1.putBoolean( sTT('MkVs'), makeVisible );
    
    executeAction( sTT('select'), d1, DialogModes.NO );
    }

alert("Selected Layer: " + activeDocument.activeLayer + " - ItemIndex: " + activeDocument.activeLayer.itemIndex)
actionManagerSelectLayerByIndex(activeDocument.activeLayer.itemIndex, true);
alert("Selected Layer: " + activeDocument.activeLayer + " - ItemIndex: " + activeDocument.activeLayer.itemIndex)

 

I want to use the function select by index to select the layer above or below the currently selected layer. But that just became unreliable... Is anyone aware of this issue?

TOPICS
Actions and scripting

Views

45

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Nov 03, 2021 Nov 03, 2021
It's unusual, but it's not a bug  In Action Manager (AM), only the background layer can have index 0. Indexing of any other layers starts from 1. You need to check if there is a background layer in the document and change the index value accordingly try {activeDocument.backgroundLayer} catch (e) {alert ('no background!')}

Likes

Translate

Translate
Adobe Community Professional ,
Nov 03, 2021 Nov 03, 2021

Copy link to clipboard

Copied

It's unusual, but it's not a bug 🙂
In Action Manager (AM), only the background layer can have index 0. Indexing of any other layers starts from 1. You need to check if there is a background layer in the document and change the index value accordingly

 

 

try {activeDocument.backgroundLayer} catch (e) {alert ('no background!')}

 

 

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 03, 2021 Nov 03, 2021

Copy link to clipboard

Copied

LATEST

Ah.. oke, if AM and DOM have different index numbers, that can explain why one layer can have a different index when targetting or reading the index.

Would a background check be enough to make it reliable?

I just added the check background, and lowers the index by 1...

 

sTT = stringIDToTypeID;

function actionManagerSelectLayerByIndex(itemIndex, makeVisible){
    
    function hasBackground() { 
        
        var ref = new ActionReference(); 
        
        ref.putProperty( charIDToTypeID("Prpr"), charIDToTypeID( "Bckg" )); 
        ref.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Back" ));
        
        var desc =  executeActionGet(ref); 
        var res = desc.getBoolean(charIDToTypeID( "Bckg" )); 
        
        return res;   
        
    };

    var backgroundLayerPresent = hasBackground()

    if (backgroundLayerPresent){
        itemIndex = itemIndex - 1;
        }

    var d1 = new ActionDescriptor();
    var r1 = new ActionReference();

    r1.putIndex( sTT('layer'), itemIndex );
    d1.putReference( sTT('null'), r1 );
    d1.putBoolean( sTT('MkVs'), makeVisible );
    
    executeAction( sTT('select'), d1, DialogModes.NO );
    }

alert("Selected Layer: " + activeDocument.activeLayer + " - ItemIndex: " + activeDocument.activeLayer.itemIndex)
actionManagerSelectLayerByIndex(activeDocument.activeLayer.itemIndex, true);
alert("Selected Layer: " + activeDocument.activeLayer + " - ItemIndex: " + activeDocument.activeLayer.itemIndex)

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines