Skip to main content
prtamil
Known Participant
May 6, 2016
Answered

change text frame options based on content paragraph style

  • May 6, 2016
  • 5 replies
  • 1691 views

Hi Experts,

I write a script to change the text frame options when that frame contains particular style(s), but I am getting error as object is invalid when I pass the 12th line. Can some one please help me to find out the error?

And can some on please teach me where should I exactly find what error it is on ESTK?

var mystyle=["caption"];

for(k=0;k<mystyle.length;k++)

{

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

app.findGrepPreferences.appliedParagraphStyle = mystyle;

var fnd = app.findGrep();

    for (a=0; a<fnd.length; a++)

        {

            var myTextFrame = app.selection[0].textFrames[0];

            alert (myTextFrame);

            myTextFrame.textFramePreferences.firstBaselineOffset=FirstBaseline.ASCENT_OFFSET;

            myTextFrame.textFramePreferences.minimumFirstBaselineOffset=6;

            myTextFrame.fit(FitOptions.FRAME_TO_CONTENT);

        }

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

}

This topic has been closed for replies.
Correct answer -hans-

sthg along this ... not tested

var styleNames=["caption"];  //array to fill

var doc = app.activeDocument;

for(var p = 0; p < styleNames.length; p++)

{

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

app.findGrepPreferences.appliedParagraphStyle = doc.paragraphStyles.itemByName(styleNames

);

var find = app.findGrep();

if(find.length > 0)setTFProps(find);

    }

function setTFProps(find)

{

    for(var i = 0; i < find.length; i++)

{

    var parentTextframe = find.parentTextFrames[0];//assuming only one textFrame per caption

with( parentTextframe)

{

    textFramePreferences.firstBaselineOffset=FirstBaseline.ASCENT_OFFSET;

        }

  pT.fit(FitOptions.FRAME_TO_CONTENT);

    }

    }

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

5 replies

Community Expert
April 30, 2021

Hi Jameel,

ah, yes, this is the error for the older posted code.

I assume that code was damaged by the end of 2019 when this thread was moved from the old InDesign Scripting forum to the new InDesign forum. A typical error. The iterator was dropped by the moving process.

 

Before:

doc.paragraphStyles.itemByName(styleNames);

Rob corrected that to:

doc.paragraphStyles.itemByName(styleNames[p]);

 

styleNames as argument made no sense, because the variable styleNames contains an array of style names.

Even if the array contains only one name. The loop is walking through this array, and a single entry in the array must be accessed through the iterator [p].

 

Regards,
Uwe Laubender

( ACP )

rob day
Community Expert
Community Expert
April 30, 2021

Usually the old post problems are with the variable in array loops, but this is also wrong

pT.fit(FitOptions.FRAME_TO_CONTENT);

should be

parentTextframe.fit(FitOptions.FRAME_TO_CONTENT);

 

Community Expert
April 30, 2021

What does the error message say?

Please provide a sample document where you see this error if you run the script.

 

Thanks,
Uwe Laubender

( ACP )

rob day
Community Expert
Community Expert
April 30, 2021

Hi Uwe, I think this is a case of an old thread dropping the loop variables on the forum upgrade. This should work:

 

 

 

 

var styleNames=["caption"];  //array to fill
var doc = app.activeDocument;

for(var p = 0; p < styleNames.length; p++){

    app.findGrepPreferences = null;
    app.changeGrepPreferences = null;
    app.findGrepPreferences.appliedParagraphStyle = doc.paragraphStyles.itemByName(styleNames[p]);
    var find = app.findGrep();

    if(find.length > 0)setTFProps(find);
}


function setTFProps(find){

    for(var i = 0; i < find.length; i++){
        var parentTextframe = find[i].parentTextFrames[0];//assuming only one textFrame per caption

        with(parentTextframe){
            textFramePreferences.firstBaselineOffset=FirstBaseline.ASCENT_OFFSET;
        }
        parentTextframe.fit(FitOptions.FRAME_TO_CONTENT);
    }
}

app.findGrepPreferences = null;
app.changeGrepPreferences = null;

 

 

 

Jameel Bin Abdallah
Known Participant
April 30, 2021

Please explain how should I go about using the code, I may be the one using it wrongly. 

I copied and saved it into a script file, then select a text frame from my InDesign Document and double click the script. Is that the right way?

Community Expert
April 30, 2021

Hi Jameel Bin Abdallah,

does the code Hans provided will not work for you?

Is there an error message if you execute the script?

 

Regards,
Uwe Laubender

( ACP )

Jameel Bin Abdallah
Known Participant
April 30, 2021

Yes @Laubender, it's display an error which I don't understand, please check the code and help out.

 

Thanks Once Again.

Inspiring
May 10, 2016
  1. app.findGrepPreferences.appliedParagraphStyle = mystyle

this refers to a String rather than a paragraphstyle

  1.             var myTextFrame = app.selection[0].textFrames[0]; 

does not take care of the searchresults, should be

fnd.parentTextFrames[0];//assuming only one textFrame per caption

-hans-Correct answer
Inspiring
May 6, 2016

sthg along this ... not tested

var styleNames=["caption"];  //array to fill

var doc = app.activeDocument;

for(var p = 0; p < styleNames.length; p++)

{

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

app.findGrepPreferences.appliedParagraphStyle = doc.paragraphStyles.itemByName(styleNames

);

var find = app.findGrep();

if(find.length > 0)setTFProps(find);

    }

function setTFProps(find)

{

    for(var i = 0; i < find.length; i++)

{

    var parentTextframe = find.parentTextFrames[0];//assuming only one textFrame per caption

with( parentTextframe)

{

    textFramePreferences.firstBaselineOffset=FirstBaseline.ASCENT_OFFSET;

        }

  pT.fit(FitOptions.FRAME_TO_CONTENT);

    }

    }

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

prtamil
prtamilAuthor
Known Participant
May 6, 2016

Hi -hans-

it works perfectly. Can you please let me know what went wrong on coding? This will help me to learn further script writing.

Thanks

pr

Jameel Bin Abdallah
Known Participant
April 30, 2021

Hy @prtamil, am currently in same problem as you before, please help me send the edited script of the above code that works for you through my email on jamilu19 @ gmail.com.

 

Note: there's no space in between the email address.

 

Thanks