Skip to main content
Mohamed Hameed21513110
Inspiring
February 4, 2023
Answered

A problem with a code that does not work on Photoshop CS5

  • February 4, 2023
  • 2 replies
  • 842 views

Hello dear brothers

I have this code and it puts a number of image layers into a group based on an input value
This code works well on recent versions of Photoshop
But sometimes I work on Photoshop CS5
I noticed that the code does not work on this version with a problem appearing in this line

 

ref. putIdentifier(charIDToTypeID("Lyr"), index);


And this is the complete code in order to be fully explained

 

deSelectAllLayers();
var _layers = app.activeDocument.artLayers;
var _name = prompt('Enter number : ', '');
if (_name) {
    _groupLength = Number(_name);
    if (!isNaN(_groupLength)) {
        if (_groupLength != 0 && _groupLength <= _layers.length) {
            for (var i = 0; i < _groupLength; i++) {
                selectLayerByID(_layers[i].id, true);
            }
            groupSelectedLayers(_name);
        } else {
            alert('Either you entered 0 or entered number is higher than the number of layers present in the document');
        }
    } else {
        alert('Enter value is not the number')
    }
}

function groupSelectedLayers(theName) {
    var desc159 = new ActionDescriptor();
    var ref114 = new ActionReference();
    var idlayer = stringIDToTypeID("layer");
    var idordinal = stringIDToTypeID("ordinal");
    var idtargetEnum = stringIDToTypeID("targetEnum");
    var idnull = stringIDToTypeID("null");
    var idname = stringIDToTypeID("name");
    ref114.putEnumerated(idlayer, idordinal, idtargetEnum);
    desc159.putReference(idnull, ref114);
    desc159.putString(idname, "aaa");
    executeAction(stringIDToTypeID("groupLayersEvent"), desc159, DialogModes.NO);
    var desc63 = new ActionDescriptor();
    var ref37 = new ActionReference();
    ref37.putEnumerated(idlayer, idordinal, idtargetEnum);
    desc63.putReference(idnull, ref37);
    var desc64 = new ActionDescriptor();
    desc64.putString(idname, theName);
    desc63.putObject(stringIDToTypeID("to"), idlayer, desc64);
    executeAction(stringIDToTypeID("set"), desc63, DialogModes.NO)
}

function selectLayerByID(index, add) {
    add = undefined ? add = false : add
    var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), index);
    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) {
        alert(e.message);
    }
};

function deSelectAllLayers() {
    var idselectNoLayers = stringIDToTypeID("selectNoLayers");
    var desc26 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref3 = new ActionReference();
    var idLyr = charIDToTypeID("Lyr ");
    var idOrdn = charIDToTypeID("Ordn");
    var idTrgt = charIDToTypeID("Trgt");
    ref3.putEnumerated(idLyr, idOrdn, idTrgt);
    desc26.putReference(idnull, ref3);
    executeAction(idselectNoLayers, desc26, DialogModes.NO);
}

 

Thank you for your interest and constant response and assistance to us

This topic has been closed for replies.
Correct answer r-bin
Well, if you use minimal changes in the code, then you can do it this way.

 

deSelectAllLayers();
var _layers = app.activeDocument.artLayers;
var _name = prompt('Enter number : ', '');
if (_name) {
    _groupLength = Number(_name);
    if (!isNaN(_groupLength)) {
        if (_groupLength != 0 && _groupLength <= _layers.length) {

            // NEW CODE BEG ////////////////////
            for (var i = 0; i < _groupLength; i++) 
                {
                app.activeDocument.activeLayer = _layers[i];
                var r = new ActionReference();    
                r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
                _layers[i].id = executeActionGet(r).getInteger(stringIDToTypeID("layerID"));
                }

            deSelectAllLayers();    
            // NEW CODE END ////////////////////

            for (var i = 0; i < _groupLength; i++) {
                selectLayerByID(_layers[i].id, true);
            }
            groupSelectedLayers(_name);
        } else {
            alert('Either you entered 0 or entered number is higher than the number of layers present in the document');
        }
    } else {
        alert('Enter value is not the number')
    }
}

function groupSelectedLayers(theName) {
    var desc159 = new ActionDescriptor();
    var ref114 = new ActionReference();
    var idlayer = stringIDToTypeID("layer");
    var idordinal = stringIDToTypeID("ordinal");
    var idtargetEnum = stringIDToTypeID("targetEnum");
    var idnull = stringIDToTypeID("null");
    var idname = stringIDToTypeID("name");
    ref114.putEnumerated(idlayer, idordinal, idtargetEnum);
    desc159.putReference(idnull, ref114);
    desc159.putString(idname, "aaa");
    executeAction(stringIDToTypeID("groupLayersEvent"), desc159, DialogModes.NO);
    var desc63 = new ActionDescriptor();
    var ref37 = new ActionReference();
    ref37.putEnumerated(idlayer, idordinal, idtargetEnum);
    desc63.putReference(idnull, ref37);
    var desc64 = new ActionDescriptor();
    desc64.putString(idname, theName);
    desc63.putObject(stringIDToTypeID("to"), idlayer, desc64);
    executeAction(stringIDToTypeID("set"), desc63, DialogModes.NO)
}

function selectLayerByID(index, add) {
    add = undefined ? add = false : add
    var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), index);
    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) {
        alert(e.message);
    }
};

function deSelectAllLayers() {
    var idselectNoLayers = stringIDToTypeID("selectNoLayers");
    var desc26 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref3 = new ActionReference();
    var idLyr = charIDToTypeID("Lyr ");
    var idOrdn = charIDToTypeID("Ordn");
    var idTrgt = charIDToTypeID("Trgt");
    ref3.putEnumerated(idLyr, idOrdn, idTrgt);
    desc26.putReference(idnull, ref3);
    executeAction(idselectNoLayers, desc26, DialogModes.NO);
}

 

A faster way to work requires more complex and longer code.
 

2 replies

Legend
February 5, 2023
In Photoshop CS5 and CS6, the Layer object does not have an id property.
Therefore, the line selectLayerByID(_layers[i].id, true) sends undefined to the function, inside which the error occurs.
 
Mohamed Hameed21513110
Inspiring
February 5, 2023

Sorry for not being familiar with the lines of this code

Is there a solution to this problem

Sometimes I work on files that contain layers of images of more than 300 or 400 images
And I want to divide those layers into groups with specified numbers entered

I don't know if I explained what was required or not

r-binCorrect answer
Legend
February 6, 2023
Well, if you use minimal changes in the code, then you can do it this way.

 

deSelectAllLayers();
var _layers = app.activeDocument.artLayers;
var _name = prompt('Enter number : ', '');
if (_name) {
    _groupLength = Number(_name);
    if (!isNaN(_groupLength)) {
        if (_groupLength != 0 && _groupLength <= _layers.length) {

            // NEW CODE BEG ////////////////////
            for (var i = 0; i < _groupLength; i++) 
                {
                app.activeDocument.activeLayer = _layers[i];
                var r = new ActionReference();    
                r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
                _layers[i].id = executeActionGet(r).getInteger(stringIDToTypeID("layerID"));
                }

            deSelectAllLayers();    
            // NEW CODE END ////////////////////

            for (var i = 0; i < _groupLength; i++) {
                selectLayerByID(_layers[i].id, true);
            }
            groupSelectedLayers(_name);
        } else {
            alert('Either you entered 0 or entered number is higher than the number of layers present in the document');
        }
    } else {
        alert('Enter value is not the number')
    }
}

function groupSelectedLayers(theName) {
    var desc159 = new ActionDescriptor();
    var ref114 = new ActionReference();
    var idlayer = stringIDToTypeID("layer");
    var idordinal = stringIDToTypeID("ordinal");
    var idtargetEnum = stringIDToTypeID("targetEnum");
    var idnull = stringIDToTypeID("null");
    var idname = stringIDToTypeID("name");
    ref114.putEnumerated(idlayer, idordinal, idtargetEnum);
    desc159.putReference(idnull, ref114);
    desc159.putString(idname, "aaa");
    executeAction(stringIDToTypeID("groupLayersEvent"), desc159, DialogModes.NO);
    var desc63 = new ActionDescriptor();
    var ref37 = new ActionReference();
    ref37.putEnumerated(idlayer, idordinal, idtargetEnum);
    desc63.putReference(idnull, ref37);
    var desc64 = new ActionDescriptor();
    desc64.putString(idname, theName);
    desc63.putObject(stringIDToTypeID("to"), idlayer, desc64);
    executeAction(stringIDToTypeID("set"), desc63, DialogModes.NO)
}

function selectLayerByID(index, add) {
    add = undefined ? add = false : add
    var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), index);
    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) {
        alert(e.message);
    }
};

function deSelectAllLayers() {
    var idselectNoLayers = stringIDToTypeID("selectNoLayers");
    var desc26 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref3 = new ActionReference();
    var idLyr = charIDToTypeID("Lyr ");
    var idOrdn = charIDToTypeID("Ordn");
    var idTrgt = charIDToTypeID("Trgt");
    ref3.putEnumerated(idLyr, idOrdn, idTrgt);
    desc26.putReference(idnull, ref3);
    executeAction(idselectNoLayers, desc26, DialogModes.NO);
}

 

A faster way to work requires more complex and longer code.
 
Stephen Marsh
Community Expert
Community Expert
February 5, 2023

I don't know why, that is a very generic piece of code that should work in any version. I can't test on such an old version.

 

Note that:

 

ref. putIdentifier(charIDToTypeID("Lyr"), index);

 

should be (no space after ref.)

 

ref.putIdentifier(charIDToTypeID("Lyr "), index);

 

(i.e. A four character code using a space at the end)

 

You could try changing:

 

ref.putIdentifier(charIDToTypeID("Lyr "), index);

 

To:

 

ref.putIdentifier(stringIDToTypeID ('layer'), index);

 

However, I doubt that it would help!

 

P.S. You could Google "selectLayerByID" and see what hits you get!

Mohamed Hameed21513110
Inspiring
February 5, 2023

Thank you but when trying the code after modifying the line
I see an error in this line

 

Stephen Marsh
Community Expert
Community Expert
February 5, 2023

As I wrote, I doubt it will help! It is the same thing, just expressed differently. Is the error the same or different?

 

Have you tried running the script directly in Photoshop, not through ESTK?

 

Do you have ScriptListener for CS5 to see what code it produces?