4 Replies Latest reply on Feb 13, 2012 7:17 AM by rwumarbio

    Script to find/replace regular to Italic based on search terms in .txt


      Sorry for such a newbie post.


      I have been reading on JS scripts for find/replace.  I'm hoping develop a script or find one that is written that


      1. Looks into a .txt or any searchable file format, containing search terms

      2. Script will run on the entire Indesign document

      3. For any matching search terms script replace the style from regular to Italic, or from Bold to Bold/Italic. 

        • 1. Re: Script to find/replace regular to Italic based on search terms in .txt
          [Jongware] Most Valuable Participant

          Perfectly doable. Do you want to give it a go yourself?


          A few first thoughts (things I'd probably solve myself while writing the script):


          1. JS can read any file, but getting just the plain text out of an MS Word document is kind of ambitious -- even for me. Keep it simple: if your search text is nothing but a list of keywords or short phrases, write them out in a plain text file, one search item per line.


          2. With that I presume you mean not on one single continuous story, but all over the file -- including loose text frames, master pages, and what-have-you-got. Thass' easy, you can set the target for a Find or Replace operation with pin-point accuracy.


          3. .. This is the interesting part, actually.

          (a) Do you want to change the text style? That will set an override "+" for the modified text. It would be safer (wiser, etc.) to change it using a character style. (For the script it doesn't matter which method you use, this is just for the Future You who is going to edit this file in 6 months.)


          (b) If you do use a character style: what should happen if there already is one applied? Not bold or italic, but underline, for instance. Applying a new char style will override this one.


          (c) The naive approach (which I probably would have taken first) is to first search for each of the search terms, go over the 'found' list and test for each single entry if it's in regular or bold, then take appropriate action.

          A better approach -- without knowing anything else about this document -- could be to blindly use Replace to change ALL search terms in "Regular" to "Italic" and ALL search terms in "Bold" to "Bold Italic". It sure would be faster.


          (d) Just like in the interface, you can search for a word and 'change' only the formatting -- no need to change the text as well. And just like in the interface, you need to specify "whole words" and/or "case sensitive".

          • 2. Re: Script to find/replace regular to Italic based on search terms in .txt
            rwumarbio Level 1

            Thanks for the reply.  I'm still learning the Adobe commands and such.  I've done a bit of JS work for another platform, but I'm not sure how much work I'm going to be doing within Adobe.  So I might try it and let everyone get a laugh out of my way of coding, but its a good learning tool for understanding how the program works. 


            1.  Yes I think a .txt file is likely the best, these are two word names for the most part. 

            2.  I currently use Paragraph and Character styles.  I just find/replace using the built in function within Adobe and it preforms what i need.  Just looking to automate it.


            3. a) I'd like to just have it do an overide of the stlye, the reason is that these words always have to Italic and we don't generaly go back to old documents.  Once we produce them they are set, baring some small changes at the beginning.  There are a lot of different character and paragraph styles in document, and the find/replace as I'm currently doing it works great and doesn't cause any issues.

                 c) I will let it replace all from the list as any term in the list must be Italics.  Script will only do this one type of change. 

                 d) yes I will just change the font style nothing else.  I think I saw the produedure listed in a recent post but I have to track it back down.


            Thanks for the help.

            • 3. Re: Script to find/replace regular to Italic based on search terms in .txt
              [Jongware] Most Valuable Participant

              Go ahead, make us laugh -- I promise we'll be gentle


              Some previous experience with JS is good, it doesn't matter on what platform. The only thing to watch out for is things that you took for granted but were in fact not part of JS but of the surrounding DOM (Document Object Model). For example, if you worked with web pages, you would have used the HTML DOM without even being aware of it.


              The InDesign DOM is comp-lick-a-ted ... but that's because InDesign is complicated! And in fact it's a good thing too; fortunately the programmers took care to make virtually every function in ID available to scripters (one exclusion, for example, seems to be "Show Text Threads" -- and no-one ever mentioned that in the decade I've been working with ID/JS, so it's not like it is something important).


              To get your head around ID's DOM, I'll gladly recommend "the best" reference, that is, my own: http://www.jongware.com/idjshelp.html, because even though it contains the exact same text as Adobe's own help, mine looks nicer, is hyper-referenced, searchable, and shows a graphical hierarchy of most object relations.

              • 4. Re: Script to find/replace regular to Italic based on search terms in .txt
                rwumarbio Level 1

                Ok, so I have it working with the FindChangebyList that comes with Indesign.  I just put the style in the text search.


                text{findWhat:"XXX"}{changeTo:"XXX",fontStyle:"Bold Italic"}{includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}Find XXX and replace with XXX



                I guess I'll have to learn how it works to make the last a bit easier to use.  I'll want just a list of terms in the end. I can script this with VBA in excel to give me a fresh .txt that currently works, but that really shows my inabilty to do good work.