4 Replies Latest reply on Dec 11, 2008 6:15 AM by (nilom)

    Apply a noBreak with javascript

      I am trying to apply a no break to a currency sign followed by numbers.
      I tryed this javascript. But nothing seems to change in the textframe that I selected before running this script:

      var txt = app.selection[0].parentStory.contents;
      var myfound = txt.match( /( \d\d?\d?)(\.\d\d\d)?(\.\d\d\d)?(\.\d\d\d)?(\,)(\d\d)/g );
      if( myfound != null )
      for( var i = 0; i < myfound.length; i++ )
      myfound[i].noBreak = true;

      Is there something missing?

      Please excuse my poor english.

      P.S. if i select only the sign and the digits, instead of the textframe, and run

      app.selection[0].noBreak = true;

      it will work great.
        • 1. Re: Apply a noBreak with javascript
          Peter Kahrel Adobe Community Professional & MVP

          There's nothing wrong with your regular expression, it works fine, though you make things unnecessarily complicated. You might as well use this one:
          >var myfound = txt.match( /( [\d.,]+)/g );

          which does the same. But the reason why it doesn't work is that with your regular expression you collect an array (myfound), and then apply something to that array -- apply noBreak. This has no effect on the array at all (because noBreak is relevant to InDesign objects, not to arrays of strings), and your InDesign document is unaffected.

          Before we go on it is useful to know which version of InDesign you use (CS 2, 3, or 4).

          • 2. Re: Apply a noBreak with javascript
            Level 1
            oh Peter,
            i got running just a few days ago with javascript, thanks to you three books i bought from oreally:
            AutomatingInDesignwithRegularExpressions; GREPinInDesignCS3CS4;ScriptingInDesignwithJavaScript.
            Fantastic and clear (considering that I am a newbie).
            I didn't expect an answer from you and I'm glad.

            Excuse my ot and let's answer.

            I'm using InDesign CS 3

            Thank you.

            • 3. Re: Apply a noBreak with javascript
              Peter Kahrel Adobe Community Professional & MVP
              Thanks for your kind words. As to your script, you were caught up in big changes between CS2 and CS3/4. Your approach (approaching InDesign stories as text) was going the CS2 way, which was the only way to go because CS2 did not know GREP natively. But CS3 and CS4 do know GREP natively, which make many things a lot easier. What you want to do ca\n be scripted in CS3 as follows:
              app.findGrepPreferences = app.changeGrepPreferences = null;
              app.findGrepPreferences.findWhat = " [\\d.,]+";
              app.changeGrepPreferences.noBreak = true;

              The O'Reilly titles aren't clear on this -- yet. They soon will be.

              1 person found this helpful
              • 4. Re: Apply a noBreak with javascript
                Level 1
                Thank you so much.

                Works perfect (don't need to tell you)