1 Reply Latest reply on Oct 23, 2009 3:16 AM by [Jongware]

    Why can I not use random in my CS3 JS

    Brian Carter

      I'm working on my Thesis and building a graphic design engine in InDesign. I've done something similar in VB on Windows, but as I use a Mac now, I'm trying to work in Javascript. Still getting my footing though.

       

      A big thing in my engine will be using random numbers. In the CS3 scripting guide there is a script that uses the Math.Random function. I'll reproduce it at the end.

       

      My problem is:I've copied the script line by line and it fails silently and nothing is created. I feel like maybe the Math.random functions aren't working. If anybody has any ideas as to why this script isn't working.

       

      Or, more importantly, could anybody suggest anything to get random functions working at all? Even when I try to pare this script down to it's simplest two lines, I get nothing.

       

      Thanks!

       

      var myDocument = app.documents.add();

      var myPage = myDocument.pages.item(0);

      var myPageWidth = myDocument.documentPreferences.pageWidth;

      var myPageHeight = myDocument.documentPreferences.pageHeight;

      //Create 10 random page items.

      //for(var myCounter = 0; myCounter < 10; myCounter++){


      myRectangle = myPage.rectangles.add;

      myRectangle.geometricBounds = [0,0,5,5];


      myX1 = myGetRandom(0, myPageWidth, false);

      myY1 = myGetRandom(0, myPageHeight, false);

      myX2 = myGetRandom(0, myPageWidth, false);

      myY2 = myGetRandom(0, myPageHeight, false);


      myRectangle.geometricBounds=[myY1, myX1, myY2, myX2];

      // if(myGetRandom(0, 1, true)){

      // myRectangle.label = "myScriptLabel";

      // }

      // }


      //var myPageItems = myPage.pageItems.item("myScriptLabel");

      //if(myPageItems.getElements().length != 0){

      // alert("Found " + myPageItems.getElements().length + " page items with the label.");

      //}



      function myGetRandom(myStart, myEnd, myInteger){

      var myRandom;

      var myRange = myEnd - myStart;

      if(myInteger == true){myRandom = myStart = Math.round(Math.random());}

      else{myRandom = myStart + Math.floor(Math.random()*myRange); return myRandom;}

      return myRandom;

      }

       

       

        • 1. Re: Why can I not use random in my CS3 JS
          [Jongware] Most Valuable Participant

          This appears to be working fine on Windows, CS3, JS:

           

          var myPageHeight = app.activeDocument.documentPreferences.pageHeight;
          myY1 = myGetRandom(0, myPageHeight, false);
          alert (myY1);
          

           

          Every time I run the script it returns a different value. Perhaps someone can try the same on a Mac?

           

           

          [Edit]

          Ha ha ha! Brilliant!

          After copying the entire script it also failed. Took me a few minutes to find the cause.

           

          myRectangle = myPage.rectangles.add();

           

          -- you forgot the parentheses (it's a function).

          I saw nothing in the "Undo" after running the script, and it ought to say something like "Undo Add Rectangle". Then a simple

           

          alert (myRectangle)

           

          said it was 'native code', rather than "Object [Rectangle]'.