Skip to content
fabiantheblind edited this page May 4, 2016 · 5 revisions

This script takes a peek into the layers.
It makes use of the printable option to make the layer invisible. It is NOT the visible option (the tiny eye on the layer panel). have a deeper look into layers and their props here:
http://jongware.mit.edu/idcs6js/pc_Layer.html

    
    /*
     This script takes a peek into the layers
     It makes use of the printable option to make the layer invisible
     it is NOT the visible option (the tiny eye on the layer panel)
     have a deeper look into layers and their props here:
     http://jongware.mit.edu/idcs6js/pc_Layer.html
    */
    main();
    function main (){
    var redrawState = app.scriptPreferences.enableRedraw; // save the initial state 
    app.scriptPreferences.enableRedraw = true; // enable redraw // seems not to work well
        /*
        Lets create a nice document with lots of presets.
        To do that we define some variables for better handling
        */
    var pw = 200; // the PageHeight
    var ph = 200; // the PageWidth
    var gutter = 23; // the Gutter from the Margins to the page border
    var columns_num = 3; // the columns in the layout
    var columns_gutter = 5;
    /* Create a doc with some properties */
    var doc = app.documents.add({
        documentPreferences:{
            facingPages : false,
            pageWidth     : pw,
            pageHeight     : ph
            },
        viewPreferences:{
            horizontalMeasurementUnits     : MeasurementUnits.MILLIMETERS,
            verticalMeasurementUnits     : MeasurementUnits.MILLIMETERS
            }
        });
    /* set some properties on the pages */
    var page = doc.pages[0];
    page.properties = {
            marginPreferences:{
            top              : gutter,
            left              : gutter,
            right              : gutter,
            bottom              : gutter,
            columnGutter     : columns_gutter,
            columnCount      : columns_num
            }
        };
    var first_layer = doc.layers.item(0); // get the first layer
    first_layer.name = "Printable Layer"; // rename it 
    /*
    Add a text frame wit home properties
    The text frame will be on the first layer.
    Whenever you multiple layers the text frame will be created on the last used / active Layer
    Set or get the active Layer via:
    app.documents.item(0).activeLayer = app.documents.item(0).layers.item(X);
    */
    var first_textframe = page.textFrames.add({
                    geometricBounds:[gutter,gutter,ph - gutter, pw - gutter],
                    contents: "Now You see me!",
                    textFramePreferences:{
                        verticalJustification: VerticalJustification.CENTER_ALIGN
                        }
                    });
    var firstPar = first_textframe.paragraphs[0]; // get the first paragraph
    // set some properties
    firstPar.properties = {
                    pointSize: 23,
                    justification : Justification.CENTER_ALIGN,
                    hyphenation : false
                    };
    /*
    Lets get to the gist.
    you can duplicate layers like everything else.
    When you do that all the objects from the duplicated layer are also duplicated
    */
    var second_layer = first_layer.duplicate(false); // make a copy of the first layer
    second_layer.name = "Non Printable Layer"; // give it a name
    second_layer.printable = false; // now this layer wont show in the preview
    second_layer.textFrames.item(0).remove();// a layer has also a collection of textFrames lets remove the duplicated tf
    alert("I will move the text frame to the non printable layer in 2 seconds"); // Talk a bit
    // set to preview  mode
    // all content of the non printable layer wont show
    app.activeWindow.screenMode = ScreenModeOptions.PREVIEW_TO_PAGE; 
    /*
    all the following actions cant be seen by the user because they happen on a
    non displayed layer via the printable option NOT the visible option
    */
    first_textframe.itemLayer = second_layer; // move the text frame
    first_textframe.contents = "Now You don't"; // add some text just for fun
    first_textframe.itemLayer = first_layer; // now move the tf back to the printable layer
    first_textframe.contents = "Now You see me!"; // change the content
    app.scriptPreferences.enableRedraw = redrawState; // !Achtung! reset this to the initial state!
    };

This solves the problem: "Export a single layer as a new document in InDesign"

Written for this graphicdesign.stackexchange.com question.

        // this script saves the current doc as a new one (with prompt)
        // and removes all layers except for the one
        // the user chooses in a drop down
        // main function
        var main = function() {
          // if there is no doc
          // abort
          if (app.documents.length === 0) {
            return;
          }
        
          var doc = app.activeDocument; // get the current active doc
          var path = doc.filePath; // get the path
        
          var name = prompt("Enter a name"); // get a name
          if (name.length === 0) {
            // if the user is to lazy add the date
            name = (new Date()).toString();
          }
        
          var layernames = [];// to store the layer names
          // loop the layer array
          for (var i = 0; i < doc.layers.length; i++) {
            layernames.push(doc.layers[i].name);// get the name
          }
          // save the doc as a new one
          // under the new name
          doc.save(new File(path + "/" + name + ".indd"));
          // add a small dialog to select the layers
          var diag = app.dialogs.add({
            name: "Layers",
            canCancel: true
          });
          // needs a column for the dropdown
          var column = diag.dialogColumns.add();
          // add the dropdown
          var layerdd = column.dropdowns.add({
            stringList: layernames,
            selectedIndex: 0,
            minWidth: 75
          });
          // show the dialog
          if (diag.show() === true) {
            var ndx = layerdd.selectedIndex;// get the index
            diag.destroy(); // remove dialog
            // move the layer to top
            doc.layers[ndx].move(LocationOptions.AT_BEGINNING);
            // remove all other layers
            for (var j = doc.layers.length - 1; j >= 1; j--) {
              doc.layers[j].remove();
            }
          }
          // THE END
        };
        main(); // run that thing
    ```    
Clone this wiki locally