4 Replies Latest reply on Jul 14, 2009 1:05 PM by [Jongware]

    Writing Scripts


      I am looking to write a script for starters.

      I think once I get the hang of it I will have more.

      First off, I have a 4 step process for formatting a directory that I publish on a quarterly basis.

      It is all generated in HTML so I can run the report and get the data I need

      BUT it doesn't format exactly the way I want it to when I put it in InDesign.

      I have 4 saved find/change steps that I run in a specific order to achieve the final formatting.

      How would I script the find change steps to run each find/change step rather than running each one at a time?


      Thanks in Advance

        • 1. Re: Writing Scripts
          SuperMacGuy Level 2

          You should tell us what platform/language you want to use.


          I also hate to be the less-than-useful responder, but there are many sample scripts that come with your InDesign/Creative Suite disks. There are some find/change examples that you can copy/paste and change your specific info. That should get you started, then you can string a couple operations together for your script.



          • 2. Re: Writing Scripts
            ELWILLIAMSON Level 1

            Oops sorry forgot to mention a few things.

            1. Windows XP

            2. InDesign CS 4

            • 3. Re: Writing Scripts
              ELWILLIAMSON Level 1

              I saw the sample scriipts but wasn't entirely sure what all it it was changing.

              Usually I can pick code apart and alter it but the code was a little tough to decipher.


              I am used to using PC Macros in Office or the ones like in Photoshop where you can "hit record"

              • 4. Re: Writing Scripts
                [Jongware] Most Valuable Participant

                I'd sure love to be able to hit a record button in InDesign As it is, writing even the smallest script boils down to a good reference and some preparing for the unexpected.

                You don't mention your preferred scripting language, but I'd advocate JavaScript over VBScript. Writing JS in itself is easy, and there is lots of documentation on the general issues of the language all over the web. And, as added bonii, you can use your experience for web pages and your scripts will be usable on both Mac and PC versions of InDesign (that also means, you'd get help from both platform afici.. aficio.. users if you ask for help on this forum).


                Let's get you started with a simple search and replace. The very first thing to know is, although Javascript is pretty much an easy programming language (if there is such a rarity), it has to interface with InDesign through what's called a Document Object Model. That's the way InDesign objects are "exposed" to the language.


                app.findTextPreferences = null;

                app.changeTextPreferences = null;


                Now that was easy, wasn't it. "app" holds, at the start of the script, a pointer to the current running 'host' application: the program actually running the script. From within InDesign, it's InDesign


                Notice the semi-colon ending each line. JS does not care about hard returns but the semi-colon tells it the previous command line is finished. You can also write everything on a single line and without spaces inbetweem, like this




                but let's keep things readable, shall we.


                If you find and change text in InDesign, you have to remember to "clear out" the Find/Change dialog settings, or otherwise you might be searching for formatted text, or replace it with the wrong text. For a script, it's the same. The findText and changeText prefs have lots and lots of sub-parameters (such as what text to search and which formatting it should have applied), but fortunately, you don't have to clear them one by one. And 'null' is simply Javascript parlance for "nothing at all, not even 0" ("0" is a perfectly valid number, where "null" is ... nothing ... space ... emptyness ...).


                You can already run this little script, and you'll see that it indeed clears out the Find/Replace dialog. Try it: put "something" in the Find Text, and "Italic" in the Change to Format. Then run the script. If you call up the Find/Change dialog again, you'll see it's empty.


                You mentioned searching and replacing text. Well, let's put it in the proper Find and Replace fields. Documentation says, findTextPreferences has a property 'findWhat' -- the text to find.


                app.findTextPreferences.findWhat = "what";


                -- and changeTextPreferences has a property 'changeTo' -- the text to change:


                app.changeTextPreferences.changeTo = "to";


                You can run this script, but it won't seem to replace a thing (yet). But... if you call up the Find/Change dialog now, you'll see these texts in the Find/Change edit fields. It worked!


                Let's do something. Setting values to properties is one thing, but to actually perform an action, you need an object that performs it and the command to do it (these commands are called 'methods').


                As from within the User Interface, you need to provide a find scope for the operation: application-wide ("All Documents"), document-wide ("Document"), current story only, or selection only. Supposedly, you want it document-wide. Checking the Document object shows this indeed has a method 'changeText', and it has one optional parameter. You can forget about the parameter for now, its default value ("false") is good at the moment. So the full command will look like "Document.changeText()".


                "Document" itself is a kind of object, defined by InDesign (a regular InDesign document, opened in the application). Since you can have more than one document, you'll need to state the actual document that you want to replace in. InDesign can do the replace in any document (that's opened), even if it's not the active one, but to refer to the active one -- the one in front of you now -- it has a shortcut called 'activeDocument'. That's an Application property (and "app" is the current application), so it sounds like this is the proper line:




                If you want to add more find/changes, you don't have to clear the preferences stuff after the first time, since all you are changing is the 'findWhat'/'changeTo' strings, and the rest will stay empty.

                There it is: your first script.