4 Replies Latest reply on Aug 24, 2008 4:26 PM by John.Kordas

    CS2 Javascript crashing InDesign works as 2 parts and in CS3.

    John.Kordas Level 1
      I have a script which works fine in CS3 but when I set it up in CS2 I found it was crashing InDesign. I've tried to cut it down to the minimum and set the 2 parts as functions.

      The first function makeMyslug create a new layer called slug and then some text frames on all master pages, with details about the document. The second function printMyslug prints all pages to postscript and adds a page number to the postscript file.

      If I have a document open and run each of the functions separately all is well. If I try and run both function in one go InDesign crashes. The script seems to crash InDesign once the printing process begins.

      NOTE for this example to work you will need to have a document open and make sure there are no blank pages. I'll post in as a reply so that it can be copied if anyone is able to help me out.

      Any suggestions would be greatly appreciated.
        • 1. Re: CS2 Javascript crashing InDesign works as 2 parts and in CS3.
          John.Kordas Level 1
          //Here is the script.

          var mySluglay, myBlueBlk, mySlugChr, mySlugPar, myPageNo, myTitle

          myPageNo = 1;
          myTitle = "Test";

          myDocument = app.activeDocument;

          function makeMyslug(){
          //Create a layer for the slug items.
          mySluglay = myDocument.layers.item("Slug");
          if (mySluglay == null){
          mySluglay = myDocument.layers.add({name:"Slug"});
          }
          //Create a blue background color.
          myBlueBlk = myDocument.colors.item("BlueBack");
          if (myBlueBlk == null){
          myDocument.colors.add({name:"BlueBack", model:ColorModel.process, colorSpace:ColorSpace.cmyk, colorValue:[100, 60, 0, 0]});
          }
          //Create a cahr style.
          mySlugChr = myDocument.characterStyles.item("SlugChar");
          if (mySlugChr == null){
          myDocument.characterStyles.add({name:"SlugChar", appliedFont:"Arial"});
          }
          //Create a para style.
          mySlugPar = myDocument.paragraphStyles.item("SlugPar");
          if (mySlugPar == null){
          myDocument.paragraphStyles.add({name:"SlugPar"});
          }
          // Set units to mm.
          app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS
          app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

          with(myDocument.documentPreferences){
          //Slug
          slugBottomOffset = "20";
          slugTopOffset = "20";
          slugInsideOrLeftOffset = "0";
          slugRightOrOutsideOffset = "0";
          }
          //Set the documents ruler origin to page origin. This is very important
          //--if you dont do this, getting objects to the correct position on the
          //page is much more difficult.
          myDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;

          //Create the slug details on all master pages.
          with(myDocument.masterSpreads.item(0)){

          myDocument.pages[0].appliedSection.continueNumbering = false;
          myDocument.pages[0].appliedSection.pageNumberStart = parseInt(myPageNo);
          var myDetails = "Title: "+myTitle;

          var myPageWidth = myDocument.documentPreferences.pageWidth;
          var myPageHeight = myDocument.documentPreferences.pageHeight;

          mp = app.activeDocument.masterSpreads.everyItem().pages.everyItem().getElements();

          for (i = 0; i < mp.length; i++){

          var myTitleBox = mp[i].textFrames.add(myDocument.layers.item("Slug"), undefined, undefined, {geometricBounds:[myPageHeight+6,10,myPageHeight+14, myPageWidth-10], contents:myDetails});
          myTitleBox.label = "Title";
          myTitleBox.textFramePreferences.verticalJustification = VerticalJustification.bottomAlign;
          myTitleBox.parentStory.paragraphs[0].appliedCharacterStyle = "SlugChar";
          myTitleBox.parentStory.paragraphs[0].appliedParagraphStyle = "SlugPar";
          myTitleBox.parentStory.fillColor = myDocument.swatches.item("Paper");
          myTitleBox.parentStory.pointSize = 10;
          myTitleBox.bringToFront();
          myTitleBox.textFramePreferences.ignoreWrap = true;
          myTitleBox.textFramePreferences.textColumnCount = 1;

          var mycount = app.activeDocument.masterSpreads.item(0).pages.item(0).textFrames.item("Title");

          var myBlueBlock = mp[i].textFrames.add(myDocument.layers.item("Slug"), undefined, undefined, {geometricBounds:[myPageHeight+6, 8,myPageHeight+20, myPageWidth-8]});
          myBlueBlock.label = "BlueBlock";
          myBlueBlock.fillColor = "BlueBack";
          myBlueBlock.textFramePreferences.verticalJustification = VerticalJustification.bottomAlign;
          myBlueBlock.sendToBack();

          var myWhiteBlock = mp[i].textFrames.add(myDocument.layers.item("Slug"), undefined, undefined, {geometricBounds:[myPageHeight+14.5, 9,myPageHeight+19, myPageWidth-9]});
          myWhiteBlock.label = "WhiteBlock";
          myWhiteBlock.fillColor = myDocument.swatches.item("Paper");

          var myPageNoBox = mp[i].textFrames.add(myDocument.layers.item("Slug"), undefined, undefined, {geometricBounds:[myPageHeight-5, 10, myPageHeight+18, myPageWidth-10]});
          myPageNoBox.label = "PageNo";
          myPageNoBox.insertionPoints.item(0).contents = SpecialCharacters.autoPageNumber;
          myPageNoBox.insertionPoints.item(0).contents = SpecialCharacters.emSpace;
          myPageNoBox.insertionPoints.item(0).contents = "Page No";
          myPageNoBox.parentStory.paragraphs[0].appliedCharacterStyle = "SlugChar";
          myPageNoBox.parentStory.paragraphs[0].appliedParagraphStyle = "SlugPar";
          myPageNoBox.textFramePreferences.verticalJustification = VerticalJustification.bottomAlign;
          myPageNoBox.textFramePreferences.ignoreWrap = true;
          myPageNoBox.textFramePreferences.textColumnCount = 1;
          }
          }
          }

          //print function
          function printMyslug(){
          alert("Start print process.");
          myDocument.printPreferences.printer = Printer.POSTSCRIPT_FILE;
          myDocument.printPreferences.ppd = PPDValues.DEVICE_INDEPENDENT;
          for(var myCounter = 0; myCounter < myDocument.pages.length; myCounter++){
          myPageName = myDocument.pages.item(myCounter).name;
          myDocument.printPreferences.pageRange = myPageName;
          //The name of the exported files will be the base name + the page name + ".pdf".
          //If the page name contains a colon (as it will if the document contains sections),
          //then remove the colon.
          var myRegExp = new RegExp(":","gi");
          myPageName = myPageName.replace(myRegExp, "_");
          myFilePath = "/c/idjavascript/test_" + myPageName + ".ps";
          myFile = new File(myFilePath);
          document.printPreferences.printFile = myFile;
          try{
          myDocument.print(false);
          }
          catch(e){
          alert("Page "+myPageName+" may be blank plesae check.");
          }
          }
          }

          //call the makeMyslug function

          makeMyslug();

          //call the printMyslug function

          printMyslug();
          • 2. Re: CS2 Javascript crashing InDesign works as 2 parts and in CS3.
            try67 MVP & Adobe Community Professional
            What do you mean by "crashes"? Is there any error message, or does it simply exit?
            • 3. Re: CS2 Javascript crashing InDesign works as 2 parts and in CS3.
              John.Kordas Level 1
              The error I get is:

              Microsoft Visual C++ Runtime Library

              Runtime Error!

              Program: C:\Program Files\Adobe\Adobe InDesign CS2\InDesign.exe

              This application has requested the Runtime to terminate it in an unusale way. Please contat the application's support team for more information.

              Once I click OK the InDesign is gone.
              • 4. Re: CS2 Javascript crashing InDesign works as 2 parts and in CS3.
                John.Kordas Level 1
                The only thing I found that stop the script crashing InDesign was to comment out the myDocument.print(false) and put in an alert to check if the path was correct and it works.

                I have a feeling that the print process may be trying to execute before the slug area is created.

                Can anyone suggest a way to check that one function or part of the script is complete before the second part starts?