4 Replies Latest reply on Nov 23, 2016 5:22 AM by williamadowling

    Script for changing order of layers

    Babymac08 Level 1

      I have multiple files that I need to prepare for Die- Cutting... The art files have been built in Photoshop and saved as a .tif... I then need to place them in an illustrator document in a layer called artwork, which is above the die-cut layer... I have built an action that allows me to pick the file to be placed, (I then need a script it to flip the order of the layers to put the die cut layer on top)... Then the action continues with a print... (then I need the script to flip the layers back the Artwork on top and diecut on bottom), then the action continues with a save.


      Any help would be greatly appreciated

        • 1. Re: Script for changing order of layers
          williamadowling Level 4

          I replied to your question in another post, but i'll put the answer here as well in hopes that it will be more helpful for people who have a similar question in the future.


          function wrapper()
              var docRef = app.activeDocument;
              var layers = docRef.layers;
              var artLayer = layers["Artwork"];
              var thruCutLayer = layers["Through Cut"];
              function bringThruCutForward()
              function sendThruCutBackward()
              function printCommand()
                    //code to print file
          • 2. Re: Script for changing order of layers
            williamadowling Level 4

            I'm moving my reply to your question in the other thread here for better organizational purposes. Let's leave that thread alone and answer your questions here.


            Your question was:


            Thank you so much... Now you have me thinking... I'm not overly familiar with scripts... but This type of set-up I'm creating we run into often... Can you let me know if this is something that could be accomplished in one step...


            1. Run a script on an individual .tif or a folder of *.tif files

            2. Open in Illustrator and place on a layer named "artwork"

            3. Create a new layer named "Through Cut" and make a 1pt stroke that is .25" (H & W) less than the input file size (ie: input file is 48.25 x 96.25 rectangle would be 48 x 96 Centered) However I'd love it if the size could be determined from the input.

            4. Do the layer flip like you previously assisted with putting Through Cut on Top / Artwork on Bottom

            5. Print using a particular print preset (700 w-crops_Bleeds)

            6. Flip the layers back to the order of Artwork on top and Through Cut on Bottom.

            7. Save .pdf file (with PDF preset) with same name as input file.

            8. Close


            What I'm not sure of if it can handle the variable of input file being different sizes and creating the rectangle based off that...


            Thanks again so much for your feedback... It's greatly appreciated.



            Here's my answer:

            I haven't worked with placing images via script so I don't know the gory details, but I don't see any reason that a script couldn't do all of the things that you're asking. It would take a decent amount of time and effort for me to experiment and determine what, if any, limitations there may be and unfortunately i don't have the requisite time available but there are tons of people here who have likely done all of the things you're looking for and they'll surely be glad to help you compile something that works for your purposes.


            If you have any specific questions on how to execute any of those steps i'd be glad to help, but a good first step is to research each step individually and then work on piecing it all together.


            Here's a freebie snippet that resizes the artboard to the size of the placed image, creates your artwork and through cut layers, and adds a rectangle to act as the Through Cut line with .25" bleed. (i wasn't sure whether you wanted .25" all the way around or if you effectively wanted .125" all around. All you need to do is change the value of "bleed" to 9 if you want 1/8th inch bleed on each side.)


            function container()
                var docRef = app.activeDocument;
                var layers = docRef.layers;
                var swatches = docRef.swatches;
                var artboards = docRef.artboards;
                var artLay, cutLay;
                var bleed = 18;
                //these next 5 lines assume an image has already been placed.
                //you will need to make sure an image has been placed first or this will throw an error.
                var theImg = docRef.placedItems[0];
                theImg.left = 0;
                theImg.top = 0;
                var docSize = [theImg.width,theImg.height];
                artboards[0].artboardRect = [0,0,docSize[0],-(docSize[1])];
                function initLayers()
                    artLay = layers.add();
                    artLay.name = "Artwork";
                    cutLay = layers.add();
                    cutLay.name = "Through Cut";
                function addThroughCut()
                    var cutLine = cutLay.pathItems.rectangle(-(bleed),bleed,docSize[0]-(bleed*2),docSize[1]-(bleed*2));
                    cutLine.filled = false;
                    //this line assumes an existing spot color swatch called "Through Cut"
                    cutLine.strokeColor = swatches["Through Cut"].color;
            • 3. Re: Script for changing order of layers
              Babymac08 Level 1

              ok.. With the combination of an Action and Scripts I have this thing rocking perfectly... One last question... I have a stray layer from the beginning called Layer 1 that I'd like to delete in the final file...

              What's the line I'd need to insert to delete that...


              Thank you so much for your help... It's greatly appreciated...

              • 4. Re: Script for changing order of layers
                williamadowling Level 4

                the quick and dirty answer the following line:

                layers["Layer 1"].remove();


                This will get rid of the residual layer, but it's a really good idea to include some kind of checks to make sure that layer exists before you try to delete it or else you'll get an error and everything will crash to a halt. If you know for sure you only want your document to have 2 layers, one called "Artwork" and the other called "Through Cut", then you can run a loop to delete all layers that do not have one of those 2 names.. Like so:


                for(var a = layers.length-1;a>-1;a--)


                     if(layers[a].name != "Artwork" && layers[a].name != "Through Cut")






                This way you're not removing a layer by name, but rather just removing any layer that doesn't match the desired layer names. I'd still recommend some checks in there to make sure there isn't any artwork sitting on the layers you're deleting. Just in case your artwork/image somehow ended up on the wrong layer, you don't want to delete it before printing/saving.


                Good luck. =)