Skip to main content
Known Participant
March 14, 2008
Question

Wish to check if a style exists; create it if it doesn't exist.

  • March 14, 2008
  • 1 reply
  • 734 views
Hello,

I am trying to write a script that will check to see if a style already exists in a document. If it does, fine, I'll use it. If it does not, then I want to create the style. The following script errors out, telling me there is an invalid parameter.

if(document.objectStyles.item().name !== myCharStyle){
var myCharStyle = myDoc.characterStyles.add({name:"myCharStyle"});
}

I have tried using the try/catch function as it is written in Adobe's InDesign Scripting Guide: JavaScript but this also just errors out.

Thanks,
Tom

1 reply

Community Expert
March 14, 2008
Try this:

if(myDoc.characterStyles.item ("cstyle") == null)
myDoc.characterStyles.add({name:"cstyle"});
var myCharStyle = myDoc.characterStyles.item ("cstyle");

"cstyle" is the name you see in the char. style panel, myCharStyle is a variable -- these are not the same thing.

Peter