Article View: adobe.scripting.indesign
Article #20150Re: fit to content ?
From: Dave Saunders
Date: Mon, 21 Oct 2002 19:16
Date: Mon, 21 Oct 2002 19:16
161 lines
7383 bytes
7383 bytes
Dan,
I had posted it once, but I see that it has been deleted from the archives, so here it is again. The script is "proxy-aware." That it, it will shrink the frame towards the point represented by the proxy point you have activated. If you choose a side point, it will operate only in one dimension. Choose the corners or center point and it operates in two dimensions.
There is a possibility of some text flowing out of the frame if some external circumstance disturbs things. If the script detects this, it will issue a warning.
The script doesn't care if the frame is connected to a story. Here it is (rather long):
-- Proxy Aware Fit Text Frame to Content
-- This version will operate on any text frame
-- However, the potential exists for it, having done its thing, for
-- the composition engine to come to a different conclusion about the
-- line breaks in the last paragraph, so I provide a rudimentary check
-- and warn you if I think things might have changed.
on run
tell application "InDesign 2.0.1"
try
set theClass to class of selection
on error
beep
return
end try
-- if we get past that initial check, then we need to preserve
-- the user's ruler settings and change to picas, restoring at
-- the end
tell document 1
copy horizontal measurement units of view preferences to UserHoriz
copy vertical measurement units of view preferences to UserVert
set horizontal measurement units of view preferences to picas
set vertical measurement units of view preferences to picas
end tell
-- I've discovered that I often want to use this while
-- the text tool is still active, so:
if theClass is in {text, insertion point} then
select parent text frame of selection
set theClass to class of selection
end if
if theClass is text frame then
set theProxy to transform reference point of window 1
set theBounds to geometric bounds of selection
set theOrigHeight to (item 3 of theBounds) - (item 1 of theBounds)
set theOrigWidth to (item 4 of theBounds) - (item 2 of theBounds)
-- 2/13/02 A new wrinkle. What if the frame alignment is not top?
-- The script should still work, so temporarily change to top.
-- Note this will optimize the space consumed by a frame that is set to
-- justify (assuming you have other than the left center or right center
-- proxy point selected
set userJust to vertical justification of text frame preferences of item 1 of selection
set vertical justification of text frame preferences of item 1 of selection to top
set charCount to count of characters of selection
set charDiff to my myFit()
set vertical justification of text frame preferences of item 1 of selection to userJust
set theChangedBounds to geometric bounds of selection
set theNewHeight to (item 3 of theChangedBounds) - (item 1 of theChangedBounds)
set theNewWidth to (item 4 of theChangedBounds) - (item 2 of theChangedBounds)
-- first, reshape
copy theBounds to theNewBounds
if theProxy is in {top left anchor, top right anchor, center anchor, bottom left anchor, bottom right anchor, anchor point} then
copy theChangedBounds to theNewBounds
else if theProxy is in {top center anchor, bottom center anchor} then
copy item 3 of theChangedBounds to item 3 of theNewBounds
set theNewWidth to theOrigWidth
else
copy item 4 of theChangedBounds to item 4 of theNewBounds
set theNewHeight to theOrigHeight
end if
-- now, move
set xDelta to 0
set yDelta to 0
if theProxy is in {bottom left anchor, bottom center anchor, bottom right anchor} then
set yDelta to theOrigHeight - theNewHeight
else if theProxy is in {left center anchor, center anchor, right center anchor} then
set yDelta to (theOrigHeight - theNewHeight) / 2
end if
if theProxy is in {top right anchor, right center anchor, bottom right anchor} then
set xDelta to theOrigWidth - theNewWidth
else if theProxy is in {top center anchor, center anchor, bottom center anchor} then
set xDelta to (theOrigWidth - theNewWidth) / 2
end if
set item 1 of theNewBounds to (item 1 of theNewBounds) + yDelta
set item 3 of theNewBounds to (item 3 of theNewBounds) + yDelta
set item 2 of theNewBounds to (item 2 of theNewBounds) + xDelta
set item 4 of theNewBounds to (item 4 of theNewBounds) + xDelta
set geometric bounds of selection to theNewBounds
set newCount to count of characters of selection
set charDiff to newCount - charCount
if charDiff is not 0 then
activate
display dialog "Warning: character count changed."
end if
else
beep
end if
-- restore measurement units
tell document 1
set horizontal measurement units of view preferences to UserHoriz
set vertical measurement units of view preferences to UserVert
end tell
return
end tell
end run
on myFit()
tell application "InDesign 2.0.1"
set theFrame to item 1 of selection
tell theFrame
set lineCount to count of lines
set charCount to count of characters
set textWidth to 0
repeat with x from 1 to lineCount
if (count of characters of line x) > 1 then
if baseline of insertion point -1 of line x = baseline of insertion point -2 of line x then
set theRight to (horizontal offset of insertion point -1 of line x)
else
set theRight to (horizontal offset of insertion point -2 of line x)
end if
set theWidth to theRight - (horizontal offset of insertion point 1 of line x)
if theWidth > textWidth then
set textWidth to theWidth
end if
end if
end repeat
set strokeWeight to (stroke weight) / 12
set frameInsets to inset spacing of text frame preferences as list
if (count of frameInsets) = 1 then
set frameInsets to {item 1 of frameInsets, item 1 of frameInsets, item 1 of frameInsets, item 1 of frameInsets}
end if
set bottomAdjust to strokeWeight / 2 + (item 3 of frameInsets)
set widthAdjust to strokeWeight + (item 2 of frameInsets) + (item 4 of frameInsets)
set theBounds to geometric bounds
set theNewBounds to {item 1 of theBounds, �
item 2 of theBounds, �
(baseline of character -1) + bottomAdjust, �
(item 2 of theBounds) + textWidth + widthAdjust}
set geometric bounds to theNewBounds
set newCount to count of characters
return newCount - charCount
end tell
end tell
end myFit
Dave
Technical Details
Message-ID:
<1de637ee.2@WebX.la2eafNXanI>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-04!supernews.com!news.maxwell.syr.edu!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebXReferences:
<1de637ee.-1@WebX.la2eafNXanI>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f939a,93fd7f36e2d2c580 X-Google-Attributes: gidf939a,public X-Google-ArrivalTime: 2002-10-21 12:41:46 PST From: "Dave Saunders" <daves@pdsassoc.com> Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-04!supernews.com!news.maxwell.syr.edu!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebX Newsgroups: adobe.scripting.indesign Subject: Re: fit to content ? Message-ID: <1de637ee.2@WebX.la2eafNXanI> Date: Mon, 21 Oct 2002 12:16:49 -0700 References: <1de637ee.-1@WebX.la2eafNXanI> Lines: 184 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit