12 Replies Latest reply on Aug 3, 2015 12:26 PM by DBarranca

    Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.

    darkie1973

      I'm a freelance illustrator that utilizes layer styles extensively in my workflow.  As the rasterization and implementation of a new layer style is constant and continual, I would like a way to streamline the 3-4 actions that I currently use into a single conditional script.  I'm currently using CS6.  Here is the itemized description of how I would like the script to function:

       

          1)    Is the selected layer named "magic layer 1"?  If no, go to line #2.  If yes, go to line #8.

          2)    Is the selected layer named "magic layer 2"?  If no, go to line #3.  If yes, go to line #8.

          3)    Is the selected layer named "magic layer 3"?  If no, go to line #4.  If yes, go to line #8.

          4)    Is the selected layer named "magic layer 4"?  If no, go to line #'s 5-7.  If yes, go to line #8.

          5)    Make new layer.

          6)    Name new layer "magic layer1".

          7)    Add layer style (also named) "magic layer1".  Script concludes.

          8)    Is layer empty?  If yes, go to line #9.  If no, go to line #'s 10-12.

          9)    Queue dialog box:  "End magic layers?"  with choice of "yes" or "no".

          •    If "yes" is selected, the layer is deleted.  Script concludes.

          •    If "no" is selected, the layer is kept (no change).  Script concludes.

         10)   Rasterize layer.

         11)   Rename layer "base paint layer".

         12)   Merge layer down.  Go to line #'s 5-7.

       

      I would prefer javascript if possible, as I'll need to port the script between my Mac desktop and my Windows based Cintiq tablet.  Any and all help would be greatly appreciated; I've put some time into trying to learning scripting, but between the day job and the freelance evenings, I've succeeded only in creating extreme levels of frustration!  LOL!  PLEASE AND THANK YOU!

        • 1. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
          DBarranca Level 4

          Hi,

          give this a try (I've not tested, and also streamlined a bit your logic)

           

          function main() {
            
              var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
          
              var makeNewLayer = function() {
                  var newLayer = doc.layers.add();
                  newLayer.name = 'magic layer1';
                  newLayer.applyStyle('magic layer1');
                  doc.activeLayer = newLayer;
              }
          
              var doc = app.activeDocument,
                  lay = doc.activeLayer,
                  names = ['magic layer 1', 
                           'magic layer 2', 
                           'magic layer 3', 
                           'magic layer 4'];
          
              if (__indexOf.call(names, lay.name) < 0 ) {
                  makeNewLayer();
                  return;
              }
          
              if (doc.activeLayer.kind == LayerKind.NORMAL && doc.activeLayer.bounds[2] == 0 && doc.activeLayer.bounds[3] == 0) {
                  var dialog = confirm("End magic layers?");
                  if (dialog) {
                      app.activeDocument.activeLayer.remove();
                  }
                  return;
              }
              doc.activeLayer.rasterize(RasterizeType.ENTIRELAYER);
              doc.activeLayer.name = "base paint layer";
              doc.activeLayer.merge();
              makeNewLayer();
              return;
          
          }
          
          main();
          

           

          Davide Barranca

          www.davidebarranca.com

          www.cs-extensions.com

          • 2. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
            darkie1973 Level 1

            Thank you so much for the response and the effort that you have already made!  I've tried to test the script in ES toolkit, but I seem to be getting an error on line 6 columne1.    Is there any information that I can provide to aid you in the process?  I'm extremely grateful for the help,... been searching for assistance with this since last November!

             

            -Eric P.

            • 3. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
              pixxxel schubser MVP & Adobe Community Professional

              Hi DBarranca,

              yes, the logic written by the TO darkie1973 is a little bit confusing.

               

              I did not tested your code.

              But are you really sure, that your code  a correct PS-scripting syntax is? (because in Illu layers.add() works – but in PS IMHO should be artLayers.add() )

               

              And why to loop through all the names of layers ( with indexOf … magic … ) ? IMHO to complicated.

              Please try this part instead:

              var aDoc = app.activeDocument;
              var checkName = /magic\slayer\s[1-4]/;
              
              var pass = aDoc.activeLayer.name.match (checkName);
              if ( pass == null ) {
                  newLayer = aDoc.artLayers.add(); // or your own function in the next line
                  //makeNewLayer();
                  } else {
              alert ("vorhanden und aktiv:  " + pass);
                  }
              

               

              Furthermore it's never a good idea to use a style without previous check if this style already exists. (e.g. with a try catch clause)

               

              Greetings (and sorry or my bad english)

               

              • 4. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
                darkie1973 Level 1

                Hi ,

                 

                Okay,... sorry for the confusion.  So, basically, I generally work with approx 3-5 layer styles routinely.  What I am trying to accomplish is that when I use the script it will either:

                 

                a.)  create a new layer and add a "magic layer1" layer style and rename layer "magic layer1" if the active layer IS NOT named "magic layer1", or "magic layer2", etc.

                OR

                b.)  if the active layer IS named "magic layer1",etc., then rasterize active layer, rename it "base paint layer", merge layer down, create a new layer and add a "magic layer1" layer style and rename layer "magic layer1".

                 

                In the event that I activate the script and the active layer is named "magic layer1", etc., and the layer is empty, then a dialog box would pop-up asking "End magic layers?"  Should I choose to end, then the active layer would be deleted.  Should I choose to NOT end, then the active layer would be kept (no change).

                 

                I see your logic in checking the names [1-4],... however, I don't ACTUALLY name all my layer styles "magic layer1,2,3,4", they all have fancy (and various) artistic names that help me remember what they do, which is my reasoning for having each layer style checked individually.  Although my scripting knowledge is SEVERELY LIMITED (pretty much non-existent), I do know enough to be able to go in and adjust the script to accommodate for all of my fancy layer style names; this would also allow me to make adjustments to the script should I need to add or subtract new layer styles in the future.

                 

                Also, as a side note (and perhaps you were actually addressing DBarranca), but I don't know enough about scripting to know where I would need to substitute/include your code additions!?

                 

                Regardless,... I appreciate all the help that you have given/can give.

                 

                -Eric P

                • 5. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
                  darkie1973 Level 1

                  Okay,... so I changed illu layers.add() to artLayers.add(), and it seems to be working much better!  Thank you !  It doesn't seem, however, to rename the layer "base paint layer"?!

                   

                  Also, during the testing of the script, I realized that I need to make a modification to my previous statement:

                   

                  a.) create a new layer named "base paint layer", THEN create a new layer (above "base paint layer") and add a "magic layer1" layer style and rename layer "magic layer1" if the active layer IS NOT named "magic layer1", or "magic layer2", etc.

                   

                  Thanks again,

                  -Eric P

                  • 6. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
                    DBarranca Level 4

                    Hi pixxxel schubser,

                    you're right, I wrote the code as it came to my mind without any further testing, so it's very possible that I run into layers/artLayers mistakes

                     

                    As for the RegExp I thought that providing an array of names is preferable since darkie1973 is not bound to the "magic layer #" pattern and he can easily add other ones without messing with RegExp (ok, this is the official reply - the true one is that I use RegExp very often but I've never been able to like them so when possible I use different code).

                     

                    Little magic in the __indexOf(), it's just a polyfill because ExtendScript doesn't know Array.prototype.indexOf() (see http://feedback.photoshop.com/photoshop_family/topics/-ps-scripting-extendscript-support-f or-ecmascript-6th-version?rfm=1) - mostly, I write in Coffeescript then compile to JS, and __indexOf is a nice addition that I get for free in the compiled code.

                     

                    Cheers,

                     

                    Davide

                    • 7. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
                      DBarranca Level 4

                      Hi Eric,

                      the layer is actually renamed "base paint layer", but just after that (according to your workflow) it merges down, so it acquires the name of the layer below as it happens if you perform the procedure manually.

                       

                      Try this:

                       

                      function main() {
                          
                          var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
                      
                          var makeNewLayer = function() {
                              var currentLayer = doc.activeLayer;
                              var basePaintLayer = doc.artLayers.add();
                              basePaintLayer.name = "base paint layer";
                              basePaintLayer.move(currentLayer, ElementPlacement.PLACEBEFORE);
                      
                              var newLayer = doc.artLayers.add();
                              newLayer.name = 'magic layer1';
                              newLayer.move(basePaintLayer, ElementPlacement.PLACEBEFORE);
                              try {
                                  newLayer.applyStyle('magic layer1');
                              } catch(e) {
                                  throw new Error("Can't find magic layer1 Style");
                              }
                              doc.activeLayer = newLayer;
                          }
                      
                          var doc = app.activeDocument,
                              lay = doc.activeLayer,
                              names = ['magic layer 1', 
                                       'magic layer 2', 
                                       'magic layer 3', 
                                       'magic layer 4'];
                      
                          // If the layer is not named as in names array
                          if (__indexOf.call(names, lay.name) < 0 ) {
                              // create a new layer
                              makeNewLayer();
                              return;
                          }
                      
                          if (doc.activeLayer.kind == LayerKind.NORMAL && doc.activeLayer.bounds[2] == 0 && doc.activeLayer.bounds[3] == 0) {
                              var dialog = confirm("End magic layers?");
                              if (dialog) {
                                  app.activeDocument.activeLayer.remove();
                              }
                              return;
                          }
                          doc.activeLayer.rasterize(RasterizeType.ENTIRELAYER);
                          doc.activeLayer.name = "base paint layer";
                          doc.activeLayer.merge();
                          makeNewLayer();
                          return;
                      
                      }
                      
                      main();
                      

                       

                      As you see I've changed the makeNewLayer function with the extra base layer as you requested (called in line 32) not sure this applies too for the call in line 46, though.

                       

                      -Davide

                      • 8. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
                        pixxxel schubser MVP & Adobe Community Professional

                        Hi Davide or DBarranca,

                        don't worry. I like your code. And I see the same problems when using Regex. But I like Regex (and/or Grep in ID). It is an extremely efficient and a very quickly way – if you use the correct Regex (no problem for me, but mostly for the others).

                         

                        Here in this thread my greatest problem is to understand, what the TO darkie1973 really want to do. Perhaps the barricade between our languages is to high for me, perhaps the explanation is not exactly enough for me. There are to much possibilities remain to find the right code for the TO.

                         

                        And I go in the next days in holidays.

                         

                         

                         

                        Edit:

                        I wrote this at the same time as your post #7

                        And I also like your new code.

                         

                        • 9. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
                          DBarranca Level 4

                          Hi pixxxel schubser,

                          in fact it's not that I don't like RegExp! I admit they are incredibly powerful: but nothing of their rules sticks in my brain after I've used them - each time I need to seek through documentation like the first time. It reminds me when I teach my mother how to use her computer

                          Have some nice holidays! I'll wait just a couple of weeks and then it'll be my turn.

                          Best,

                           

                          -Davide

                          • 10. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
                            pixxxel schubser MVP & Adobe Community Professional

                             

                            If you need one day a special Regex, than ask.

                             

                            And thx for the wishes.

                            • 11. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
                              darkie1973 Level 1

                              Okay guys, was able to play around a bit with what I learned between the two of you and others and came up with this:

                               

                              1)    function main() {

                                  2)          

                                  3)            var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

                                  4)        

                                  5)            var makeNewLayer = function() {

                                  6)               var newLayer = doc.artLayers.add();

                                  7)                newLayer.name = 'magic layer1';

                                  8)                newLayer.applyStyle('magic layer1');

                                  9)                doc.activeLayer = newLayer;

                                  10)            }

                                  11)        

                                  12)            var doc = app.activeDocument,

                                  13)                lay = doc.activeLayer,

                                  14)                names = ['magic layer1', 

                                  15)                         'magic layer2', 

                                  16)                         'magic layer3', 

                                  17)                         'magic layer4'];

                                  18)        

                                  19)            if (__indexOf.call(names, lay.name) < 0 ) {

                                  20)                newLayer = doc.artLayers.add();

                                  21)                makeNewLayer();

                                  22)                return;

                                  23)            }

                                  24)        

                                  25)            if (doc.activeLayer.kind == LayerKind.NORMAL && doc.activeLayer.bounds[2] == 0 && doc.activeLayer.bounds[3] == 0) {

                                  26)                var dialog = confirm("End magic layers?");

                                  27)                if (dialog) {

                                  28)                    app.activeDocument.activeLayer.remove();

                                  29)                }

                                  30)                return;

                                  31)            }

                                  32)            var idrasterizeLayer = stringIDToTypeID( "rasterizeLayer" );

                                  33)        var desc5 = new ActionDescriptor();

                                  34)        var idnull = charIDToTypeID( "null" );

                                  35)            var ref4 = new ActionReference();

                                  36)            var idLyr = charIDToTypeID( "Lyr " );

                                  37)            var idOrdn = charIDToTypeID( "Ordn" );

                                  38)            var idTrgt = charIDToTypeID( "Trgt" );

                                  39)            ref4.putEnumerated( idLyr, idOrdn, idTrgt );

                                  40)        desc5.putReference( idnull, ref4 );

                                  41)        var idWhat = charIDToTypeID( "What" );

                                  42)        var idrasterizeItem = stringIDToTypeID( "rasterizeItem" );

                                  43)        var idlayerStyle = stringIDToTypeID( "layerStyle" );

                                  44)        desc5.putEnumerated( idWhat, idrasterizeItem, idlayerStyle );

                                  45)    executeAction( idrasterizeLayer, desc5, DialogModes.NO );

                                  46)    

                                  47)            doc.activeLayer.merge();

                                  48)            doc.activeLayer.name = "base paint layer";

                                  49)            makeNewLayer();

                                  50)            return;

                                  51)        

                                  52)        }

                                  53)        

                                  54)        main();

                               

                              I know that's a bit of ugly code from lines 32-45, but I realized (through other channels, Thanks & !) that there's no way to script the rasterization of a "Layer Style" other that using ScriptingListener.plugin.    Yeah,... I kinda realized I 'screwed the pooch' on the layer renaming after I played with it for a while.  Also kinda realized that the "new layer" you were kinda enough to add wasn't going to solve my problem; ended up having to add a "new layer" on line 20.  Everything seems to work exactly as I intended it to now.  I DO, though, love the idea of having an error message "Can't find magic layer1 Style".  How would I add that in?

                               

                              Thanks again,

                              -Eric P

                              • 12. Re: Need a conditional script written that involves layer styles and rasterization; Possible payment can be negotiated.
                                DBarranca Level 4

                                I DO, though, love the idea of having an error message "Can't find magic layer1 Style".  How would I add that in?

                                 

                                Just use the makeNewLayer function as I've modified it in the last code (see the try/catch block).

                                Good you've found what you were looking for :-)

                                Regards

                                 

                                Davide