1 Reply Latest reply on Jun 9, 2018 12:26 AM by Michel [FRIdNGE]

    Help to improve script with rounded corners

    franz joelv69400054

      Hi,

      I need help, I'm a novice in javascript, but I like to automate

      my query is that the tables would like to give the effect of rounded edges. Searching the web I found 2 scripts that I would like to merge.

      The first works like this:

      The table is selected and when applying the script, I see options of corner, after selecting one, a floating frame appears on top of the table with the effect I chose.

       

      Adobe InDesign CS4 - Sin título-1 con script por mejorar.indd @ 150%.jpg

       

      PrtScr capture.jpg

       

      Adobe InDesign CS4 - Sin título-1 con script por mejorar.indd @ 300%.jpg

       

       

       

      var myCornerRadius;
      var myCornerOptionsIndex;
      main();
      function main()
      {
          if (app.documents.length == 0) 
          {
              alert ("The document is not open. Open the document first");
              return;
          }
          if(app.selection.length > 0)
          {
                  if(app.selection[0].constructor.name == "Table" )
                  {
                          if( myDialog()  ) {
                              myTest(app.selection[0]);
                              roundedTable(app.selection[0]);
                          }
                  }
                  else
                  {
                      alert("It is necessary to select the table in text mode");
                      return;
                  }
              }
          else 
              {
              alert("Select the table in text mode and run the script again");
              return;
              }
      } // main()
      
      function myDialog()
      {
          var myDialog = app.dialogs.add({name:"Corner Options"});
          with(myDialog){
              with(dialogColumns.add()){
                  with(borderPanels.add()){
                      with(dialogRows.add()){
                          with(dialogColumns.add()){
                              staticTexts.add({staticLabel:"Effect:"});
                           }
                          with(dialogColumns.add()){
                              var myEffectsMenu = dropdowns.add({stringList:["None", "Fancy", "Bevel", "Inset", "Inverce Rounded","Rounded"], selectedIndex:5});
                           }
                     } // with(dialogRows.add()){
                      with(dialogRows.add()){
                          with(dialogColumns.add()){
                              staticTexts.add({staticLabel:"Point Size:"});
                          }
                          with(dialogColumns.add()){
                              var myPointSizeField = realEditboxes.add({editValue:10});
                          }
                      } //with(dialogRows.add()){
                      
                  } // borderpanel
              } // with(dialogColumns
          } // with(myDialog)
      // end of dialogue
              if (  !myDialog.show()  ) 
              {
                 myDialog.destroy();  
                  return false;
              }
      myCornerRadius = myPointSizeField.editValue;     
      myCornerOptionsIndex = myEffectsMenu.selectedIndex;
      myDialog.destroy(); 
      return true;
      }  // fnc
      
      function roundedTable(myTable)
      {
      var myDocument = app.activeDocument
      with (myDocument.viewPreferences){
          // Save the old units in variables OldXUnits, myOldYUnits
          var myOldXUnits = horizontalMeasurementUnits;
          var myOldYUnits = verticalMeasurementUnits;
          // We set new units of measurement
          horizontalMeasurementUnits = MeasurementUnits.points;
          verticalMeasurementUnits = MeasurementUnits.points;
      }
      
      //----------------------------------------------------------
      // The main script code
      //----------------------------------------------------------
          var myH = myTable.height;
          var myW = myTable.width;
          var myStrokeWeight = myTable.topBorderStrokeWeight;
          var myTextFrame = myTable.parent;
          var myParagraphs =myTextFrame.paragraphs;
      
          if((myTextFrame.geometricBounds[3] - myTextFrame.geometricBounds[1] ) >= myW || (myTextFrame.geometricBounds[2] - myTextFrame.geometricBounds[0] ) >= myH)
          {
              DelTableEdge(myTable);
              for (var i=0; i< myParagraphs.length; i++)
                  {
                      if(myParagraphs[i].tables[0] == myTable)
                      {
                          var myTablePara =myParagraphs[i]; 
                          break;
                      }
                  } // for
              
             var myPrevPara = myParagraphs[i-1];
             myTablePara.leading = 0;
      // Insert a frame    ****************   
             var myBorder = myPrevPara.insertionPoints[-1].rectangles.add();
      // Define the properties of the inserted frame
              var myNoneSwatch = app.activeDocument.swatches.item("None");
              myBorder.fillColor    = myNoneSwatch;
              //myBorder.strokeColor= myStrokeColor;
              myBorder.strokeWeight = myStrokeWeight;
      // Set binding parameters
              with(myBorder.anchoredObjectSettings)
              {
                      anchoredPosition = AnchorPosition.anchored;
                      anchorPoint = AnchorPoint.TOP_LEFT_ANCHOR
                      horizontalReferencePoint = AnchoredRelativeTo.anchorLocation;
                      horizontalAlignment = HorizontalAlignment.leftAlign;
                      anchorXoffset = myTablePara.firstLineIndent;
                      verticalReferencePoint = VerticallyRelativeTo.lineBaseline;
                      anchorYoffset = myTable.spaceBefore; 
                      anchorSpaceAbove = 0;
              }
          
      // Rounding type of corners         
               switch (myCornerOptionsIndex)
               {
                      case 0:
                          myBorder.cornerOption = CornerOptions.NONE;
                      break;
                      case 1:
                          myBorder.cornerOption = CornerOptions.FANCY_CORNER;
                      break;
                      case 2:
                          myBorder.cornerOption = CornerOptions.BEVEL_CORNER;
                      break;
                      case 3:
                          myBorder.cornerOption = CornerOptions.INSET_CORNER;
                      break;
                      case 4:
                          myBorder.cornerOption = CornerOptions.INVERSE_ROUNDED_CORNER;
                      break; 
                      case 5:
                          myBorder.cornerOption = CornerOptions.ROUNDED_CORNER;
                      break;                  
                } //switch
      // radio
               myBorder.cornerRadius = myCornerRadius;
               myBorder.strokeColor = app.activeDocument.swatches.item("Black");
              
              
              var myTextFrameGB = myTextFrame.geometricBounds;
              var anchorGB = myBorder.geometricBounds;
              anchorGB[2] = anchorGB[0] + myH;
              anchorGB[3] = anchorGB[1] + myW;
              switch(myTablePara.justification)
              {
                  case Justification.LEFT_ALIGN:
                  case Justification.LEFT_JUSTIFIED:
                  case Justification.FULLY_JUSTIFIED:
                  break;
                  case Justification.CENTER_ALIGN:
                  case Justification.CENTER_JUSTIFIED:
                      myBorder.anchoredObjectSettings.anchorPoint = AnchorPoint.TOP_CENTER_ANCHOR;
                      myBorder.anchoredObjectSettings.horizontalAlignment = HorizontalAlignment.centerAlign;
                  break;
                  case Justification.RIGHT_ALIGN:
                      myBorder.anchoredObjectSettings.anchorPoint = AnchorPoint.TOP_RIGHT_ANCHOR;
                      myBorder.anchoredObjectSettings.horizontalAlignment = HorizontalAlignment.rightAlign;                
                  break;
                  default:
                  break;
               } // switch
      
              myBorder.geometricBounds = anchorGB;
          }
          else
          {
              alert("The size of the table is larger than the size of the frame.");
              exit();
          }
      
      
      // Return the Indies to the old units of measure
              with (myDocument.viewPreferences){
                  try{
                      horizontalMeasurementUnits = myOldXUnits;
                      verticalMeasurementUnits = myOldYUnits;
                  }
                  catch(myError){
                      alert("Could not reset custom measurement units.");
                  }
              } /// with
      }// fnc
      
      
      function DelTableEdge(myTable)
      {
           myTable.bottomBorderStrokeWeight = myTable.topBorderStrokeWeight = 0;
          myTable.leftBorderStrokeWeight = myTable.rightBorderStrokeWeight = 0;
          var myRows = myTable.rows;
          var myNRows = myRows.length
          var myFirstRow = myRows[0];
          var myLastRow = myRows[myNRows-1];
          
           var myColumns = myTable.columns;
           var myNColumns = myColumns.length;
           var myFirstColumn = myColumns[0];
           var myLastColumn = myColumns[myNColumns-1];
       // Up and down    
          for (var r = 0; r < myNColumns; r++)
          {
               try {
                      myFirstRow.cells[r].topEdgeStrokeWeight = 0;
                      myLastRow.cells[r].bottomEdgeStrokeWeight = 0;
                      }
              catch(_) {}
           }
       // Left and right side
           for (var c = 0; c < myNRows; c++)
           {
               try {
                       myFirstColumn.cells[c].leftEdgeStrokeWeight = 0;
                       myLastColumn.cells[c].rightEdgeStrokeWeight = 0;
                      }
                  catch(_) {}
           }   
      } // fnc
      
      function myTest(myTable)
      // Check if the table is in the first paragraph of the text frame
      {
          var myTextFrame = myTable.parent;
          var myParagraphs =myTextFrame.paragraphs;
          for (var i=0; i< myParagraphs.length; i++)
                  {
                      if(myParagraphs[i].tables[0] == myTable)
                      {
                          var myTablePara =myParagraphs[i]; 
                          break;
                      }
                  } // for
          if(!i)
          {
              myParagraphs[0].insertionPoints[0].contents ="\r";
              myTextFrame.recompose;
          }
          
      } // 
      
      
          var a = app.activeDocument.allPageItems, t;  
            
          while( t = a.pop() )  
              {  
              t.isValid &&  
              t.hasOwnProperty('anchoredObjectSettings') &&  
              (t.parent instanceof Character) &&  
              (t=t.anchoredObjectSettings).isValid &&  
              t.releaseAnchoredObject();  
              }  
      

       

      The second works like this

      I select any text and when applying the script the text happens to be in an independent text frame.

       

      Adobe InDesign CS4 - Sin título-1 con script por mejorar.indd @ 200%.jpg

      Adobe InDesign CS4 - Sin título-1 con script por mejorar.indd @ 200%_2.jpg

       

       

      app.doScript(function() {
      if (app.selection.length == 1 && app.selection[0].hasOwnProperty("baseline") && app.selection[0].length > 0)
      {
      frame = app.selection[0].parentTextFrames[0].duplicate();
      frame.texts[0].remove();
      frame.move (undefined, [0, app.selection[0].characters[0].baseline- app.selection[0].parentTextFrames[0].characters[0].baseline]);
      app.selection[0].move(LocationOptions.AT_BEGINNING, frame.texts[0]);
      while (frame.texts[0].characters[-1].contents == 'r')
      frame.texts[0].characters[-1].remove();
      frame.fit (FitOptions.FRAME_TO_CONTENT);
      frame.select();
      }
      }, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Frame from Selection");
      

       

      You could merge these two scripts so that you can first choose the type of vertex and that when accepting the result is the table outside the main text frame (in an independent frame) but within the frame with the effect of the corner.

      Thanks for your help