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!
2 Correct answers
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]);
...
This original code is correct for finding a text box and applying an Object Style:
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
Copy link to clipboard
Copied
Thanks for responding! Here's the error message that I'm getting:
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.)
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.
Copy link to clipboard
Copied
This original code is correct for finding a text box and applying an Object Style:

