9 Replies Latest reply on May 2, 2012 7:30 PM by sstanleyau

    Fit Text Frame to Content - Applescript

    ax1e2l_b3r4a5s6s Level 1

      I'm trying to size a text frame to fit the width of it's contents.

       

      Possible??

        • 1. Re: Fit Text Frame to Content - Applescript
          Muppet Mark Level 5

          tell application "Adobe InDesign CS5"

               tell the active document

                    tell the first text frame

                         fit given frame to content

                    end tell

               end tell

          end tell

          1 person found this helpful
          • 2. Re: Fit Text Frame to Content - Applescript
            ax1e2l_b3r4a5s6s Level 1

            Thank you.

             

            I know the name of the text frame, so I try:

             

            tell application "Adobe InDesign CS5.5"

                      tell the active document

                                tell the text frame PersonalizeOneT

              fit given frame to content

                                end tell

                      end tell

            end tell

             

             

            This doesn't work.

             

            Undeclared variable.

             

            Sorry for the stupid questions.  Applescript syntax confuses the heck out this old php programmer !


            • 3. Re: Fit Text Frame to Content - Applescript
              Muppet Mark Level 5

              The green color indicates a script variable… as you have not previously set/declaired it… it's not vaild… If it's some sting property then wrap it in double quotes for AppleScript… eg

               

              tell application "Adobe InDesign CS5"

                           tell the active document

                                          set Text_Frame to the first text frame whose label is "test"

                                          tell Text_Frame to fit given frame to content

                           end tell

              end tell

              1 person found this helpful
              • 4. Re: Fit Text Frame to Content - Applescript
                Larry G. Schneider Adobe Community Professional & MVP

                Try putting the name of the text frame in quotes.

                1 person found this helpful
                • 5. Re: Fit Text Frame to Content - Applescript
                  ax1e2l_b3r4a5s6s Level 1

                  Mark -

                   

                  We are getting closer:

                   

                  tell application "Adobe InDesign CS5.5"

                    tell the active document

                    set Text_Frame to the text frame "PH"

                    tell Text_Frame to fit given frame to content

                    end tell

                  end tell

                   

                  works, but it only fits the frame to the height, not the width.

                   

                  Frame contains three lines of text, all centered.

                   

                  Any more thoughts???

                  • 6. Re: Fit Text Frame to Content - Applescript
                    sstanleyau Level 4

                    but it only fits the frame to the height, not the width.

                     

                    Which is exactly what the command does in the UI -- that's how it works. If you want it reduced in width, you will have to get the length of each line, find the longest, and set the width to that.

                    1 person found this helpful
                    • 7. Re: Fit Text Frame to Content - Applescript
                      -hans- Level 4

                      Shane's right ;-)

                       

                       

                      Getting it from the left:

                       

                      tell application "Adobe InDesign CS5.5"

                         

                          tell the active document

                             

                              set Text_Frame to item 1 of (every text frame whose label is "PH")

                             

                              tell Text_Frame

                                  set theLines to every line

                                 

                                  --get maxLineLength

                                  set maxLength to null

                                  repeat with i from 1 to count of theLines

                                      set {start, stopp} to {horizontal offset of line i, end horizontal offset of line i}

                                      set currentLineLength to stopp - start

                                      if i is 1 then

                                          set maxLength to currentLineLength

                                      else

                                          if currentLineLength is greater than maxLength then

                                              set maxLength to currentLineLength

                                          end if

                                      end if

                                  end repeat

                                 

                                  --set x2-bounds of Text_Frame

                                  set {y1, x1, y2, x2} to visible bounds

                                  set visible bounds to {y1, x1, y2, x1 + maxLength}

                                 

                                  --fit height

                                  fit given frame to content

                                 

                              end tell

                             

                          end tell

                         

                      end tell

                      Hope it'll work

                       

                      Hans-Gerd Claßen

                      • 8. Re: Fit Text Frame to Content - Applescript
                        ax1e2l_b3r4a5s6s Level 1

                        Great!  Thank you!

                        • 9. Re: Fit Text Frame to Content - Applescript
                          sstanleyau Level 4

                          Hope it'll work

                          It will nearly all the time, but it's worth a check afterwards to make sure. It can fail if, for example, the last text on the longest line is a long auto page number; InDesign gives the wrong width for those.

                           

                          Also, it's a fraction quicker to say:

                           

                                  set Text_Frame to (text frame 1 whose label is "PH")

                           

                          rather than:

                           

                                  set Text_Frame to item 1 of (every text frame whose label is "PH")