Skip to main content
Known Participant
July 21, 2008
Question

delete and substitute another paragraph styles...

  • July 21, 2008
  • 9 replies
  • 2249 views
Hi everyone,

We need script for delete unwanted paragraph styles & character styles in InDesignCS2. While deleting also want to replace some styles for it. So can anyone help me for this. Thanks.

Thiyagu
This topic has been closed for replies.

9 replies

Known Participant
August 20, 2010

I found this script hoping it would help me out. We recently updated the template to one of our books so the paragraph & character style names line up with other projects. I added two lines to the script in the format shown and ran the script and it worked. "Great!" I thought. But when I made a comprehensive list (the script is 47 lines long), it suddenly gives me error messages and won't even get through one line of the script! I have confirmed that both the old and new style names are present in the document and match exactly. Can anyone help me understand what I'm doing wrong? I've attached the warning message and a few lines of the script. I am working in CS4. Thanks,


Matt

var myDocument = app.activeDocument;
myDocument.paragraphStyles.item("Body - Recipe Head").remove("STORY HEAD");
myDocument.paragraphStyles.item("Body - Recipe Head 2 line").remove("STORY HEAD 2LINE");
myDocument.paragraphStyles.item("Body-Recipe Head 3 lines").remove("STORY HEAD 3LINE");
myDocument.paragraphStyles.item("Body - Indent").remove("STORY BODY");


(continues this pattern for other styles)

Kasyan Servetsky
Legend
August 20, 2010

Try to reference paragraph style as Object instead of String:

myDocument.paragraphStyles.item("Body - Recipe Head").remove(myDocument.paragraphStyles.item("STORY HEAD"));

c.pfaffenbichler
Community Expert
Community Expert
July 23, 2008
Youre welcome.
jctremblay
Community Expert
Community Expert
February 5, 2010

I have modified this script so I can delete & replace paragraph styles...

var myDocument = app.activeDocument;

if (myDocument.paragraphStyles.item("SBS Step i") != null) {

if (myDocument.paragraphStyleGroups.item("MainStyles"). paragraphStyles.item("SBS Step i2") != null) {

myDocument.paragraphStyles.item("SBS Step i").remove("SBS Step i2")

}}

It is working find but, not if the "SBS Step i2" styles is inside a Group. How can we specify if a paragraph is from a group when removed.

JC

Kasyan Servetsky
Legend
February 5, 2010

Try this:

myDocument.paragraphStyles.item("SBS Step i").remove(myDocument.paragraphStyleGroups.item("MainStyles"). paragraphStyles.item("SBS Step i2"));

dhishokAuthor
Known Participant
July 23, 2008
Hi christoph,

var myDocument = app.activeDocument;
if (myDocument.characterStyles.item("name1") != null) {
if (myDocument.characterStyles.item("name2") != null) {
myDocument.characterStyles.item("name1").remove("name2")
}}

This working fine yaar. Thank you so much. I have completed my script with your help.

Thiyagu
c.pfaffenbichler
Community Expert
Community Expert
July 22, 2008
No, the Script cannot continue when one of the styles is missing (to the best of my knowledge) unless You include the conditional clauses (or some other construction to the effect).
If You include those the Script will only remove/replace Styles that it verified exist, thus avoiding errors.
dhishokAuthor
Known Participant
July 22, 2008
oooh sorry its working in CS3.

But Problem is while running the script, if the defined style in script is not there in the file it shows error like "Unterminated comment". In some of the documents few of the styles are missed out. But script should execute even the style is not present in the document. Please help me to this. Thanks once again.

Thiyagu
c.pfaffenbichler
Community Expert
Community Expert
July 22, 2008
The object error probably is caused by what Peter Kahrel mentioned, the Styles referring to one another, or one or more of the Styles having different names or being missing.
The Script should therefore incorporated if-clauses to verify that the Styles exist and have the exact names You use in the Script before running the remove-operation.
But Peter is far more experienced (his book on automating Indesign with JavaScript goes way over my head) and maybe he could provide You with the better lines of Script.

var myDocument = app.activeDocument;
if (myDocument.characterStyles.item("name1") != null) {
if (myDocument.characterStyles.item("name2") != null) {
myDocument.characterStyles.item("name1").remove("name2")
}}
dhishokAuthor
Known Participant
July 22, 2008
Thanks christoph,

It is working nicely. We need this same for CS3. But the same script is not worked out. Because it shows an object error. I have checked the syntax for CS3 in the scripting guide, i am unable to find it. Please help me to this. Thanks once again.

Thiyagu
Peter Kahrel
Community Expert
Community Expert
July 21, 2008
You also need to check nested styles and "based on" styles. This can be pretty tricky.

Peter
c.pfaffenbichler
Community Expert
Community Expert
July 21, 2008
If You are sure of the Styles names and existence this might suffice (name2 is the style replacing the the one named name1):

var myDocument = app.activeDocument;
myDocument.paragraphStyles.item("name1").remove("name2")

Same with characterStyles.

But You probably should set up clauses to verify the existence of the various Styles to avoid errors and thats where Im unfortunately out of my depth; hopefully someone else can help You there.