Remove character at the end of a line of wrapped text

Learn / Forums / General InDesign Topics / Remove character at the end of a line of wrapped text

Viewing 5 reply threads
  • Author
    Posts
    • #85185

      Hi. Two questions please.

      Let’s say that I have a cell in a table that has the following text: 1,3,7,13,20,25,29,32,35,39,55,72,133,136,139,525

      Next, assume that the width of the cell causes the text to wrap onto three lines.

      Question #1: can you specify the line-break character? I’m sure you can, I just couldn’t find it it. In this example, I want the line to break on the comma. I tried messing with this, the numbers themselves were being broken in the middle at times.

      Question #2: with the line breaking on the comma, can I remove the comma (or at least hide it) at the end of a line that is wrapped?

      So, if the wrapped text in a cell would look like:

      1,3,7,13,20,25,29,
      32,35,39,55,72,133,
      136,139,525

      I would like the resulting text to look like:

      1,3,7,13,20,25,29
      32,35,39,55,72,133
      136,139,525

      Thanks in advance for the help!

    • #85189
      Peter Kahrel
      Participant

      Question #1: Add a discretionary line break after every comma. That’ll keep the digits together.

      Question #2: Not automatically. A Grep style won’t work because you can’t find the end of turn lines (except when you add forced line breaks, which you probably don’t want). But a script can do that.

      Create a character style Hide (set just the colour to Paper), then click somewhere in the table and run the script.

      (function () {
        var i, lines, cells;
        cells = app.selection[0].parent.parent.cells.everyItem().getElements();
        for (i = 0; i < cells.length; i++) {
          cells[i].texts[0].appliedCharacterStyle = app.documents[0].characterStyles[0];
          cells[i].texts[0].clearOverrides (OverrideType.CHARACTER_ONLY);
          lines = cells[i].lines;
          for (j = 0; j < lines.length; j++) {
            if (lines[j].characters[-1].contents == SpecialCharacters.DISCRETIONARY_LINE_BREAK) {
              lines[j].characters[-2].appliedCharacterStyle = 'Hide';
            }
          }
        }
      }());

      Peter

    • #85207

      Thank you Peter for the quick reply.

      Regarding question #1, can discretionary line breaks be imported in XML?

      Regarding question #2, I thought this might be the case. In looking at your script, if I’m reading this correctly, you are hiding every comma, not just the comma at the end of a link where a wrap occurred. Am I correct?

    • #85210
      Peter Kahrel
      Participant

      > can discretionary line breaks be imported in XML?

      Probably. Their hex code is 200B, you can use that. Not sure how, though.

      > you are hiding every comma, not just the comma at the end of a link where a wrap occurred. Am I correct?

      Not really. The script first unhides all commas, then hides the commas at the end of each line. That’s really only needed if you change the width of a column table, because the styles aren’t updated automatically when you resize a column. If you never resize, then lines 5 and 6 can be deleted (the two lines starting with cells[i].texts[0]).

      P.

      • #85212
        Matt Isaac
        Participant

        Peter,

        I am trying to learn/understand coding myself. From what I see, wouldn’t that script apply the ‘Hide’ character style to the last two characters in a line that ends with a discretionary line break; even if the line ended with a discretionary line break following a letter?
        ex. This is my sentence(Discretionary Line Break) with a dlb.
        If the text needed to be broke at that dlb wouldn’t the text read:
        “This is my sentenc
        with a dlb.”??
        Just trying to understand for myself.

      • #85219
        Ari Singer
        Member

        You’re absolutely right, but Peter didn’t care about that because in this situation he knew there will only be discretionary line breaks after a comma. So he didn’t bother. But in the real world if there was such a case then that line will be written as such:

        if ((lines[j].characters[-1].contents == SpecialCharacters.DISCRETIONARY_LINE_BREAK) && (lines[j].characters[-2].contents = ",")) {
                lines[j].characters[-2].appliedCharacterStyle = 'Hide';
        

        Which basically says: If the last character is a discretionary line break and there is a comma before that, then…

      • #85222
        Ari Singer
        Member

        Sorry, just noticed a flaw in my code. I wrote a single ‘=’ instead of ‘==’. So here’s the fixed code:

        if ((lines[j].characters[-1].contents == SpecialCharacters.DISCRETIONARY_LINE_BREAK) && (lines[j].characters[-2].contents == ",")) {
        lines[j].characters[-2].appliedCharacterStyle = 'Hide';

        Unfortunately, for whatever reason, I can’t always edit my posts :(

    • #85221
      Peter Kahrel
      Participant

      > wouldn’t that script apply the ‘Hide’ character style to the last two characters

      No: lines[j].characters[-2] means ‘the one-before-last character in lines[j]’

      > in a line that ends with a discretionary line break; even if the line ended with a discretionary line break following a letter?

      That’s true. But in Mike’s example that wasn’t the case.

      P.

    • #85270

      Thank you everyone’s kind input on this.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.
>