Select the specific named layer in order according to the number given to it

Contributor ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

Hello sir help me

Kukurykus and Stephen_A_Marsh

 

Jay_1 , Jay_2  ,Jay_4, Jay_5 , Jay_6  ,Jay_7.............................

If the layer name is not Jay_1 then the layer name Jay_2 should be processed and if the layer name is neither Jay_1 nor Jay_2 then the layer name Jay_3 should be processed.

 

 

///////////////////////////////////////////////////////////////////////////

if (app.documents.length > 0) mainScript();

function mainScript() {

try{

var myLayerName = activeDocument.activeLayer.name;

var myLayer= activeDocument.artLayers.getByName("Jay");

 

}catch (errStr){

alert(errStr);

}

}

TOPICS
Windows

Views

213

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 2 Correct answers

Community Expert , Aug 25, 2022 Aug 25, 2022

Try  

// select layer of certain name with highest number;
// 2022, use it at your own risk;
if (app.documents.length > 0) {
    var theRegExp = /^jay_\d{1,3}$/i;
    var theLayers = collectLayersOfName (theRegExp);
    if (theLayers.length > 0) {
    theLayers.sort(sortByDate);
    selectLayerByID(theLayers[theLayers.length-1][2], false);
    }
};
////// collect layers //////
function collectLayersOfName (theRegExp) {
    // the file;
    var myDocument = app.activeDocument;
    // get number o
...

Likes

Translate

Translate
Community Expert , Aug 29, 2022 Aug 29, 2022

If there are Layers that match the name one gets selected, if there are none then nothing should happen. 

If you want an alert (edited) instead add an else-clause to the if-clause. 

 

if (theLayers.length > 0) {
    theLayers.sort(sortByDate);
    selectLayerByID(theLayers[theLayers.length-1][2], false);
    }
else {alert ("whatever")};

 

Likes

Translate

Translate
Community Expert ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

Try  

// select layer of certain name with highest number;
// 2022, use it at your own risk;
if (app.documents.length > 0) {
    var theRegExp = /^jay_\d{1,3}$/i;
    var theLayers = collectLayersOfName (theRegExp);
    if (theLayers.length > 0) {
    theLayers.sort(sortByDate);
    selectLayerByID(theLayers[theLayers.length-1][2], false);
    }
};
////// collect layers //////
function collectLayersOfName (theRegExp) {
    // 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'));
    var theColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
    var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
//    var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
    if (theName.match(theRegExp) != null) {theLayers.push([theName, theIndex, theID, theColor, theName.match(/\d{1,3}$/)])}
    };
    }
    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){
    alert(e.message); 
    }
    };
////// based on code by sam, http://www.rhinocerus.net/forum/lang-javascript/ //////
function sortByDate(a,b) {
    if (Number(a[4])<Number(b[4])) return -1;
    if (Number(a[4])>Number(b[4])) return 1;
    return 0;
    };

edited 2022-08-26

 

 

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
Contributor ,
Aug 26, 2022 Aug 26, 2022

Copy link to clipboard

Copied

First of all thank you for replying to my post may god give you long life may you live 100 years

 

 

This script works well but the problem is that first the script should select Jay_1 name layer but it selects Jay_12 name layer.

This script does the reverse select

It selects the layers in reverse order

And if there is no layer named Jay_1 or Jay_7 or (Jay_another number) then it does not alert.

If the layer with that name is not in the Psd file, then the script should alert and if the layer with that name is present, it should be selected and alerted.

 

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
Contributor ,
Aug 27, 2022 Aug 27, 2022

Copy link to clipboard

Copied

Hello Sir I have modified this place in the script then the script works perfectly but if the layer does not exist then the script does not alert

If there is no layer named Jay_any number then it would be good if the script alerts

 

function sortByDate(a,b) {
if (Number(a[4])<Number(b[4])) return  1;
if (Number(a[4])>Number(b[4])) return -1;
return 0;
};

 

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
Community Expert ,
Aug 28, 2022 Aug 28, 2022

Copy link to clipboard

Copied

I updated the Script on 2022-08-26 to include a check to make sure the length of the Array theLayers is larger than 0. 

Did you use that? 

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
Contributor ,
Aug 28, 2022 Aug 28, 2022

Copy link to clipboard

Copied

thank you for replying

 

No sir I don't know about that script plz send me the link of that script and this script works very well but if the psd file doesn't have a layer named Jay_any number then this script doesn't alert so please fix it this script is too much for me Only useful

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
Community Expert ,
Aug 28, 2022 Aug 28, 2022

Copy link to clipboard

Copied

I updated the Script in this thread. 

Did you use that or the previous version that did not have the line 

    if (theLayers.length > 0) {

?

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
Contributor ,
Aug 28, 2022 Aug 28, 2022

Copy link to clipboard

Copied

yes sir i checked this script before also after update also checked now if there is no layer named jay_any in psd then this script doesn't alert

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
Community Expert ,
Aug 29, 2022 Aug 29, 2022

Copy link to clipboard

Copied

If there are Layers that match the name one gets selected, if there are none then nothing should happen. 

If you want an alert (edited) instead add an else-clause to the if-clause. 

 

if (theLayers.length > 0) {
    theLayers.sort(sortByDate);
    selectLayerByID(theLayers[theLayers.length-1][2], false);
    }
else {alert ("whatever")};

 

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
Contributor ,
Aug 29, 2022 Aug 29, 2022

Copy link to clipboard

Copied

LATEST

Now this script works perfect

Thank you may God give you long life and may you live 100 years

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
Contributor ,
Aug 27, 2022 Aug 27, 2022

Copy link to clipboard

Copied

Sir, I don't have much scripting knowledge, I don't know how many days it will take if I fix it myself, it's just a couple of minutes work for you, so please help.

It took me about 3 days to understand the script and improve it

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
Contributor ,
Aug 28, 2022 Aug 28, 2022

Copy link to clipboard

Copied

Hello sir I am  waiting for your reply

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
Contributor ,
Aug 28, 2022 Aug 28, 2022

Copy link to clipboard

Copied

Sorry sir, my English is not perfect, I am answering your question through google translate

This script works very well but my only problem is that it doesn't alert

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
Community Expert ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

@Nir Photo Art 

 

I'm not 100% sure of what you want, however, give this a try:

 

if (activeDocument.activeLayer.name == "Jay_1") {
    alert("Do something to: " + activeDocument.activeLayer.name);

} else if (activeDocument.activeLayer.name == "Jay_2") {
    alert("Do something to: " + activeDocument.activeLayer.name);

} else {
    activeDocument.activeLayer = activeDocument.layers["Jay_3"];
    alert("Do something to: " + activeDocument.activeLayer.name);
}

 

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
Contributor ,
Aug 26, 2022 Aug 26, 2022

Copy link to clipboard

Copied

The number of layers in a PSD file is not fixed, the layers can be 20 or 30 or even more than 50

During the process if there is no layer of Psd file (Jay_any number) then the process must be stopped.

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