Import Styles from file
Do you want to import styles from other document through scripting? It’s really easy to achieve, and you also have control about overwriting styles with same name. Also, you can import all kind of styles, you are not limited to just Paragraph or Character styles. We will take a look to the style ‘importStyles()’, but I also prepared short example how to implement it in dialog. Also, keep in mint that we can import to the document or to the application. So, let’s get started! 🙂
First, let’s see how to use ‘importStyles()’:
//import to application app.importStyles(/*import format*/, /*source file*/, /*import policy*/); //import to active document app.activeDocument.importStyles(/*import format*/, /*source file*/, /*import policy*/);
Import format defines style group that we are importing.
Usage form: ImportFormat.CHARACTER_STYLES_FORMAT.
Name | Description |
---|---|
CELL_STYLES_FORMAT | Imports cell styles |
CHARACTER_STYLES_FORMAT | Imports character styles |
OBJECT_STYLES_FORMAT | Imports object styles |
PARAGRAPH_STYLES_FORMAT | Imports paragraph styles |
STROKE_STYLES_FORMAT | Imports stroke styles |
TABLE_AND_CELL_STYLES_FORMAT | Imports table and cell styles |
TABLE_STYLES_FORMAT | Imports table styles |
TEXT_STYLES_FORMAT | Imports character and paragraph styles |
TOC_STYLES_FORMAT | Imports table of contents styles |
Import policy defines what to do if styles with same name already exists in destination file. This parameter is optional. Default is LOAD_ALL_WITH_OVERWRITE.
Usage form: GlobalClashResolutionStrategy.DO_NOT_LOAD_THE_STYLE.
Name | Description |
---|---|
DO_NOT_LOAD_THE_STYLE | Does not import styles whose names clash with existing items |
LOAD_ALL_WITH_OVERWRITE | Overwrites existing styles whose names clash with imported items |
LOAD_ALL_WITH_RENAME | Renames imported styles whose names clash with existing items to preserve existing items |
Good, let’s see simple example:
app.activeDocument.importStyles(ImportFormat.PARAGRAPH_STYLES_FORMAT, File('/c/mySourceFile.indd'), GlobalClashResolutionStrategy.DO_NOT_LOAD_THE_STYLE);
So, this will import all paragraph styles from ‘mySourceFile.indd’ to active document, but it will skip styles with same name.
And at the end, like I said, here is the script with integrated all options for importing:
//get all import formats var importFormat = Array( ["Character Styles", ImportFormat.CHARACTER_STYLES_FORMAT], ["Paragraph Styles", ImportFormat.PARAGRAPH_STYLES_FORMAT], ["Text Styles", ImportFormat.TEXT_STYLES_FORMAT], ["TOC Styles", ImportFormat.TOC_STYLES_FORMAT], ["Object Styles", ImportFormat.OBJECT_STYLES_FORMAT], ["Stroke Styles", ImportFormat.STROKE_STYLES_FORMAT], ["Table Styles", ImportFormat.TABLE_STYLES_FORMAT], ["Cell Styles", ImportFormat.CELL_STYLES_FORMAT], ["Table and Cell Styles", ImportFormat.TABLE_AND_CELL_STYLES_FORMAT]); //get all import polices var importPolicy = Array( ["Don't import Styles with same name", GlobalClashResolutionStrategy.DO_NOT_LOAD_THE_STYLE], ["Overwrite Styles with same name", GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE], ["Import all Styles but rename those with same name", GlobalClashResolutionStrategy.LOAD_ALL_WITH_RENAME]); var importFormatD = Array(); for(var i = 0; i < importFormat.length; i++){ importFormatD.push(importFormat[i][0]); } var importPolicyD = Array(); for(var i = 0; i < importPolicy.length; i++){ importPolicyD.push(importPolicy[i][0]); } var styleImport = new Window('dialog', 'Styles Import by IndiSnip'); styleImport.alignChildren = 'center'; with(styleImport){ styleImport.fontGroup = add('group'); styleImport.fontGroup.orientation = 'column'; styleImport.fontGroup.alignment = 'fill'; styleImport.fontGroup.alignChildren = 'left'; with(styleImport.fontGroup){ styleImport.fontGroup.srcFileTxt = add('statictext', undefined, 'Source file:'); styleImport.fontGroup.srcFile = add('edittext'); styleImport.fontGroup.srcFile.enabled = false; styleImport.fontGroup.srcFile.alignment = 'fill'; styleImport.fontGroup.btnBrowse = add('button', undefined, 'Browse'); styleImport.fontGroup.btnBrowse.onClick = function(){ var fileFilters = "InDesign:*.indd, All files:*.*"; sourceFile = File.openDialog("choose file", fileFilters, false); if(sourceFile != null)styleImport.fontGroup.srcFile.text = sourceFile.fsName; } } styleImport.styleGrp = add('group'); styleImport.styleGrp.orientation = 'column'; styleImport.styleGrp.alignChildren = 'left'; with(styleImport.styleGrp){ styleImport.styleGrp.styleTxt = add('statictext', undefined, 'Import what?'); styleImport.styleGrp.styleFormatDrop = add('dropdownlist', undefined, importFormatD); styleImport.styleGrp.styleFormatDrop.selection = 0; styleImport.styleGrp.styleTxt1 = add('statictext', undefined, 'Import Policy:'); styleImport.styleGrp.stylePolicyDrop = add('dropdownlist', undefined, importPolicyD); styleImport.styleGrp.stylePolicyDrop.selection = 0; } styleImport.buttonGrp = add('group'); styleImport.buttonGrp.orientation = 'row'; with(styleImport.buttonGrp){ styleImport.buttonGrp.btnOK = add('button', undefined, 'OK'); styleImport.buttonGrp.btnCancel = add('button', undefined, 'Cancel'); } }; styleImport.center(); var importDlg = styleImport.show(); if(importDlg == true && sourceFile != null){ app.activeDocument.importStyles(importFormat[styleImport.styleGrp.styleFormatDrop.selection.index][1], sourceFile, importPolicy[styleImport.styleGrp.stylePolicyDrop.selection.index][1]); alert(importFormat[styleImport.styleGrp.styleFormatDrop.selection.index][0] + " successfully imported!"); }
That’s it!
Have fun! 😀
Hi…
Thanks for the script…It made my life easier.
Also, do you have any idea as to how to import swatches through javascript from one indesign file to another?
Thanks in advance for your reply!
Anitha
Hi,
You can simply do something like this:
var _loadSwatches = File.openDialog ( "Select Swatches file" );
if ( _loadSwatches != null ) {
try { app.activeDocument.loadSwatches ( _loadSwatches ) }
catch ( _ ) { alert ( _ ) }
}
Hope that helps.
Best,
Marijan.