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

Run script on character style

Return to Member Forum

  • Author
    Posts
    • #63572
      o-Boo
      Member

      Hello,

      I have just discovered the existence of scripts in indesign…. so sorry for the banility of the question.

      story in short: I have a script that I would like to run al ALL text that is in a specific character style. the character style is applied on different words, with nothing in common, scattered around the regular text (which need not be treated with the script).

      How can I direct the script to run on ALL text in that style? I could find them one by one with find/change but there must be a less tedious way…

      Thanks, Os

    • #63573

      “Find one by one” is actually a good way — of course, it depends on how you do that! The findText command is very reliable for these kind of operations, as it returns a complete list of all occurrences of your character style. The only caveat emptor is that the returned list of occurrences is a static copy; that is, if you change something in the text of the first found item, the references to all next occurrences will be off. The common solution is to work backwards through your found list of items (and findText can return the found list in reverse, so you don't even have to run 'backwards').

      This is only necessary when you are doing something with the actual text, such as inserting or deleting characters, or adding a hyperlink, XML, anchored object or index marker — these all insert invisible codes into text. It's not necessary when changing formatting, such as adding underline or changing the font.

      Here is a sample script that adds underline to all occurrences of a character style (just as a visible example; this could be done better by changing the character style definition!).

      app.findTextPreferences = null;
      app.findTextPreferences.appliedCharacterStyle = “Emphasis”;
      result = app.activeDocument.findText(true);
      for (i=0; i<result.length; i++)
      {
      result[i].underline = true;
      }

      If speed is of the essence, you could venture into the realm of Advanced Scripting. One of InDesign's most powerful scripting commands is “everyItem”. What does this do? It gathers a list of every item of the kind you give a reference to; and it does so with an astonishing speed as well. When the list is gathered, you can inspect its various properties at leisure and decide what to do with each of the items.

      everyItem(), and its often-partner getElements(), is a tough beast to master, and so I would not recommend this for starters :) It took Marc Autret not one but two blog articles to investigate it in full: On 'everyItem()' Part 1 and (no surprise) On 'everyItem()' Part 2. However, just to tease you, here is a variant of the script above that gathers every single formatted text span and then inspects each separate one for your applied character style.

      cstyles = app.activeDocument.stories.everyItem().textStyleRanges.everyItem().getElements();
      for (i=0; i<cstyles.length; i++)
      {
      if (cstyles[i].appliedCharacterStyle.name == “Emphasis”)
      cstyles[i].underline = true;
      }

    • #63574

      Just for laffs, I just timed the above two scripts on a fully formatted 200 pages long scientific thesis. FindText-then-check took 1399 milliseconds, the same thing took everyItem 553 ms; both changed the same 61 occurrences.

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