Skip to main content
Inspiring
November 3, 2022
Answered

Simple Scripting Help: Find Text Box, Apply Object Style

  • November 3, 2022
  • 2 replies
  • 522 views

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);
This topic has been closed for replies.
Correct answer jimDcleveland

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

2 replies

jimDclevelandAuthorCorrect answer
Inspiring
November 4, 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);
Loic.Aigon
Legend
November 3, 2022

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

Inspiring
November 3, 2022

Thanks for responding! Here's the error message that I'm getting:

Loic.Aigon
Legend
November 3, 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]);
    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.)