• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Simple Scripting Help: Find Text Box, Apply Object Style

Explorer ,
Nov 03, 2022 Nov 03, 2022

Copy link to clipboard

Copied

Hi All, I'm new to scripting and I wrote a simple script to find the 7th text box on a page and apply the Object Style, "Text 2A". However, I can't seem to get it to work. Any help you can give me is greatly appreciated!

 
var curDoc = app.activeDocument;
curDoc.pages[0].textFrames[6];
curDoc.select (curDoc.pages[0].textFrames[6]);
app.selection[0].applyObjectStyle(curDoc.objectStyles.item("Text 2A"),true);
TOPICS
Scripting

Views

219

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 2 Correct answers

People's Champ , Nov 03, 2022 Nov 03, 2022

I can reproduce the error with a non valid object style reference like this: 

app.selection[0].applyObjectStyle(curDoc.objectStyles.item("NON VALID NAME"),true);

So you may want to both:

- use itemByName property instead of item("the name"), item() method generally expect an index (i.e. item(0) )

- check for style existence before you wanna use it (use boolean isValid):

try{
    var curDoc = app.activeDocument;
    curDoc.pages[0].textFrames[6];
    curDoc.select (curDoc.pages[0].textFrames[6]);
    
...

Votes

Translate

Translate
Explorer , Nov 04, 2022 Nov 04, 2022

This original code is correct for finding a text box and applying an Object Style:

var curDoc = app.activeDocument;
curDoc.pages[0].textFrames[6];
curDoc.select (curDoc.pages[0].textFrames[6]);
app.selection[0].applyObjectStyle(curDoc.objectStyles.item("Object Style Name Here"),true);

Votes

Translate

Translate
People's Champ ,
Nov 03, 2022 Nov 03, 2022

Copy link to clipboard

Copied

With a valid context, your script works just fine. What is the error that you get?

Use try/catch if needed, that will give the line and error message.

 

try{
    var curDoc = app.activeDocument;
    curDoc.pages[0].textFrames[6];
    curDoc.select (curDoc.pages[0].textFrames[6]);
    app.selection[0].applyObjectStyle(curDoc.objectStyles.item("Text 2A"),true);
}
catch(err){
    alert(err.line+":"+err.message)
}

 

HTH

Loic

Votes

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
Explorer ,
Nov 03, 2022 Nov 03, 2022

Copy link to clipboard

Copied

Thanks for responding! Here's the error message that I'm getting:Screen Shot 2022-11-03 at 4.05.04 PM.png

Votes

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
People's Champ ,
Nov 03, 2022 Nov 03, 2022

Copy link to clipboard

Copied

I can reproduce the error with a non valid object style reference like this: 

app.selection[0].applyObjectStyle(curDoc.objectStyles.item("NON VALID NAME"),true);

So you may want to both:

- use itemByName property instead of item("the name"), item() method generally expect an index (i.e. item(0) )

- check for style existence before you wanna use it (use boolean isValid):

try{
    var curDoc = app.activeDocument;
    curDoc.pages[0].textFrames[6];
    curDoc.select (curDoc.pages[0].textFrames[6]);
    var theStyle = curDoc.objectStyles.itemByName("Text 2AB");
    if( theStyle.isValid){
        app.selection[0].applyObjectStyle(theStyle,true);
    }
    else{
        alert("Could'nt find the object style :\\")
    }
}
catch(err){
    alert(err.line+":"+err.message)
}

But of course those kind of precheck would be useful for your whole script (is a doc open, a selection valid, etc.)

Votes

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
Explorer ,
Nov 04, 2022 Nov 04, 2022

Copy link to clipboard

Copied

Thank you so much @Loic.Aigon! Your precheck method above helped me figure out what was going on! It was a super rookie mistake I guess, but helpfully it will help others down the road. I was referencing "Text A" in my code, but in the InDesign Object Panel I used all caps, "TEXT A". I didn't know the code needed to be typed EXCACTLY as it appears in the Object Panel and it's also case-sensitive.

Votes

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
Explorer ,
Nov 04, 2022 Nov 04, 2022

Copy link to clipboard

Copied

LATEST

This original code is correct for finding a text box and applying an Object Style:

var curDoc = app.activeDocument;
curDoc.pages[0].textFrames[6];
curDoc.select (curDoc.pages[0].textFrames[6]);
app.selection[0].applyObjectStyle(curDoc.objectStyles.item("Object Style Name Here"),true);

Votes

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