9 Replies Latest reply on Feb 21, 2011 2:18 PM by joshuakoomen1

    Connect specified words in textframe with lines

    joshuakoomen1

      Dear all,

       

       

      I want to use the script below and change it to a script that connects specified words with lines. Now it connects all the words with lines. I'm not sure what to do, so I hope someone can help me.

       

       

      Thanks in advance

       

       

      #include ./lib/bene.jsx
      //InDesign CS2 JavaScript
      //Set to 4.0 scripting object model
      app.scriptPreferences.version = 4.0;

       

      ////////////////////////////////////////////////////////////////////////////////////////// ///
      var myScriptName = app.activeScript.name;

       

      var myObjectList = new Array;
      if (app.documents.length != 0){
           if (app.selection.length != 0){
               for(var myCounter = 0;myCounter < app.selection.length; myCounter++){
                   switch (app.selection[myCounter].constructor.name){
                       //allow only textframes
                       case "TextFrame":
                           myObjectList.push(app.selection[myCounter]);
                           break;
                   }
               }
               if (myObjectList.length != 0){
                   //run the thing! call a function           
                   myConnectWords(myObjectList);
               }
               else{
                   alert ("Please select TextFrame and try again.");
               }
           }
           else{
               alert ("Please select an object and try again.");
           }
      }
      else{
           alert ("Please open a document, select an object, and try again.");
      }

       

      ////////////////////////////////////////////////////////////////////////////////////////// ///
      //functions
      ///////////////////////////////////////////////////////////////////////////////////////// ////
      function myConnectWords(myObjectList){
           var myDocument = app.activeDocument;
           var myLayer, myCounter, myColor;
          
           //Measurement
           var myOldRulerOrigin = myDocument.viewPreferences.rulerOrigin;
           myDocument.viewPreferences.rulerOrigin = RulerOrigin.spreadOrigin;
           //Save the current measurement units.
           var myOldXUnits = myDocument.viewPreferences.horizontalMeasurementUnits;
           var myOldYUnits = myDocument.viewPreferences.verticalMeasurementUnits;
           //Set the measurement units to points.
           myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
           myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
          
           //Get references  the None swatch.       
           var myNoneSwatch = myDocument.swatches.item("None");   
          
           //Create a color (if it does not already exist).   
           myColor = myDocument.colors.item(myScriptName);
           try{
               myColorName = myColor.name;
           }
           catch (myError){
               var myColorArray = new Array(100, 0, 0, 0);
               myColor = myDocument.colors.add({model:ColorModel.process,  space: ColorSpace.CMYK, colorValue:myColorArray,name: myScriptName});        
           }
          
           //Create a layer to hold the generated lines (if it does not already exist).
           myLayer = myDocument.layers.item(myScriptName);
           try{
               myLayerName = myLayer.name;
           }
           catch (myError){
               myLayer = myDocument.layers.add({name:myScriptName});
           }
              
           //Process the objects (here text frames) in the selection.   
           for(myCounter = 0; myCounter < myObjectList.length; myCounter ++){
               //get textframe
               var myNewFrame = myObjectList[myCounter];       
              
              
               //get all lines in the frame
               var myLines = myNewFrame.lines;
               //connect each word with all words in the next line
               for(myLineCounter = 0; myLineCounter < myLines.length-1; myLineCounter ++){
                   //get all words in the line + next line
                   var myLine = myLines[myLineCounter].words;           
                   var myNextLine = myLines[myLineCounter+1].words;           
                   //myPathPoints.push([myLine.insertionPoints[-2].horizontalOffset,myTmpY]);

       

                  //combine this + next line words
                   for(i = 0; i < myLine.length; i++){   
                       //get word       
                       var myWord_i = myLine[i];
                       //array for points
                       var myPathPoints = new Array;
                       //add fist point X/Y               
                       myPathPoints[0]=[myWord_i.horizontalOffset,myWord_i.baseline]; 

       

                      for(j = 0; j < myNextLine.length; j++){

       

                          //get word

       

                          var myWord_j = myNextLine[j];                                     

       

                          if (myWord_j.baseline != myWord_i.baseline  && myWord_j.horizontalOffset != myWord_i.horizontalOffset) {

       

                              //add secound point X/Y

       

                              myPathPoints[1]=[myWord_j.horizontalOffset,myWord_j.baseline];

       

                              //draw the line

       

                              myDrawPolygon(myPathPoints, 1, 100, myColor, myNoneSwatch, myLayer);          

       

                          }

       

                      }

       

                  }

       

              }

       

          }  

       

        

       

          myDocument.viewPreferences.rulerOrigin = myOldRulerOrigin;

       

          //Set the measurement units back to their original state.

       

          myDocument.viewPreferences.horizontalMeasurementUnits = myOldXUnits;

       

          myDocument.viewPreferences.verticalMeasurementUnits = myOldYUnits;

       

        

       

      }