Skip to main content
Inspiring
October 25, 2022
Answered

How to append all styles to new document from current document in indesign using javascript

  • October 25, 2022
  • 4 replies
  • 429 views
var spDoc =app.documents[0];
var allPstyles = spDoc.allParagraphStyles;
var allCstyles = spDoc.allCharacterStyles;
spDoc.close();
var newDoc = app.documents[1];
for(var a=0; a<allPstyles.length;a++){
newDoc.paragraphStyles.add(allPstyles[0]);
}

I tried like this I am getting the styles as object but I don't know how to add it in new document!

This topic has been closed for replies.
Correct answer rob day

Maybe this?:

 

 

//the frontmost doc
var spDoc =app.documents[0];
//its file path
var fp = spDoc.fullName
spDoc.close();
//the doc behind spDoc is now the front doc
var newDoc = app.documents[0];
newDoc.importStyles(ImportFormat.PARAGRAPH_STYLES_FORMAT, File(fp));

 

4 replies

rob day
Community Expert
Community Expert
October 25, 2022

Hi @Karthik SG , Keeping the docs open doesn’t throw an error, but I don’t think you are actually moving the front document styles to the document behind. When I test your script the loop creates new styles but they all have the same properties and are named Paragraph Style 1, Paragraph Style 2...etc.

 

You could use the document’s importStyles() method:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html#d1e49521__d1e54566

Community Expert
October 25, 2022

Paragraph style has a move method that takes document object as an argument as well. This method can be investigated. So do this

  • Don't close the source document
  • Use the move method and use the document object of the destination document.

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#ParagraphStyle.html#d1e539931__d1e545655

-Manan

-Manan
Community Expert
October 25, 2022

Hi @Karthik SG ,

look into DOM documentation!

Method add() for paragraph styles does NOT take a paragraph style object as argument.

It's taking an object of properties and values:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#ParagraphStyles.html#d1e545903__d1e545952

 

Look for a different method like document.importStyles() .

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Document.html#d1e49521__d1e54566

 

Regards,
Uwe Laubender
( Adobe Community Expert )

m1b
Community Expert
Community Expert
October 25, 2022

What happens if you don't close spDoc until the very end? I can't test right now so I'm just thinking about it. If spDoc is closed, the paragraphStyles references might be invalid. Also, if you can't find a better way, perhaps you could try applying each style to some temporary text in the new document. That is likely to add them to the document.

- Mark

Inspiring
October 25, 2022

Now its working fine 

var spDoc =app.documents[0];
var allPstyles = spDoc.allParagraphStyles;
var allCstyles = spDoc.allCharacterStyles;
//spDoc.close();
var newDoc = app.documents[1];
for(var a=0; a<allPstyles.length;a++){
newDoc.paragraphStyles.add(allPstyles[0]);
}

 As you said I tired without closing the current document and now it is working fine!

 

Thanks a lot Mark @m1b 

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
October 25, 2022

Maybe this?:

 

 

//the frontmost doc
var spDoc =app.documents[0];
//its file path
var fp = spDoc.fullName
spDoc.close();
//the doc behind spDoc is now the front doc
var newDoc = app.documents[0];
newDoc.importStyles(ImportFormat.PARAGRAPH_STYLES_FORMAT, File(fp));