6 Replies Latest reply on Sep 5, 2008 5:00 AM by (Andy_Cummins)

    Text frame wrapping text question

      Hi,
      I'm trying to use applescript to create text frames in both CS2 and CS3. My problem is this, I am trying to create text frames without knowing the length of the content I am putting in there. I have a height that the frame has to be but no width and so I want the width 'grow' with the content.

      I have tried a couple of ways of doing this.

      1 - First specify a massive text frame and then use the fit to content functionality. However this just fitted the content by height and kept the width I had initially specified. So I had a massively wide text frame snugly fitted to the height of the text.

      2 - Next I specified a small width, one too narrow for my content, to see if it would stretch. It didn't and so the text wrapped over multiple lines instead.

      My final thought on how to do this is to specify a small width, fit the frame to content and then in a loop increase the width and fit to content repeatedly until the bottom bound of the box stops decreasing. This would mean the frame is wide enough.

      Am I missing something? Is there an easier way to do this
        • 1. Re: Text frame wrapping text question
          Loic.Aigon Adobe Community Professional
          What about after creating your textframe (pt 2), create a while condition like (sheme, I don't know much of AS):
          while mytextframe overflows
          add 1mm to the width of mytextframe
          Loic
          • 2. Re: Text frame wrapping text question
            Level 1
            Thanks for your reply, I gave it a go but it's lightly off on what I need. Over flow determines whether or not the entire content flows over the entire box size. What I need is whether or not a line goes out over the width of a text frame.

            For example if I had a text frame 3 units wide and 20 units long a single long word would wrap onto a second line but it wouldn't overflow out of the textframe. I need to know whether it wrapped onto the second line...
            • 3. Re: Text frame wrapping text question
              Loic.Aigon Adobe Community Professional
              Do I understand that you want to know if words are hyphenated and where ? Because if you mean that, well you can also deactivate hyphenation so the words don't wrap to another line.

              If in the contrary, you mean that you want to keep a specific string of words in a line without the words are cut, well, you could add a "hard" return (is this the term in uk ? Shift Enter ?).
              After that I imagine a kind of search like
              set mysearch to character xx of line 1 to last character of line 1
              set mymatch to toto & ^n

              while mysearch doesn't match to mymatch
              expand myframe

              Sorry for this very conceptual story but I don't know enough of AS to make it functionnal.

              Good luck
              Loic
              • 4. Re: Text frame wrapping text question
                Level 1
                If you want to make a text frame wider until no paragraphs in the text consist of more than one line, then test for that between making the frame wider. Something like this:
                myFrame = app.selection[0];
                
                gb = myFrame.geometricBounds;
                while (frameHasWrap(myFrame)) {
                  gb[3] += 1;
                  myFrame.geometricBounds = gb;
                }

                function frameHasWrap(frame) {
                  var myParas = frame.paragraphs;
                  for (var j = myParas.length - 1; j >= 0; j--) {
                    if (myParas[j].lines.length > 1)
                      { return true }
                  }
                  return false;
                }
                Only you'll have to translate that into AS.

                Dave
                • 5. Re: Text frame wrapping text question
                  Level 1
                  I've sorted it out now thanks for the suggestions though. I am taking my content from a database and so it cannot be altered. I think Dave's suggestion might be what I needed but here is what I did this morning and it works for me...I've only included the relevent part of the code:

                  on shrinkToFit(TB, iExtra)
                  tell application "Adobe InDesign CS2"
                  tell TB
                  set properties of text frame preferences of TB to {inset spacing:{0.7, 0.75, 0.7, 0.75}}
                  fit TB given frame to content
                  end tell
                  end tell
                  end shrinkToFit

                  tell document 1
                  tell last page
                  set LabelBox to make new text frame with properties {geometric bounds:{1, 1.5, 3.0, 15.0}, stroke color:"Black", stroke weight:0.5}
                  place "fileCreatedFromDB.txt" on LabelBox

                  my shrinkToFit(LabelBox, kBottomPadding)

                  set currentBottom to item 3 of geometric bounds of LabelBox as number
                  repeat while LabelBox is not overflows
                  set currentBoxBounds to geometric bounds of LabelBox
                  set currentBoxBottom to item 3 of currentBoxBounds as number
                  set currentBoxTop to item 1 of currentBoxBounds as number
                  set currentBoxLeft to item 2 of currentBoxBounds as number
                  set currentBoxRight to item 4 of currentBoxBounds as number

                  set geometric bounds of LabelBox to {currentBoxTop, currentBoxLeft, currentBoxBottom, currentBoxRight - 0.1}
                  end repeat

                  -- add last decrement back on so label no longer overflows
                  set geometric bounds of LabelBox to {currentBoxTop, currentBoxLeft, 3.0, currentBoxRight + 0.1}

                  my moveBoxTo(LabelBox, 8.808112)

                  set myBounds to geometric bounds of LabelBox
                  set LabelBoxBottom to item 3 of myBounds as number
                  end tell
                  end tell
                  • 6. Re: Text frame wrapping text question
                    Level 1
                    Sorry, so to explain, I have a set height I need the frame to be but no set width. I make an arbitrarily long frame place my text then narrow the frame until the frame overflows...this way the frame is as long as the longest line of text in my content.

                    For some reason I had to use the fit frame to content function within shrinkToFit() in order for the overflow test to work. I then re-specify my initial height at the end so all text frames are the same compulsory height.