Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Table script for setting var column widths

Return to Member Forum

  • Author
    Posts
    • #84777

      Hi All,

      I found this script somewhere on one of the Adobe forums I could use (written by Ramkumar. P). When I try to run the script, it shows an error message. Now, I know virtually nothing about scripting. Is the Java or Applescript? Is it outdated or broken somewhere?

      Thanks, Flo

      It says:

      Number of columns in a table may vary depends on the book.
      If the columns count is constant then you can use the below script.

      var myDoc = app.activeDocument;
      var myWidths = [100, 100, 150, 150];
      for(var T=0; T < myDoc.textFrames.length; T++){
      for(var i=0; i < myDoc.textFrames[T].tables.length; i++){
      for(var j=0; j < myWidths.length; j++){
      myDoc.textFrames[T].tables[i].columns[j].width = myWidths[j];
      }
      }
      }
      alert(“Table width updated successfully…”);

      In the above script i declared the 4 columns width(s) as 100pt, 100pt, 150pt & 150pt. Change width acording to your requirement.
      I think this will give you the solution.

      Regards,
      Ramkumar. P

    • #84786
      Ari Singer
      Member

      This is JavaScript.

      I’m afraid you included his comments in the script, without ‘commenting it out’. The script should look like this:


      // Number of columns in a table may vary depends on the book.
      // If the columns count is constant then you can use the below script.
      var myDoc = app.activeDocument;
      var myWidths = [100, 100, 150, 150];
      for(var T=0; T < myDoc.textFrames.length; T++){
      for(var i=0; i < myDoc.textFrames[T].tables.length; i++){
      for(var j=0; j < myWidths.length; j++){
      myDoc.textFrames[T].tables[i].columns[j].width = myWidths[j];
      }
      }
      }
      alert(“Table width updated successfully…”);
      // In the above script i declared the 4 columns width(s) as 100pt, 100pt, 150pt & 150pt. Change width acording to your requirement.
      // I think this will give you the solution.

      Make sure to save it as a .jsx file and place it in the Scripts panel and run it from there.

      • #117552
        Ha Ab
        Member

        Hi.
        How can I appliedCellStyle on the column[j]?

    • #84849

      Thank you Ari, but still.. it doesn’t work :\

      Is the width always in points, or given by the program prefs?

    • #84862
      Ari Singer
      Member

      No, the width is only in points if it’s set up in the prefs.
      I added a few lines to the script to automatically change the prefs to points, and then set it back to what it was before.


      var myDoc = app.activeDocument;
      with (myDoc.viewPreferences){
      var myOldXUnits = horizontalMeasurementUnits;
      var myOldYUnits = verticalMeasurementUnits;
      horizontalMeasurementUnits = MeasurementUnits.points;
      verticalMeasurementUnits = MeasurementUnits.points;
      }
      //At this point, you can perform any series of script actions
      var myWidths = [100, 100, 150, 150];
      for(var T=0; T < myDoc.textFrames.length; T++){
      for(var i=0; i < myDoc.textFrames[T].tables.length; i++){
      for(var j=0; j < myWidths.length; j++){
      myDoc.textFrames[T].tables[i].columns[j].width = myWidths[j];
      }
      }
      }
      alert(“Table width updated successfully…”);
      with (myDoc.viewPreferences){
      try{
      horizontalMeasurementUnits = myOldXUnits;
      verticalMeasurementUnits = myOldYUnits;
      }
      catch(myError){
      alert("Could not reset custom measurement units.");
      }
      }

    • #84866

      Keep getting the syntax error.

      Looking into the code, I can change the numbers betweet the brackets [100, 100 etc]?

      Do I have to fill in any number on the for(var T=0 / i=0 / j=0 also to make it work?

    • #84868
      Ari Singer
      Member

      No, you don’t change that. Can you please tell me what it says when the syntax error pops up?

    • #84885

      It says something like:

      Error: 8
      Syntax Error

      Engine: main
      File (etc)
      Line:18
      Source: alert(“Table width updated sucessfully…”);
      Responsible text: ”

      Ooh, I just fixed this one with Extendscript Toolkit (I found the debug function there). There where typographic quotes instead of dumb ones.

      But now it still stops at:
      with (myDoc.viewPreferences){

      Anyway, I found out this script will change all the colums in the document and that’s not what I was looking for.

      This was what I needed:

      var myDoc = app.activeDocument;
      var myWidths = [100, 100, 150, 150];
      for(var j=0; j < myWidths.length; j++)
      {
      app.selection[0].columns[j].width = myWidths[j];
      }

      So simple, so efficient;) After all, it was good to dive into the world of table scripting and all the possibilities there are. Ari, thanks for your time and response.

    • #14366612

      Hello.

      I have two questions:

      1. Would there be a way to call a dialogue box where I could input the values for the column widths?
      That way I could adapt it according to my document.

      2. How can this script be modified to apply only to a selected table? Again with a dialogue box asking for the widths.

      Thank you.

    • #14366659

      Hi Stephane, try this for Selected Table Only

      //Start Simple Window
      var w = new Window (‘dialog’);
      w.text = ‘Change Table Columns Width’;
      w.preferredSize.width = 280;
      w.group = w.add (‘group’);
      var RadioPanel1 = w.add(‘panel’, undefined, undefined, {name: ‘myGroupS’});
      var StaticaStart = RadioPanel1.add(‘statictext’, undefined,’Enter Table Columns Width’);
      var myString = RadioPanel1.add (‘edittext’, undefined, ‘100, 100, 150, 150’, {readonly: false}); //read only prevent user Entering Nums
      myString.alignment = ‘center’;
      myString.characters = 20;
      w.buttons = w.add (‘group {alignment: “center”}’);
      w.ok = w.buttons.add (‘button {text: “OK”, enabled: true}’);
      w.buttons.add (‘button {text: “Cancel”}’);
      //After Interface
      //Showing the Dialog
      var a = w.show()
      //if User Hit Cancel
      if(a == 2){
      exit(0);
      }

      //Run with UNDO Enabled
      app.doScript(doColumnWidth, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, “Change Selected Table Column Width”);

      function doColumnWidth(){
      //Convert String to Array
      var WidthVar = (myString.text);
      var TargetWidthArray = eval(‘[‘+WidthVar+’]’);
      //Script
      var mySelection = app.selection[0];
      var myWidths = TargetWidthArray;
      for(var i=0; i < mySelection.tables.length; i++){
      for(var j=0; j < myWidths.length; j++){
      mySelection.tables[i].columns[j].width = myWidths[j];
      }
      }
      alert(“Table width updated successfully…”);
      }

Viewing 8 reply threads
  • You must be logged in to reply to this topic.
Forum Ads