3 Replies Latest reply on Jan 30, 2009 11:28 AM by (Olav_Kvern)

    text replace and SpecialCharacters

      Hi, I'm using xml-rules to place xml-content into my document. My problem is to replace paragraph breaks in an element by forced line breaks. I tried:

      > with (element) {
      > app.findGrepPreferences = app.changeGrepPreferences = null;
      > app.findGrepPreferences.findWhat = "\r";
      > app.changeGrepPreferences.changeTo = SpecialCharacters.FORCED_LINE_BREAK;
      > changeGrep(false);
      > }

      Error (CS3): changeTo expects a String or NothingEnum but SpecialCharacters.FORCED_LINE_BREAK seems to be a number. Does anyone know a workaround?

      Btw: insertTextAsContent(SpecialCharacters.FORCED_LINE_BREAK, XMLElementPosition.beforeElement) works fine - but it's not what I want.
        • 1. Re: text replace and SpecialCharacters
          [Jongware] Most Valuable Participant
          findWhat and changeTo expect the same syntax as in the UI, that is, regular text strings with special characters escaping and wildcarding the same way. A Forced Line Break in regular Find (findTextPreferences) is "^n" and in GREP mode "\n" (findGrepPreferences).

          Take notice of proper backslash escaping; because you are programming in JavaScript, the backslash (as GREP Escape character) needs to be escaped (as JavaScript Escape character). You missed that one in your '\r' sample.
          • 2. Re: text replace and SpecialCharacters
            Level 1
            > Take notice of proper backslash escaping

            That's it, thank you!

            JS-"\r" works accidentally because Indesign treats this JS-generated line break the same way than the escape sequence backslash-r (written "\\r" in JS).
            But "\n" has a different effect than "\\n" so your hint made my day.
            • 3. Re: text replace and SpecialCharacters
              Level 1
              Hi Siegfried,

              re:
              app.changeGrepPreferences.changeTo = SpecialCharacters.FORCED_LINE_BREAK;
              

              ...won't work, because you don't enter a forced line break in the Find What field of the Find/Change panel. Instead, you enter a metacharacter--so you also use a metacharacter when you perform the task from scripting. This is what jongware's saying (thanks jongware!), but I thought I'd expand on it a bit.

              In short: Don't use special characters enumerations in Find/Change. Sometimes, it'll work (when the characters are visible, non-control characters), sometimes it won't.

              Other than the need to enter escape characters (to produce valid JavaScript strings), The Find What and Change To properties of all of the find/change preferences objects exactly mimic the corresponding fields in the Find/Change panel.

              Thanks,

              Ole