Find/Change Paragraph Style to Cell Style

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Find/Change Paragraph Style to Cell Style

Viewing 7 reply threads
  • Author
    Posts
    • #74189
      Poleroad
      Member

      Sorry to say, I know very little VB programming as such, we are after all publishers, right?

      However, it would be helpful to have a VB Script to Find/Change from Paragraph Style to Cell Style in a Table.

      I found something to the effect that would change Cell Height based on the Paragraph style (which, when I tried it, bombs on my first line). Could I ask you for help?

      Below is the posted script. What are the variables that have to be replaced with my own parameters? And what am I missing in order to make it work? I am using InDesign CS6.

      Sorry for being so inept, but not everybody is good at everything.

      Martin

      Function findTable(obj) {
      while (obj.constructor.name != “Table”) {
      obj = obj.parent;
      if (obj.constructor.name == “Application”) {
      throw “Can’t find table”
      }
      }
      return obj
      }

      var myTable = 0;
      if (app.documents.length > 0 && app.selection.length > 0) {
      myTable = findTable(app.selection[0]);

      //Find styles cells formater
      if (myTable.constructor.name == “Table”) {
      app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
      app.findTextPreferences.appliedParagraphStyle = “Heading 2”;
      var myCellStyle = myTable.findText();
      app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
      }
      if (myCellStyle.length>0)
      {
      for (var c = 0; c<myCellStyle.length; c++) {
      myCellStyle[c][/c].parent.parentRow.height = “20p”; //Change row height as per your requirement } } else{alert(“Not found”);} }

    • #74236
      Poleroad
      Member

      OK, no take on this question, but is there a way to use plain Find/Change to apply Cell Styles based on Paragraph styles?

      Thank you,
      Martin

    • #74238
      David Blatner
      Keymaster

      Looks like our indesign scripters are not online at this time. You cannot use InDesign’s normal find/change to do that, no. I assume it could be done with scripting, though.

    • #74260

      Well, for starters it’s not Visual Basic but Javascript ;)

      It stops on the first line because Javascript is case sensitive and “Function” should be “function”. Apart from that it should work nicely if you change the operation from changing the Row Height to setting the cell style. In the script below, the name of the applied cell style should be in “myCellStyle”.

      When copying your code I found all single and double quotes were transformed into typographically correct “curly” ones. Javascript cannot work with these, they must be straight ones. It’s possible this forum changes them, so please make sure to check them after copying.

      Adobe’s own ExtendScript Toolkit Editor, and other Javascript editors as well (I use TextPad) will apply syntax highlighting to keywords and properly formed text strings. You can use this to check; ‘function’, for example, should blue, just as “while” and “if” and other keywords. A text string without proper quotes appears ‘black’ (i.e., as regular text), but with the proper opening and closing quotes it appears in dark red.

      Also, watch out for runaway comments. The very last line in your post contains one after the ‘//’, and this indicates the comment runs until the end of the line. However, there is a return missing and the ‘else’ part including the closing brackets should be on a new line.

      function findTable(obj)
      {
      while (obj.constructor.name != "Table")
      {
      obj = obj.parent;
      if (obj.constructor.name == "Application")
      {
      throw "Can’t find table";
      }
      }
      return obj;
      }

      var myTable = 0;
      if (app.documents.length > 0 && app.selection.length > 0)
      {
      myTable = findTable(app.selection[0]);

      //Find styles cells formater
      if (myTable.constructor.name == "Table")
      {
      app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
      app.findTextPreferences.appliedParagraphStyle = "Heading 2";
      var myCellStyle = myTable.findText();
      app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
      }
      if (myCellStyle.length > 0)
      {
      for (var c = 0; c < myCellStyle.length; c++)
      {
      myCellStyle[c][/c].parent.appliedCellStyle = "myCellStyle";
      }
      } else
      {
      alert("Not found");
      }
      }

    • #74297
      Poleroad
      Member

      Thank you Jongware, making progress !

      Got past up to line 27 :)

      but on line 28, this one:
      myCellStyle[c][/c].parent.appliedCellStyle = “Header Cell”;

      I got the following error: Offending Text: /

      I assume there is something with [/c] entity that is not accepted.

      Thank you again,
      Martin

    • #74310

      Well paint me fifty shades of red. I checked my script right after posting it, just to make sure the items I mentioned were correct. But- the weird thing is, I saw this same (wrong) construction in your original script and deleted it without thinking too much about it.

      The forum software must be doing something … unexpected here!

      It should look like this .. oh wait, I cannot show it :P It should be just the first set of square brackets with a single c inside. The second set with the slash inside should NOT be there.

    • #74314
      Poleroad
      Member

      I think you deserve 50 shades of Thank you!

      After some tinkering to figure out what were the variable-names and what were my InDesign styles and match them — voila: it worked.

      Now, if I may ask for a bit more of an improvement. As I discovered, one needs to manually place the cursor into the table, is there a way to run the script over an entire document with hundreds of tables. That would be the biggest help.

      Ditto: same for Table Styles. Find all Tables and apply Table Style.

      Thank you again,
      Martin

    • #74384
      Poleroad
      Member

      Hello Jongware,

      I managed to find the script that I was looking for above. I found a script posted by B. Eenkhoorn. That is Convert Paragraph to Table and Apply Table Style. If you have another minute for hand-holding, the script bombs on line 15 and also on the following line:
      for (var i = result.length-1; i >= 0; i-)

      Thank you again,
      Martin

      Here it is:
      ***********************************************************************

      book = app.activeBook;

      for (var i=0; i<book.bookContents.length; i++)
      {
      var currentDoc = app.open (book.bookContents[i].fullName, false);

      // DO THINGS HERE FOR EVERY BOOK

      currentDoc.close(SaveOptions.YES);
      }

      //Script to convert Paragraph to Table and apply table style
      //==================
      app.findGrepPreferences = null;
      app.findGrepPreferences.appliedParagraphStyle = app.documents.firstItem().paragraphStyles.itemByName = “Heading 3″;

      var result = app.documents.firstItem().findGrep();
      for (var i = result.length-1; i >= 0; i-)

      {
      var a_text =(result[i].characters.lastItem().contents == ‘\r’)
      ? result[i].parentStory.characters.itemByRange(result[i].characters.firstItem().index, result[i].characters.lastItem().index -1)
      : result[i];
      var a_table = a_text.convertToTable( );
      }

      app.findGrepPreferences = null;
      var d = app.activeDocument;
      var myTableStyle = myDisplayDialog ();

      d.stories.everyItem().tables.everyItem().appliedTableStyle = myTableStyle;
      d.stories.everyItem().tables.everyItem().clearTableStyleOverrides( true );

      function myDisplayDialog(){
      var myDialog = app.dialogs.add({name:”Avakov Table Stripes”});
      with(myDialog.dialogColumns.add()){
      var myTsDropDown = dropdowns.add({stringList: d.tableStyles.everyItem().name, selectedIndex:d.tableStyles.length-1});
      }
      var myResult = myDialog.show();
      if(myResult == true){
      var tS = d.tableStyles[myTsDropDown.selectedIndex];
      myDialog.destroy();
      }
      else{
      myDialog.destroy();
      exit()
      }
      return tS;
      }

Viewing 7 reply threads
  • You must be logged in to reply to this topic.
>
Notice: We use cookies on our websites to give you a great online experience. If you keep browsing, we'll assume you're ok with this. For more information, see our privacy policy. By closing this banner, you agree to the use of cookies.I AGREENo