• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

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

Contributor ,
Oct 25, 2022 Oct 25, 2022

Copy link to clipboard

Copied

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!

TOPICS
How to , Scripting

Views

76

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 25, 2022 Oct 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));

 

Likes

Translate

Translate
Community Expert ,
Oct 25, 2022 Oct 25, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 25, 2022 Oct 25, 2022

Copy link to clipboard

Copied

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 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 25, 2022 Oct 25, 2022

Copy link to clipboard

Copied

LATEST

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

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 25, 2022 Oct 25, 2022

Copy link to clipboard

Copied

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 )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 25, 2022 Oct 25, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 25, 2022 Oct 25, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines