Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


change text frame options based on content paragraph style

Community Beginner ,
May 06, 2016 May 06, 2016

Copy link to clipboard

Copied

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;

}

TOPICS
Scripting

Views

632

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , May 06, 2016 May 06, 2016
sthg along this ... not testedvar styleNames=["caption"];  //array to fillvar 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 onl...

Likes

Translate

Translate
Enthusiast ,
May 06, 2016 May 06, 2016

Copy link to clipboard

Copied

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;

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 06, 2016 May 06, 2016

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 10, 2016 May 10, 2016

Copy link to clipboard

Copied

  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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

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 )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

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

 

Thanks Once Again.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

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 )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

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;

 

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

Paste the code from my post, not the original.

 

Needs to be saved as plain text, or from the ExtendScript Toolkit app with a .jsx ext. into your ID scripts folder: ⁨

Applications⁩ ▸ ⁨Adobe InDesign 20XX⁩ ▸ ⁨Scripts⁩ ▸ ⁨Scripts Panel⁩

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

Dats exactly what I did @rob day , but it also displays an error code exactly as the one that appears in my comment below, please compile the code, test and send it to me via this mail jamilu19 @ gmail.com

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

Does your document have a Paragraph Stylesheet named "caption"? The first line of the script sets an array of Paragraph Style names to search for. The names need to be exact and are case sensitive

 

//edit the list as needed. Style names need to be in quotes separated by commas
var styleNames=["caption", "style2", "style3"];  //array to fill

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

No, my document has no paragraph style called captions, the paragraph style I want to search for, is named as CASES TITLE, and dats the similar text frame I want to change its text frame options across the whole document.

 

Therefore, if I get you right, you said I should edit the code, then replace caption with CASES TITLE?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

LATEST

Yes.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

This is the error message I got

Capture.PNG

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

The image above is a reply to @Laubender 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

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 )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

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

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

Please, @Laubender and @rob day . Help me compile the code, test it to confirm that it's working fine, then kindly send it to my email address via jamilu19 @ gmail.com.

 

I have zero knowledge of scripting.

Thanks

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines