Script to apply paragraph style to text found in table cell

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Script to apply paragraph style to text found in table cell

Viewing 5 reply threads
  • Author
    Posts
    • #93697
      Kate Basart
      Participant

      I am trying to patch together a script that will find specific phrases that are in various tables in my long document and change the paragraph style. I’m very new to scripting but so far the script I have pieced together is finding the phrases, I just don’t know how to get it to apply the style. I’m not sure if what I need to do next is somehow make sure the found text is selected? Any help would be most appreciated. Thanks!
      Here is what I have so far:
      function checkWhichTable()
      {
      // ensure the user made a selection
      if (app.selection.length != 1)
      return null;
      var currentTable = app.selection[0];
      if (currentTable.hasOwnProperty(“baseline”))
      {
      currentTable = app.selection[0].parent;
      }
      while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column)
      currentTable = currentTable.parent;
      if (!(currentTable instanceof Table))
      {
      // No table selected
      return null;
      }
      return currentTable;
      }
      app.doScript(checkUserSelection, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, “Process Table”);
      function checkUserSelection ()
      {
      var a_table = checkWhichTable();
      if (a_table == null)
      {
      if (confirm(“No table selected. Do you want to process *all* tables?”) == false)
      return;
      allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
      for (aTable=0; aTable<allTables.length; aTable++)
      {
      processTable (allTables[aTable]);
      }
      } else
      {
      processTable (a_table);
      }
      }
      function processTable(table)
      {

      // fit cells to text
      table.rows.everyItem().height = “36pt”;
      table.cells.everyItem().autoGrow == “true”;

      //Find Text in Cell and apply Paragraph Style
      var textInCell=[‘Activity’, ‘Possible Answers’];
      var myRegEx = new RegExp(“\\b(“+textInCell.join(“|”)+”)\\b”);
      for (i=0; i<table.contents.length; i++)
      {

      if (table.contents[i].match(myRegEx))
      {
      table.contents[i].appliedParagraphStyle = “table H2”;
      }
      }
      }

      Thanks!

    • #93701

      function checkWhichTable()
      {
      if (app.selection.length != 1) return null;
      var currentTable = app.selection[0];
      if (currentTable.hasOwnProperty(“baseline”)) currentTable = app.selection[0].parent;
      while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column) currentTable = currentTable.parent;
      if (!(currentTable instanceof Table)) return null;
      return currentTable;
      }

      app.doScript(checkUserSelection, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, “Process Table”);

      function checkUserSelection ()
      {
      var a_table = checkWhichTable();
      if (a_table == null) {
      if (confirm(“No table selected. Do you want to process *all* tables?”) == false)
      return;
      allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
      for (var a = 0; a < allTables.length; a++) processTable (allTables[a]);
      } else processTable (a_table);
      }

      function processTable(myTable)
      {
      myTable.rows.everyItem().height = “36pt”;
      myTable.cells.everyItem().autoGrow = true;
      var myCells = myTable.cells.everyItem().getElements(),
      C = myCells.length,
      myTextInCell = [“Activity”, “Possible Answers”],
      myRegEx = new RegExp(“\\b(” + myTextInCell.join(“|”) + “)\\b”);
      for ( var c = 0; c < C; c++) if (myCells[c].texts[0].contents.match(myRegEx)) myCells[c].texts[0].paragraphs[0].appliedParagraphStyle = “table H2”;
      }

      //__(^/)

    • #93702
      Kate Basart
      Participant

      Thank you Obi-wan! Almost perfect except only works on the first instance in any given cell. Is it possible to change it to grab any instance in all cells?

    • #93717

      function checkWhichTable() {
      if (app.selection.length != 1) return null;
      var currentTable = app.selection[0];
      if (currentTable.hasOwnProperty(“baseline”)) currentTable = app.selection[0].parent;
      while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column) currentTable = currentTable.parent;
      if (!(currentTable instanceof Table)) return null;
      return currentTable;
      }

      app.doScript(checkUserSelection, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, “Process Table”);

      function checkUserSelection() {
      var a_table = checkWhichTable();
      if (a_table == null) {
      if (confirm(“No table selected. Do you want to process *all* tables?”) == false)
      return;
      allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
      for (var a = 0; a < allTables.length; a++) processTable (allTables[a]);
      } else processTable (a_table);
      }

      function processTable(myTable) {
      myTable.rows.everyItem().height = “36pt”;
      myTable.cells.everyItem().autoGrow = true;
      var myParas = myTable.cells.everyItem().texts[0].paragraphs.everyItem().getElements(),
      P = myParas.length,
      myTextInCell = [“Activity”, “Possible Answers”],
      myParaStyle = “table H2”,
      myRegEx = new RegExp(“\\b(” + myTextInCell.join(“|”) + “)\\b”);
      for ( var p = 0; p < P; p++) if (myParas[p].contents.match(myRegEx)) myParas[p].appliedParagraphStyle = myParaStyle;
      }

      //__(^/)

    • #93730
      Kate Basart
      Participant

      Fantastic, works perfectly. Thank you!

    • #93731

      Cool!

      (^/) Join The Rebellion!

Viewing 5 reply threads
  • You must be logged in to reply to this topic.
>