• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Automatically search for specific layernames and execute action?

Explorer ,
Mar 20, 2017 Mar 20, 2017

Copy link to clipboard

Copied

Hey!

Let's assume I have a document thet looks like this:

Document setup.PNG

I want a script that does this [Pseudocode... Sortof]:

Set array = ["Template", "Product", "Canvas", "Document Version 1"];

loop trough layernames & groupnames based on array and set to active layer{

if(doc.activeLayer.name === "Template") Dothiscode1();

if(doc.activeLayer.name === "Product") Dothiscode2();

if(doc.activeLayer.name === "Canvas") Dothiscode3();

if(doc.activeLayer.name === "Document Version 1") Dothiscode4();

}

loop trough layernames & groupnames based on array{

  if(layer "template" doesn't exists) { Dothisaction(); }

}

I've only just started scripting (like... an hour this friday, and another three hours today) with javascript, and the things I've tried so far either only find layers that's not within groups, or it only detects layers in groups.

Please tell me if you need further information - do you want me to include code that I've tested so far?

[Moved out of the Coding Corner​ forum and into the more specific Photoshop Scripting​ forum - moderator]

TOPICS
Actions and scripting

Views

1.6K

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

Community Expert , Mar 21, 2017 Mar 21, 2017

Hi johny roger​,

nice try for the beginning.

Keep up the good work!

But there are much more efficient ways.

edvardkrupke​,

you could try something like this:

// layer_selectLayerOrLayerSet_oneByOne_byArrayNameList.jsx

// siehe: https://forums.adobe.com/thread/2292377

// select layers or layerSets one by one by a name list in array

// regards pixxxel schubser

var aDoc = activeDocument;

var aLay;

var arr = ["Template", "Product", "Canvas", "Not_in_Doc", "Document Version 1"];

for (i=0; i <=arr.length-1; i++){

 

...

Votes

Translate

Translate
Adobe
Enthusiast ,
Mar 20, 2017 Mar 20, 2017

Copy link to clipboard

Copied

There is possibility in Action Manager code select layer by name directly by name. No loop needed.

Votes

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 ,
Mar 20, 2017 Mar 20, 2017

Copy link to clipboard

Copied

Hey Jarda!

Thank you for your answer:)

I know this is possible, but the method doesn't allow the layer to be absent - if batching an action with "Select layer 'X'" - and the layer doesn't exist, the action stops... I need something that lets me see if layers exists, and doesn't stop if it's unavailable.

Hopefully you get what I mean?

Votes

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 ,
Mar 20, 2017 Mar 20, 2017

Copy link to clipboard

Copied

maybe this helps : Test whether a layer with a specific name exists?

You can loop through the array of all the layers, checking if the name  you're looking for is in there.
You can even place it in a function that returns a boolean, like so:

function doesLayerExist(layers, name) {
    for (i=0; i<layers.length; i++) {
        if (layers.name==name) return true;
    }
    return false;
}

You can then call it like so:

if (doesLayerExist(app.activeDocument.layers, "Layer1")) {

// do something if the layer exists

} else {

// do something if the layer doesn't exists

}

Or This Thread: Checking if layer is visible and run a function depending on answer

Votes

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 ,
Mar 20, 2017 Mar 20, 2017

Copy link to clipboard

Copied

The problem persists with selecting layers inside of groups, unfortunately. Try the following while having the layer "Canvas" in agroup anywhere in the document:

if (doesLayerExist(app.activeDocument.layers, "Canvas"))

  {

  alert("True");

  }

else

  {

  alert("False");

  }

function doesLayerExist(layers, name)

  {

  for (i=0; i<layers.length; i++)

  {

  if (layers.name==name) return true;

  }

  return false;

  }

This returns "False" even though the layer exists in the document.

As stated by Jarda Bereza, an easy way of doing this would be by using a normal action, but then the action would stop if say, "Document Version 1" was absent and the document was Version 3 or 4 instead.

Thank you for your reply - I'll try to look further into it:/

Votes

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 ,
Mar 21, 2017 Mar 21, 2017

Copy link to clipboard

Copied

Hello Edvard

i'm not a pro at all when it comes to scripting and just started a few days ago with it myself

But from what i've learned by now i could suggest the following for your problem:

  1. Record the action (as you said that would be easy).
  2. Either record the action with scriptinglistener or use one of the scripts available from Xbytor that converts an action to a script.
  3. if your documents have some constant wording in it's name i.E. "Version" but then changing numers like "4" , "5" and so on. Check your document with something like the following :

// Checks if current LayerName contains the wording "FLEX" (i.E. "Flex" nr. 4 or This is a "FLEX".

  if(app.activeDocument.activeLayer.name.toLowerCase().indexOf("flex") >= 0){

  // Do something

        }

//Check if any of the opened documents contain the wording "Mockup" (i.e. Car "Mockup" or "Mockup" Nr. 5)

var docs = app.documents;

  for(var i = docs.length - 1; i >= 0; i--){

  if(docs.name.indexOf('Mockup') > 0){

      //Do something

    }

  }

So, then you can combine your recorded action which you converted to a script with this logic for checking doc names or layer names.

Hope this is something which could help you

Votes

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 ,
Mar 21, 2017 Mar 21, 2017

Copy link to clipboard

Copied

Hey!

Thank you for your response - I'll look into it!

Will mark your post as "Correct Answer" and make a how-to if it turns out it's working

Votes

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 ,
Mar 21, 2017 Mar 21, 2017

Copy link to clipboard

Copied

Hi johny roger​,

nice try for the beginning.

Keep up the good work!

But there are much more efficient ways.

edvardkrupke​,

you could try something like this:

// layer_selectLayerOrLayerSet_oneByOne_byArrayNameList.jsx

// siehe: https://forums.adobe.com/thread/2292377

// select layers or layerSets one by one by a name list in array

// regards pixxxel schubser

var aDoc = activeDocument;

var aLay;

var arr = ["Template", "Product", "Canvas", "Not_in_Doc", "Document Version 1"];

for (i=0; i <=arr.length-1; i++){

    try {

        (ref1 = new ActionReference()).putName(stringIDToTypeID('layer'), arr );

        (desc1 = new ActionDescriptor()).putReference(stringIDToTypeID('null'), ref1);

        executeAction(stringIDToTypeID('select'), desc1, DialogModes.NO);

        aLay = aDoc.activeLayer;

        switch ( aLay.name )  

        {

            case "Template":

            //alert("show first array name"); // do something

            alert(aLay.typename + " " + aLay.name + " is active now");

            break;

            case "Product":

            //alert("show second array name"); // do something

            alert(aLay.typename + " " + aLay.name + " is active now");

            break;

            case "Canvas":

            //alert("show third array name"); // do something

            alert(aLay.typename + " " + aLay.name + " is active now");

            break;

            case "Document Version 1":

            //alert("show last array name"); // do something

            alert(aLay.typename + " " + aLay.name + " is active now");

            break;

        }

    } catch (e) {

        alert("Sorry, this layername: " + arr + " does not exists");

    }

}

Have fun

Votes

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 ,
Mar 22, 2017 Mar 22, 2017

Copy link to clipboard

Copied

LATEST

Hey!

That's awesome! Actions on steroids<3

Thank you so much! You've saved me tons of work

I'll definitely have fun with this - it's excactly what I wanted

Votes

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