Copy link to clipboard
Copied
I'm using javascript on Indesign CS4. I have a function thats placing different amounts of text into a template that I have previously. Since all the text is differently sized, sometimes it doesn't fit the template. I tried using fit.(fitOptions.proportionally) when my text frame was selected, but nothing happened. There wasn't a bug either, nothing happened at all. What should I do? Thanks!
1 Correct answer
I wrote this script way back when.. Maybe it'll be of use...
.../*************************************************************************************
FitTextToFrame.jsx
InDesign CS2 Script: version 1.0 Feb 4, 07 by Harbs
isText method and errorExit function by Dave Saunders
One line frames will expand down to fit the whole line in the width of the frame
Multi-line frames will fit the widest line
Text of varying sizes is scaled proportionally
************************************************
Copy link to clipboard
Copied
Try .fit (FitOptions.frameToContent).
Peter
Copy link to clipboard
Copied
As far as fitting options are concerned, you can only do in a script what is available in the UI. And, as Peter has pointed out,that is just the fit frame to contents (and even then it only works in certain circumstances).
If you want to increase the size of the text in your frame so it fills the frame, you'll need to do this the hard way. A sneaky way to do it would be to adjust the horizontal and vertical scale of all the text, but even there, you'd need to work a text style range at a time.
And getting the amount right would involve iterating.
Dave
Copy link to clipboard
Copied
You can use my free javascript FitStoryToFrame at Indesign Exchange, http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1657519
works with cs3 and cs4 (maybe more but that all that was tested) it iterates text ranges and therefor does not with nested styles, grep styles and line styles. But otherwise it's an amazing tool and has many choices for how to expand or shrink story
Copy link to clipboard
Copied
I wrote this script way back when.. Maybe it'll be of use...
/*************************************************************************************
FitTextToFrame.jsx
InDesign CS2 Script: version 1.0 Feb 4, 07 by Harbs
isText method and errorExit function by Dave Saunders
One line frames will expand down to fit the whole line in the width of the frame
Multi-line frames will fit the widest line
Text of varying sizes is scaled proportionally
**************************************************************************************/
Object.prototype.isText = function() {
switch(this.constructor.name){
case "InsertionPoint":
case "Character":
case "Word":
case "TextStyleRange":
case "Line":
case "Paragraph":
case "TextColumn":
case "Text":
return true;
default :
return false;
}
}
if (!app.selection[0]){errorExit("No Text Selected")}
if (app.selection[0].isText()){
var myTF = app.selection[0].parentTextFrames[0];
}else if (app.selection[0].constructor.name == "TextFrame"){
var myTF = app.selection[0];
}else{errorExit("No Text Selected")}
var mySI = myTF.characters[0].index;
var myIndx = myTF.characters[-1].index;
var myLineLength = myTF.lines.length;
var myText = myTF.parentStory.characters.itemByRange(mySI,myIndx);
var myJust = myText.justification;
myText.justification = Justification.leftAlign;
aoIPDif = new Array;
for(i = 0; i < myTF.lines.length; i++){
var oIPs = myText.lines.insertionPoints.everyItem().horizontalOffset;
oIPs.sort(sortNumber);
aoIP1 = oIPs[0];
aoIP2 = oIPs[oIPs.length-1];
aoIPDif.push(aoIP2-aoIP1)
}
myText.justification = Justification.fullyJustified;
aeIPs = myText.lines[0].insertionPoints.everyItem().horizontalOffset;
aeIPs.sort(sortNumber);
aeIP1 = aeIPs[0];
aeIP2 = aeIPs[aeIPs.length-1];
aeIPDif = aeIP2-aeIP1;
var aoDif = aoIPDif[0]
myLine = myTF.lines[0];
for (var i = 1; i < aoIPDif.length; i++){
if (aoIPDif>aoDif){var aoDif=aoIPDif}
myLine = myTF.lines;
}
myLineLength = myLine.characters.length;
var myFactor = aeIPDif / aoDif;
for(var i=0;i<myText.characters.length;i++){
var myChar = myText.characters;
myChar.pointSize = myChar.pointSize * myFactor;
}
myText.justification = myJust[0];
var myDoc = app.activeDocument;
var myPage = myTF.parent;
var userVert = app.documents[0].viewPreferences.verticalMeasurementUnits;
app.documents[0].viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
var myBottomMarginLocation = myDoc.documentPreferences.pageHeight - myPage.marginPreferences.bottom;
var myBounds = myTF.geometricBounds;
myBounds[2] = myBottomMarginLocation;
myTF.geometricBounds = myBounds;
try {var myForceErr = myLine.characters.length}
catch(e){
app.documents[0].viewPreferences.verticalMeasurementUnits = userVert;
errorExit("Some text got overset")
}
while (myLine.characters.length<myLineLength){
for(var i=0;i<myText.characters.length;i++){
var myChar = myText.characters;
myChar.pointSize = myChar.pointSize /1.01;
}
}
myBounds[2] = myTF.characters[-1].baseline + myTF.characters[-1].descent + myTF.textFramePreferences.insetSpacing[2] + myTF.strokeWeight;
myTF.geometricBounds = myBounds;
app.documents[0].viewPreferences.verticalMeasurementUnits = userVert;
if (myTF.characters[-1].index < myIndx) {errorExit("Some Text Got Overset")}
function errorExit(message) {
if (arguments.length > 0) {
if (app.version != 3) { beep() } // CS2 includes beep() function.
alert(message);
}
exit(); // CS exits with a beep; CS2 exits silently.
}//end function errorExit
function sortNumber(a, b){
return a - b
}
Copy link to clipboard
Copied
That's almost perfect. Any idea of how to change it for when my text doesn't fit in the frame at all, or is larger than the text frame?
Copy link to clipboard
Copied
I'll check if I have a version which first shrinks the text. I don't remember anymore...
Copy link to clipboard
Copied
Nope. I can't find one that does that.
All you really need to do is loop through all the textStyleRanges in the story and shrink them all by a large set amount before you expand the text...
Harbs
Copy link to clipboard
Copied
Thanks a lot! I think I can manage that!
Copy link to clipboard
Copied
I wrote this script quite some time ago. I'd probably do many things differently if I was writing it today.
One thing which will make it much more efficient would be to change the loops from looping through characters to looping through textStyleRanges...
Harbs
Copy link to clipboard
Copied
I just wrote a script that does the following:
If a text frame is overset, it will reduce the contents of the frame until all the text fits.
If a text frame is half empty, it will enlarge the contents of the frame until everything fits.
Is that what you would need?
Copy link to clipboard
Copied
yes maybe. id love to try it!
Copy link to clipboard
Copied
(I sent you a private message.)
Copy link to clipboard
Copied
I modified another script I found to work for what I needed. This loops through text frames and can either enlarge the text to fit the frame of an overflow is detected, shrink the text (font, leading, scaling, etc.) until it fits in the frame.
fitContentToFrame();
function fitContentToFrame(){
var allTextFrames = app.activeDocument.textFrames;
for(var i=0;i<allTextFrames.length;i++){
var aTextFrame= allTextFrames;
var myStory =aTextFrame.parentStory;
var myParagraphStyle=myStory.appliedParagraphStyle;
var myOver=aTextFrame.overflows;
var myPointSize= myStory.pointSize;
var myTrack= myStory.tracking;
var myHScale= myStory.horizontalScale;
var myLead=myStory.leading;
var myMaxLead = myPointSize * 1.5;
var myMinLead = myPointSize * 0.8;
var myMaxHScale = myHScale * 1.3;
var myMinHScale = myHScale * 0.7;
var myMaxTrack = myTrack + 50;
var myMinTrack = myTrack - 50;
var myTrackIncrement =1;
var myLeadIncrement = 1;
var myHScaleIncrement = 0.25;
var myPointSizeIncrement = 1;
var myFontSize = myStory.pointSize;
if (myParagraphStyle.leading==Leading.auto) {
var myLead=myPointSize * 1.2;
}
//use this code if you need the text in a frame to enlarge to fit the frame
//while (myOver==false && myLead <= myMaxLead && myHScale<= myMaxHScale && myTrack <= myMaxTrack)
// {
// var myLead = myLead + myLeadIncrement;
// var myHScale = myHScale + myHScaleIncrement;
// var myTrack = myTrack + myTrackIncrement;
// myStory.leading = myLead;
// myStory.horizontalScale = myHScale;
// myStory.tracking=myTrack;
// var myOver=aTextFrame.overflows;
// }
//this code will continue to reduce the size and leading, etc. until the overflow text fits in the frame
while (myOver==true ) //&& myLead >= myMinLead && myHScale >= myMinHScale && myTrack >= myMinTrack)
{
var myLead = myLead - myLeadIncrement;
var myHScale = myHScale - myHScaleIncrement;
var myTrack = myTrack - myTrackIncrement;
var myFontSize =myFontSize - myPointSizeIncrement;
myStory.leading = myLead;
myStory.horizontalScale = myHScale;
myStory.tracking=myTrack;
myStory.pointSize=myFontSize;
var myOver=aTextFrame.overflows;
}
}
alert("Text frames with overflow have been fitted.");
}

