Skip navigation
ax1e2l_b3r4a5s6s
Currently Being Moderated

Fit Text Frame to Content - Applescript

May 1, 2012 11:48 AM

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

 

Possible??

 
Replies
  • Currently Being Moderated
    May 1, 2012 12:32 PM   in reply to ax1e2l_b3r4a5s6s

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 1, 2012 1:35 PM   in reply to ax1e2l_b3r4a5s6s

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 1, 2012 1:39 PM   in reply to ax1e2l_b3r4a5s6s

    Try putting the name of the text frame in quotes.

     
    |
    Mark as:
  • Currently Being Moderated
    May 1, 2012 4:18 PM   in reply to ax1e2l_b3r4a5s6s

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 1, 2012 11:29 PM   in reply to ax1e2l_b3r4a5s6s

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 2, 2012 7:30 PM   in reply to -hans-

    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")

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points