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

Find, copy, and paste all text styled with a character style?

Return to Member Forum

  • Author
    Posts
    • #61090
      girljosh
      Member

      I'm trying to find all of the text in a document styled with a certain character style, then copy and paste it as a list into another document.

      For example, I have all of the phone numbers in a brochure styled with character style “phonenumber,” and want to find all of them and copy the list to another doc (a directory, say).

      Making a TOC almost works, but since it's based on paragraph styles instead of character styles, it grabs whole paragraphs and makes a list of them. That's bascially what I'm trying to acheive, only with character-styled words within text.

      Anyone have any ideas or scripts? Thanks.

    • #61091

      That was a fun 5-minute exercise!

      I did take a shortcut, though — copying the text immediately into a new document is sort of a bother. You have to draw an empty text frame in your source document first and put the text cursor into it. Then run this script (make sure to fill in the correct character style first).

      app.findTextPreferences = null;
      app.findTextPreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item(“phonenumber”);
      sourcelist = app.activeDocument.findText().reverse();
      while (sourcelist.length > 0)
      app.selection[0].parentStory.insertionPoints[-1].contents = sourcelist.pop().contents+”r”;

    • #61097
      girljosh
      Member

      This is awesome and will be super helpful. Thank you so much.

      At the risk of being greedy, a further challenge:

      Is there any way to have it grab the character-styled bits text box by text box on a page rather than by story so that they are all in order in the resulting list?

      So, in the phone number example, say I have one threaded story containing phone numbers running across all pages, but also have a sidebar with unthreaded text frames (or in my real-life case, anchored text boxes) that contain phone numbers. The current script grabs phone numbers from the long threaded story first, then goes back and grabs them from the sidebar items, so that the resulting list is not in the same order as the phone numbers appear in the doc. Can it grab all of the phone numbers from page 1, then all of the phone numbers from page 2, etc?

      Again, I know I'm being greedy, so don't even think about it if this sounds like no fun. Given your quick reply, however, I suspect you might have another easy solution up your sleeve… Thanks!

    • #61099

      Ahh, challenges are my bread and butter. Preferably, also with a slice of Gouda and a dash of mustard.

      Good thing you mentioned this:

      … in my real-life case, anchored text boxes

      because, indeed, it takes a bit more processing to search inside textframes that are inside textframes. See the nested loop that iterates one level deeper in the frame-that-is-on-a-page.

      if (app.selection.length != 1 || !app.selection[0].hasOwnProperty(“baseline”))
      {
      alert (“Please put your text cursor in a (preferably) empty text frame!”);
      exit(0);
      }
      app.findTextPreferences = null;
      app.findTextPreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item(“Cursief”);
      for (p=0; p<app.activeDocument.pages.length; p++)
      for (f=0; f<app.activeDocument.pages[p].textFrames.length; f++)
      {
      if (app.activeDocument.pages[p].textFrames[f].contents.length)
      {
      sourcelist = app.activeDocument.pages[p].textFrames[f].findText().reverse();
      while (sourcelist.length > 0)
      app.selection[0].parentStory.insertionPoints[-1].contents = sourcelist.pop().contents+”r”;
      for (af=0; af<app.activeDocument.pages[p].textFrames[f].textFrames.length; af++)
      sourcelist = app.activeDocument.pages[p].textFrames[f].textFrames[af].findText().reverse();
      while (sourcelist.length > 0)
      app.selection[0].parentStory.insertionPoints[-1].contents = sourcelist.pop().contents+”r”;
      }
      }

      For some reason findText fails with some non-obvious error message if a text frame does not contain any text (“Object contains no text for find/change”, what's that supposed to mean?) — I expected it just to find nothing — so there is an additional test to check if the frame contains anything at all (the 'contents.length' line).

      Does this come close to what you had in mind?

    • #61100
      girljosh
      Member

      Wow, you rule. This works perfectly and is going to save me a ton of time.

      I owe you a wheel of gouda.

      Thanks again.

    • #61106

      Anything for a ladies' smile.

Viewing 5 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads