9 Replies Latest reply on May 19, 2009 5:13 AM by Phil Frank

    JavaScript Error when running FindChangeByList script in CS4

    mattaca Level 2

      Hello there. InDesign CS4 user here running 10.4.11 on a PowerPC G5.

       

      I recently learned GREP and thought of some very useful find/changes that meet our company's needs before laying out text. All of these find/changes work as desired when run using the find/change dialog box. However, when I tried to replace the existing items in the findchangelist.txt file, I'm getting the following Javascript error:

       

       

      JavaScript Error!

       

      Error Number: 25

      Error String: Expected: :

       

      File: /Applications/Adobe InDesign CS4/Scripts/Scripts Panel/Samples/JavaScript/FindChangeByList.jsx

      Line: 172

      Source:  app.doScript(myString, ScriptLanguage.javascript);

       

       

      I saw at least one other person has had this problem, but there was no reply posted. I also found similar complaints about the script that comes with InDesign CS4 being "broken," but don't know JavaScript well enough to fix the error. Can anyone help? It would be greatly appreciated.

       

      Matthew

       

      P.S. - In case it's helpful, I'll include the script from the text file below. The only instances where I'm applying formatting to text (instead of replacing text) are the 3rd and 10th lines. This is my first experience with scripting, so go easy on me.

       

       

      //Pre-layout.txt
      //A support file for the InDesign CS4 JavaScript FindChangeByList.jsx
      //
      //This data file is tab-delimited, with carriage returns separating records.
      //
      //The format of each record in the file is:
      //findType<tab>findProperties<tab>changeProperties<tab>findChangeOptions<tab>description
      //
      //Where:
      //<tab> is a tab character
      //findType is "text", "grep", or "glyph" (this sets the type of find/change operation to use).
      //findProperties is a properties record (as text) of the find preferences.
      //changeProperties is a properties record (as text) of the change preferences.
      //findChangeOptions is a properties record (as text) of the find/change options.
      //description is a description of the find/change operation
      //
      //Very simple example:
      //text    {findWhat:"--"}    {changeTo:"^_"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find all double dashes and replace with an em dash.
      //
      //More complex example:
      //text    {findWhat:"^9^9.^9^9"}    {appliedCharacterStyle:"price"}    {include footnotes:true, include master pages:true, include hidden layers:true, whole word:false}    Find $10.00 to $99.99 and apply the character style "price".
      //
      //All InDesign search metacharacters are allowed in the "findWhat" and "changeTo" properties for findTextPreferences and changeTextPreferences.
      //
      //If you enter backslashes in the findWhat property of the findGrepPreferences object, they must be "escaped"
      //as shown in the example below:
      //
      //{findWhat:"\\s+"}
      //
      text    {findWhat:"--"}    {changeTo:"^_"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find all dash-dash and replace with an em dash.
      grep    {findWhat:"⁄"}    {changeTo:"/"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find all funky slashes and replace with regular slashes.
      grep    {findWhat:"\d\/\d"}    {myText.otfFraction = true}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Format opentype fractions.
      grep    {findWhat:"(?<=\d) (?=\d\/\d)"}    {changeTo:""}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Remove all spaces from mixed fractions.
      grep    {findWhat:"\x{20}\x{20}+"}    {changeTo:"\x{20}"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find multiple spaces and replace with single space.
      grep    {findWhat:" +$"}    {changeTo:""}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find trailing spaces and delete.
      grep    {findWhat:"([\d\/]+)-([\d\/]+)"}    {changeTo:"$1–$2"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find hyphens in number ranges and replace with en-dashes.
      grep    {findWhat:"(?<=\d) (?=seconds?|minutes?|hours?|days?|weeks?|months?|teaspoons?|tablespoons?|cups?|pounds?|ou nces?|degrees?|inch|inches|\bto\b|\bby\b)"}    {changeTo:"~S"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Replace spaces after some numbers with non-breaking spaces.
      grep    {findWhat:"(\d+)-([\u\l]+)"}    {changeTo:"$1‑$2"}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Replace hyphens after numbers with non-breaking hyphens.
      grep    {findWhat:"([\u\l]+(?=[-‑–]))|((?<=[-‑–])[\u\l]+)"}    {myText.noBreak = true}    {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}    Find all hyphenated words and set to no-break.

        • 1. Re: JavaScript Error when running FindChangeByList script in CS4
          Olav Martin Kvern Level 3

          Hi mattaca,

           

          It's most likely that you either a.) didn't save the file as plain text (the Mac OS Text Edit application, for example, does not save as plain text by default), b.) saved as plain text but did not use the type of line end character that the script expects, c.) did not put tab characters between fields in the file, or d.) did not properly "escape" special characters in the GREP strings you entered (all backslashes, for example, have to be entered as "\\").

           

          You can send me the file directly and I'll check it out. My direct email address is under my user name.

           

          Thanks,

           

          Ole

          1 person found this helpful
          • 2. Re: JavaScript Error when running FindChangeByList script in CS4
            mattaca Level 2

            Hi Olav. I didn't see an email address on your profile, so I sent you a private message. I checked out all the stuff you mentioned and it looks fine, but i keep getting the same error message, always with the same error number and line. Does the type of .txt encoding matter?

             

            I pasted the GREP strings into TextEdit directly from InDesign's Find/Change dialog box, and they work correctly there. To my knowledge, the only thing that didn't paste correctly was a non-breaking space (~S in GREP, which showed up as some sort of invisible space character in TextEdit) so I keyed it in manually.

             

            I'll attach the .txt file here for you to look at. Any help would be appreciated.

             

            Thanks,

            Matthew

             

            P.S. - I'm not sure how to indicate the ChangeProperties when leaving the text as is but applying formatting. That could have something to do with the error.

            • 3. Re: JavaScript Error when running FindChangeByList script in CS4
              Phil Frank Level 1

              I'm having a somewhat similar problem. I'm trying to clean up an imported Word file by running a series of find/change operations to replace local formatting with character styles, and I'd like to combine the series into one findchangebylist operation. I know I'm close, because I can get the first element to run, but after that it breaks down. In the first case, I want to replace local italic with the character style, and this coding works:

               

              grep {findWhat:".",fontStyle:"Italic"} {changeTo:"$0",appliedCharacterStyle:"italic"}

               

              In the second and third instances, I want to replace first the regular and then the bold of one Greek font with another character style, so I don't just need fontStyle, but also fontFamily, or something like that. Here is the coding that doesn't work, so I'm guessing there's something wrong with the syntax:

               

              grep {findWhat:".",fontFamily:"Galatia SIL",fontStyle:"Regular"} {changeTo:"$0",appliedCharacterStyle:"gk"}

               

              And in the next instance, I want to change local small caps to a small caps character style, and this coding doesn't work:

               

              grep {findWhat:".",Case:"small caps"} {changeTo:"$0",appliedCharacterStyle:"scap"}

               

              I've tried several variations on the ones that don't work, but clearly I'm goofing it up somewhere. Any help would be appreciated.

               

              Thanks.

               

              Phil

              • 4. Re: JavaScript Error when running FindChangeByList script in CS4
                Kasyan Servetsky Level 5

                In the first case, I want to replace local italic with the character style, and this coding works:

                 

                grep {findWhat:".",fontStyle:"Italic"} {changeTo:"$0",appliedCharacterStyle:"italic"}

                Hi Phil,

                 

                So long as questions about FindChangeByList script arise quite often I am going to do two things:

                1. Create a page on my site to show how to apply most needed parameters in this script.

                2. Create my own script that records, saves and runs queries, the first, preliminary version is here:

                http://www.kasyan.ho.com.ua/find_change_by_queries.html

                BTW, today I made a new version which I'm going to post tomorrow.

                 

                I tried to use your first line and it doesn't work for me. As far as I know a correct reference to a character style should be app.activeDocument.characterStyles.itemByName("italic") or app.activeDocument.characterStyles.item("italic")

                 

                I wonder how it can work for you. Could you send me your FindChangeByList.txt file?

                 

                Kasyan

                • 5. Re: JavaScript Error when running FindChangeByList script in CS4
                  Phil Frank Level 1

                  Hello Kasyan,

                   

                  I have attached my text file, as you requested. The first line works as expected; the second line turns everything (except the stuff now in "italic" and the bold Greek) in the file into the "gk" character style, including all of the English text that is not Galatia SIL (a Greek font with four weights). The third line then turns everything in the file into "scap", and the fourth line changes my one instance of aaaa into bbbb, which allows me to see that the script didn't abort prematurely, even if it didn't work as hoped.

                   

                  I was trying to follow Cari Jensen's article in ID Magazine 26, but couldn't get her instructions about formatting to work; then I found something in the Adobe forums using this GREP trick, and that worked for me with the italic, but I can't get the rest to work.

                   

                  For another project, I was able to get her Glyph instructions to work just fine. And I will try out your script for saving queries.

                   

                  Thanks for your help and interest.

                   

                  Phil

                   

                  Message was edited by: Phil Frank

                  • 6. Re: JavaScript Error when running FindChangeByList script in CS4
                    mattaca Level 2

                    Thank you, Ole! The file you sent did just the trick!

                     

                    Eventually I'd like to create multiple .txt files that I can use to automate some formatting on different book series we publish. To do that, I need to learn more Javascript. Are the resources provided by Adobe (Introduction to Scripting PDF, Adobe InDesign CS4 Scripting Tutorial PDF) the best place to start? Or is there another book or resource that's a better introduction to Javascript? I'd love some recommendations!

                     

                    Thanks again,

                    Matthew

                    • 7. Re: JavaScript Error when running FindChangeByList script in CS4
                      Olav Martin Kvern Level 3

                      Hi Matthew,

                       

                      For what you're doing, I think Peter Kahrel's O'Reilly e-books on grep and scripting in InDesign would both be good. But there's quite a lot in the Scripting Guide and the associated script archive--I tried to cover the most common uses of scripting.

                       

                      FYI for everyone following this thread--Matthew's file contained two things that were throwing it off that I didn't see when I looked at his email. In this line:

                       

                      grep {findWhat:"\d\/\d"} {myText.otfFraction = true} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false} Format opentype fractions.

                       

                      ...the change properties should be:

                       

                      {otfFraction:true}

                       

                      ...and in this line:

                       

                      grep {findWhat:"([\u\l]+(?=[--–]))|((?<=[--–])[\u\l]+)"} {myText.noBreak = true} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false} Find all hyphenated words and set to no-break.

                       

                      ...the change properties should be:

                       

                      {noBreak:true}

                       

                      Since we're inside a properties record in the above cases, you can't use the the form property = value, you have to use the form property:value. Hope that makes sense!

                       

                      Thanks,

                       

                      Ole

                      • 8. Re: JavaScript Error when running FindChangeByList script in CS4
                        Kasyan Servetsky Level 5

                        Phil,

                         

                        You don't need to use Grep for 2nd and 3rd instances -- use text search type instead:

                         

                        text    {appliedFont:"Galatia SIL\tRegular"}    {appliedCharacterStyle:"gk"}    {includeFootnotes:true, includeMasterPages:false, includeHiddenLayers:true, wholeWord:false}    local to style
                        text    {capitalization:Capitalization.SMALL_CAPS}    {appliedCharacterStyle:"scap"}    {includeFootnotes:true, includeMasterPages:false, includeHiddenLayers:true, wholeWord:false}    local to style

                         

                        Kasyan

                        • 9. Re: JavaScript Error when running FindChangeByList script in CS4
                          Phil Frank Level 1

                          Thanks, Kasyan, this is just the kind of syntax information I was looking for and couldn't find anywhere. I'll give this a try.

                           

                          Phil