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

Run a script if a layer exists?

Community Beginner ,
Oct 08, 2015 Oct 08, 2015

Is it possible to create a script that will search through my document and check if the layer name: "Selection01"  exists.  if it does, to run the script located in:

D:/Scripts/WorkingScripts/Selection01.jsx

If f the layer name does not exist, to ignore?

Thanks for any help!

TOPICS
Actions and scripting
2.4K
Translate
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
Adobe
Explorer ,
Oct 08, 2015 Oct 08, 2015

so the code part is easy.  As far as running the other script there are a couple of things you need to do.


First, you need to include that script/ script location into the header of the file.  Near the very top of the file add:

#include D:/Scripts/WorkingScripts/Selection01.jsx

this will make sure it loads that portion of the javascript when it loads the rest of the current file.

Second thing you need to do, is to wrap it into a function for execution. Ie.

function Run_Selection01_JSX{

     /* do a little dance

         do some code

         make everyone happy */

}

So, once that is nicely wrapped up into a function, the code for the layer name checking is easy.

if(activeDocument.artLayers.getByName("Selection01"){

     Run_Selection01_JSX

} else {

     //do nothing,

     //do something, its up to you.

}

viola! it should be that easy.  unless you need to pass arguments between the new "function" in the selection01.jsx file and the current file your in.

I hope this helps, enjoy

Translate
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 Beginner ,
Oct 08, 2015 Oct 08, 2015

Thanks, d1g1talphyre

....I only need this to run the script..

I'll try to get this to work.

thanks again!

Translate
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
Advisor ,
Oct 08, 2015 Oct 08, 2015

function selectLayerByName(name) {
  function cTID(s) { return app.charIDToTypeID(s); };
  function sTID(s) { return app.stringIDToTypeID(s); };

    var desc14 = new ActionDescriptor();
        var ref4 = new ActionReference();
        ref4.putName( cTID('Lyr '), name );
    desc14.putReference( cTID('null'), ref4 );
    desc14.putBoolean( cTID('MkVs'), false );
        var list1 = new ActionList();
        list1.putInteger( 3 );
    desc14.putList( cTID('LyrI'), list1 );
    executeAction( cTID('slct'), desc14, DialogModes.NO );

    if (app.activeDocument.activeLayer.name == name) {

       return app.activeDocument.activeLayer;

    } else {

       return null;

   }
};

var layer = selectByName("Selection01");

if (layer) {

  // do what you need to do here

}

Translate
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
Advocate ,
Oct 11, 2015 Oct 11, 2015

Hi xbytor2‌,

may I ask you what is the ActionList for, in your snippet? putInteger(3) makes me really curious 🙂

Thank you!

Davide

Translate
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
Advisor ,
Oct 11, 2015 Oct 11, 2015

My guess is that's the layer's index, which would be a bit redundant. I'll boot up PS later and see what's going on.

Translate
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
Advisor ,
Oct 11, 2015 Oct 11, 2015
LATEST

It is the layer's index but it is apparently not strictly required. This works:

function selectLayerByName(name) {
  function cTID(s) { return app.charIDToTypeID(s); };
  function sTID(s) { return app.stringIDToTypeID(s); };

    var desc14 = new ActionDescriptor();
        var ref4 = new ActionReference();
        ref4.putName( cTID('Lyr '), name );
    desc14.putReference( cTID('null'), ref4 );
    desc14.putBoolean( cTID('MkVs'), false );
    executeAction( cTID('slct'), desc14, DialogModes.NO );

    if (app.activeDocument.activeLayer.name == name) {

      return app.activeDocument.activeLayer;

    } else {

      return null;

  }
};

Translate
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