2 Replies Latest reply on Apr 5, 2008 2:13 PM by (Dave_Bubbins)

    JS:CS3 Newbie needs help! find/change character styles

      I'm new to scripting and JavaScript is giving me a headache, but I'll keep trying. What I'm trying to do is automate a few things so that the production time of our school newspaper is reduced while insuring accuracy.

      I've played around a little with the text find change sample and the changing or adding paragraph styles in the InDesignCS3_ScriptingGuide_JS.pdf and I barely understand what I'm doing.

      When us page designers receive text to place in the document, our editors mark the text with tags like: < b >text< / b >, where the word "text" is to be bolded. (there wouldn't any spaces between < and b and > etc. I just did that so It'll show up here). We have basic character styles and paragraph styles set up, and we just started working with nested styles.

      Is there a way we can search for the < b > tag and bold everything after it up until the < / b > tag? Sort of the way it does for html (the way it would do for this message if I took out the spaces). It'll be great if it could remove the tags as well, but if not, I know I could create a find/change script to do the removal afterwards.

      I appreciate any input.

      Thanks!
        • 1. Re: JS:CS3 Newbie needs help! find/change character styles
          Peter Kahrel Adobe Community Professional & MVP
          You've not found yourself the easiest of tasks! You need GREP find/replace, not text. Do you have any experience with GREP? It's worth investigating, it's a powerful feature. The script below handles bold. For clarity, it doesn't do any error checking: it assumes that there's a document with text in it and that the document contains a character style called 'bold'.

          // talk to Indesign, nobody else
          #target indesign

          // reset the Find/Change dialog
          app.findGrepPreferences = app.changeGrepPreferences = null;
          // find everything between <b> and  (including these codes)
          app.findGrepPreferences.findWhat = '<b>.+?</b>';
          // add 'bold' style
          app.changeGrepPreferences.appliedCharacterStyle = 'bold';
          // make the changes
          app.activeDocument.changeGrep();

          // now delete the <b> and </b> codes
          app.findGrepPreferences = app.changeGrepPreferences = null;
          app.findGrepPreferences.findWhat = '</?b>';
          app.changeGrep();

          You can adapt it easily to handle other codes, and at a later stage you could generalise it so that you needn't repeat whole bunches of code.

          Good luck with your first forays. Again, it's not the easiest of tasks, but it's worth getting to grips with.

          Peter
          • 2. Re: JS:CS3 Newbie needs help! find/change character styles
            Level 1
            Thanks Peter, this is exactly what I was looking to do. I know of GREP but the InDesignCS3 Scripting Guide wasn't much help to me. I know a little HTML but this stuff is a lot more complicated. JavaScript books seem more web based and not get in to Indesign specifics.

            I took your code and altered it to look for i tags instead of the b tags to find and apply an italics style. It works like a charm.

            Thanks again.