Skip to main content
Participant
September 23, 2017
Question

How to combine two scripts into one?

  • September 23, 2017
  • 1 reply
  • 1338 views

Hi all I am a student who works with the Creative Cloud and recently we started to work with scripts... but I'm no coding pro.

I was wondering if anyone can help me combine these two functions into one and make sure to stop the script if there ARE locked layers in the document and display the alert. (The user has to unlock the layers and run the script again).

Also if you guys can do me an extra favor and add an extra code that will place the last layer on each page which is the background (which is the locked one) as last as well in the new layers if that is possible. Because if I use the second function the background layer is placed between the layers and covers the other items.

I will be really thankful if someone can help me out!

function first () {

  var doc = app.activeDocument;

  for(var i = 0; i < doc.pages.length; i++) {

    $.writeln('\'I\'m on page ' + i);

    var page = doc.pages;

    for(var j = 0; j < page.pageItems.length; j++) {

      var item = page.pageItems;

      if(item.locked === true) {

      // do something to the page item so you can

      // find it visually

        $.writeln('found a locked item');

        item.fillColor = doc.swatches[5]; // normally it is

         alert("Locked Item found,please unlock and start again");

      }

    }

  }

}

first();

Main(); // < cmd (ctrl)+shift+K

// If you want the script to be un-doable, comment out the line above, and remove the comment from the line below

//app.doScript(Main, undefined, undefined, UndoModes.ENTIRE_SCRIPT,"Run Script");

function Main() {

// Check to see whether any InDesign documents are open.

// If no documents are open,the script will display an error message.

if (app.documents.length > 0) {

var myDoc = app.activeDocument;

        myDoc.viewPreferences.showRulers = true;

        myDoc.viewPreferences.showFrameEdges = true;

        myDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;

        myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

        myDoc.viewPreferences.strokeMeasurementUnits = MeasurementUnits.POINTS;

        myDoc.viewPreferences.textSizeMeasurementUnits = MeasurementUnits.POINTS;

        myDoc.guidePreferences.guidesShown = true;

        myDoc.guidePreferences.guidesSnapto = true;

        myDoc.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;

     

       pictureLayer = app.activeDocument.layers.add ({name: "Beelden Laag"});

       textLayer = app.activeDocument.layers.add({name: "Tekst Laag"});

         

     app.activeDocument.rectangles.everyItem().itemLayer = pictureLayer; 

           app.activeDocument.groups.everyItem().itemLayer = pictureLayer;

            app.activeDocument.textFrames.everyItem().itemLayer = textLayer;

       myDoc.transparencyPreferences.blendingSpace = BlendingSpace.CMYK;

      

       

         

         

alert("The changes have been made!");

}

else {

alert("No InDesign documents are open. Please open a document and try again.");

}

}

This topic has been closed for replies.

1 reply

Sunil Yadav
Legend
September 28, 2017

Hi Christopherm,

          You can try this...


Main();

function Main() {
    if(app.documents.length > 0){
        var flag = first();
        if(flag == true){
            var myDoc = app.activeDocument;
            myDoc.viewPreferences.showRulers = true;
            myDoc.viewPreferences.showFrameEdges = true;
            myDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
            myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;
            myDoc.viewPreferences.strokeMeasurementUnits = MeasurementUnits.POINTS;
            myDoc.viewPreferences.textSizeMeasurementUnits = MeasurementUnits.POINTS;
            myDoc.guidePreferences.guidesShown = true;
            myDoc.guidePreferences.guidesSnapto = true;
            myDoc.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;
            pictureLayer = app.activeDocument.layers.add ({name: "Beelden Laag"});
            textLayer = app.activeDocument.layers.add({name: "Tekst Laag"});
            app.activeDocument.rectangles.everyItem().itemLayer = pictureLayer;
            app.activeDocument.groups.everyItem().itemLayer = pictureLayer;
            app.activeDocument.textFrames.everyItem().itemLayer = textLayer;
            myDoc.transparencyPreferences.blendingSpace = BlendingSpace.CMYK;
            alert("The changes have been made!");
            }
        }
    else {
        alert("No InDesign documents are open. Please open a document and try again.");
        }
    }


function first () {
    var doc = app.activeDocument;
    for(var i = 0; i < doc.pages.length; i++) {
        $.writeln('\'I\'m on page ' + i);
        var page = doc.pages;
        for(var j = 0; j < page.pageItems.length; j++) {
            var item = page.pageItems;
            if(item.locked === true) {
                item.fillColor = doc.swatches[5];
                alert("Locked Item found,please unlock and start again");
                return false;
                }
            }
        return true;
        }
    }