Skip to main content
dublove
Legend
May 11, 2026
質問

How to import some of the styles from the “B-Title1” paragraph style in Document B into “A-Title1” in Document A?

  • May 11, 2026
  • 返信数 2.
  • 114 ビュー

Sorry, the previous post was a bit messy and off-topic.
I sorted out my thoughts.


I want to collect all the commonly used styles in a B document (assuming the path is "D:/B.indd")
When I am working on A.indd, I need to obtain the "Paragraph Line Settings" for B1.
I want to get such an interface.


for example
Paragraph line
Paragraph background
Grep


There are two issues that I cannot implement at present:
How to access the style of B.indd without opening it.
After selecting one of the items, can the content of that item be displayed on the right side.

It's roughly like this:

 

This is a piece of code copied from elsewhere.

/**
* @name A的相邻段落样式B替换BNone
*/




//call the makeDialog function
makeDialog();

//$.writeln("[" + psl + "]")
function makeDialog() {
//the dialog object
var theDialog = app.dialogs.add({ name: "Test ", canCancel: true });

//note this would not get grouped styles
with (theDialog.dialogColumns.add()) {

staticTexts.add({ staticLabel: "source document:" });
staticTexts.add({ staticLabel: "source style[DEF]: " });
staticTexts.add({ staticLabel: "target style[DEF]:" });

}

//note this would not get grouped styles
with (theDialog.dialogColumns.add()) {
//an array of style names for each dropown
listA = []
for (var i = 1; i < app.activeDocument.allParagraphStyles.length; i++) {
listA.push(app.activeDocument.allParagraphStyles[i].name)
};
styleA = dropdowns.add({ stringList: listA, selectedIndex: 0, minWidth: 80 });

listB = []
for (var j = 1; j < app.activeDocument.allParagraphStyles.length; j++) {
listB.push(app.activeDocument.allParagraphStyles[j].name)
};
styleB = dropdowns.add({ stringList: listB, selectedIndex: 0, minWidth: 80 });

listBN = []
for (var k = 1; k < app.activeDocument.allParagraphStyles.length; k++) {
listBN.push(app.activeDocument.allParagraphStyles[k].name)
};
styleBN = dropdowns.add({ stringList: listBN, selectedIndex: 0, minWidth: 80 });
}

//
with (theDialog.dialogColumns.add()) {

staticTexts.add({ staticLabel: "B.indd style options:" });
O_All = checkboxControls.add({ checkedState: false, minWidth: 20 });
O_All = checkboxControls.add({ checkedState: false, minWidth: 20 });
O_All = checkboxControls.add({ checkedState: false, minWidth: 20 });
}

with (theDialog.dialogColumns.add()) {
staticTexts.add({ staticLabel: "" });
staticTexts.add({ staticLabel: "Paragraph line" });
staticTexts.add({ staticLabel: "paragraph background" });
staticTexts.add({ staticLabel: "Grep" });

}
var res = theDialog.show();
if (res == true) {
//get the name or each dropdow choice
//main()
} else {
theDialog.destroy();
}
}

 

    返信数 2

    Community Expert
    May 14, 2026

     

    You want something like this

     

    var fileB = File.openDialog("Choose the source InDesign document", "*.indd");

    app.activeDocument.importStyles(
    ImportFormat.PARAGRAPH_STYLES_FORMAT,
    fileB,
    GlobalClashResolutionStrategy.LOAD_ALL_WITH_RENAME
    );

    So your code should look like

    var res = theDialog.show();

    if (res == true) {
    var fileB = File.openDialog("Choose B.indd", "*.indd");

    if (fileB != null) {
    app.activeDocument.importStyles(
    ImportFormat.PARAGRAPH_STYLES_FORMAT,
    fileB,
    GlobalClashResolutionStrategy.LOAD_ALL_WITH_RENAME
    );

    alert("Paragraph styles imported from B.indd.");
    }

    theDialog.destroy();
    } else {
    theDialog.destroy();
    }

     

    But this only imports the styles. It does not copy selected parts like Paragraph Rule, Paragraph Shading, or GREP from one style into another.

    For copying one group of settings, the important part is property assignment, for example Paragraph Rule Above:

    targetStyle.ruleAbove = sourceStyle.ruleAbove;
    targetStyle.ruleAboveLineWeight = sourceStyle.ruleAboveLineWeight;
    targetStyle.ruleAboveColor = sourceStyle.ruleAboveColor;
    targetStyle.ruleAboveTint = sourceStyle.ruleAboveTint;
    targetStyle.ruleAboveOffset = sourceStyle.ruleAboveOffset;

     

    importStyles() solves the closed-document part.

    A ScriptUI dialog/palette is needed for a live right-side preview.
    Individual style sections still have to be copied property-by-property.

    ----------
     

    Here’s what I’ve got so far - I used a file picker

    A dialog comes up - there’s a button on the top left to pick your B document
    Then you target the styles

    And you can select which GREP style to import from the paragraph style - all of htem import one by one, depending on which one you need to bring in. 

    Pretty chuffed with myself

     

    /**
    * Test: import paragraph styles from another InDesign file and preview/copy
    * selected style sections with a live ScriptUI preview.
    */

    if (app.documents.length === 0) {
    alert("Open A.indd first, then run this script.");
    exit();
    }

    var docA = app.activeDocument;
    var sourceFile = null;
    var sourceDoc = null;

    var w = new Window("dialog", "Paragraph Style Import Test");
    w.orientation = "column";
    w.alignChildren = ["fill", "top"];
    w.spacing = 10;
    w.margins = 14;

    var fileRow = w.add("group");
    fileRow.orientation = "row";
    fileRow.alignChildren = ["left", "center"];
    var chooseButton = fileRow.add("button", undefined, "Choose B.indd");
    var fileLabel = fileRow.add("statictext", undefined, "No source document selected");
    fileLabel.characters = 45;

    var stylePanel = w.add("panel", undefined, "Styles");
    stylePanel.orientation = "column";
    stylePanel.alignChildren = ["fill", "top"];
    stylePanel.margins = 12;

    var sourceRow = stylePanel.add("group");
    sourceRow.orientation = "row";
    sourceRow.add("statictext", undefined, "Source style:");
    var sourceDropdown = sourceRow.add("dropdownlist", undefined, []);
    sourceDropdown.preferredSize.width = 260;

    var targetRow = stylePanel.add("group");
    targetRow.orientation = "row";
    targetRow.add("statictext", undefined, "Target style:");
    var targetDropdown = targetRow.add("dropdownlist", undefined, getParagraphStyleNames(docA));
    targetDropdown.preferredSize.width = 260;
    if (targetDropdown.items.length > 0) {
    targetDropdown.selection = 0;
    }

    var body = w.add("group");
    body.orientation = "row";
    body.alignChildren = ["fill", "fill"];

    var optionPanel = body.add("panel", undefined, "B.indd style options");
    optionPanel.orientation = "column";
    optionPanel.alignChildren = ["fill", "top"];
    optionPanel.margins = 12;
    optionPanel.preferredSize.width = 180;

    var optionList = optionPanel.add("listbox", undefined, [
    "Paragraph line",
    "Paragraph background",
    "GREP"
    ]);
    optionList.preferredSize = [160, 110];
    optionList.selection = 0;

    var previewPanel = body.add("panel", undefined, "Preview");
    previewPanel.orientation = "column";
    previewPanel.alignChildren = ["fill", "fill"];
    previewPanel.margins = 12;

    var previewText = previewPanel.add("edittext", undefined, "", {
    multiline: true,
    scrolling: true,
    readonly: true
    });
    previewText.preferredSize = [420, 150];

    var grepSelectLabel = previewPanel.add("statictext", undefined, "Select GREP styles from the source paragraph style:");
    var grepList = previewPanel.add("listbox", undefined, [], {
    multiselect: true
    });
    grepList.preferredSize = [420, 90];
    grepSelectLabel.visible = false;
    grepList.visible = false;

    var buttons = w.add("group");
    buttons.alignment = "right";
    var copyButton = buttons.add("button", undefined, "Copy Selected To Target");
    var closeButton = buttons.add("button", undefined, "Close", { name: "cancel" });

    chooseButton.onClick = function () {
    var picked = File.openDialog("Choose the source InDesign document (B.indd)", "*.indd");
    if (picked === null) {
    return;
    }

    if (!picked.exists) {
    alert("The selected source document was not found.");
    return;
    }

    sourceFile = picked;
    fileLabel.text = sourceFile.fsName;

    closeSourceDoc();

    try {
    sourceDoc = app.open(sourceFile, false);
    } catch (e) {
    alert("Could not open the source document hidden: " + e);
    sourceDoc = null;
    return;
    }

    replaceDropdownItems(sourceDropdown, getParagraphStyleNames(sourceDoc));
    replaceDropdownItems(targetDropdown, getParagraphStyleNames(docA));
    selectByName(sourceDropdown, "B-Title1");
    selectByName(targetDropdown, "A-Title1");
    updatePreview();
    };

    sourceDropdown.onChange = updatePreview;
    targetDropdown.onChange = updatePreview;
    optionList.onChange = updatePreview;

    copyButton.onClick = function () {
    if (!sourceDropdown.selection || !targetDropdown.selection || !optionList.selection) {
    alert("Choose a source style, target style, and option first.");
    return;
    }

    if (!sourceDoc || !sourceDoc.isValid) {
    alert("Choose B.indd first.");
    return;
    }

    var sourceStyle = sourceDoc.paragraphStyles.itemByName(sourceDropdown.selection.text);
    var targetStyle = docA.paragraphStyles.itemByName(targetDropdown.selection.text);
    var optionName = optionList.selection.text;

    if (!sourceStyle.isValid || !targetStyle.isValid) {
    alert("Could not find the selected source or target style.");
    return;
    }

    if (optionName === "Paragraph line") {
    copyParagraphRules(sourceStyle, targetStyle);
    } else if (optionName === "Paragraph background") {
    copyParagraphBackground(sourceStyle, targetStyle);
    } else if (optionName === "GREP") {
    var selectedGrepIndexes = getSelectedGrepIndexes();
    if (selectedGrepIndexes.length === 0) {
    alert("Select one or more GREP styles to copy.");
    return;
    }
    copyGrepStyles(sourceStyle, targetStyle, selectedGrepIndexes);
    }

    alert("Copied " + optionName + " from " + sourceStyle.name + " to " + targetStyle.name + ".");
    };

    closeButton.onClick = function () {
    closeSourceDoc();
    w.close();
    };

    updatePreview();
    w.show();

    function updatePreview() {
    if (!sourceDropdown.selection) {
    previewText.text = "Choose B.indd to import its paragraph styles, then select a source style.";
    return;
    }

    if (!sourceDoc || !sourceDoc.isValid) {
    previewText.text = "Choose B.indd to read its paragraph styles.";
    return;
    }

    var sourceStyle = sourceDoc.paragraphStyles.itemByName(sourceDropdown.selection.text);
    if (!sourceStyle.isValid) {
    previewText.text = "The selected source style is not valid.";
    return;
    }

    var optionName = optionList.selection ? optionList.selection.text : "Paragraph line";

    if (optionName === "Paragraph line") {
    updateGrepList(sourceStyle, false);
    previewText.text = describeParagraphRules(sourceStyle);
    } else if (optionName === "Paragraph background") {
    updateGrepList(sourceStyle, false);
    previewText.text = describeParagraphBackground(sourceStyle);
    } else if (optionName === "GREP") {
    updateGrepList(sourceStyle, true);
    previewText.text = describeGrepStyles(sourceStyle);
    }
    }

    function getParagraphStyleNames(doc) {
    var names = [];
    for (var i = 1; i < doc.allParagraphStyles.length; i++) {
    names.push(doc.allParagraphStyles[i].name);
    }
    return names;
    }

    function replaceDropdownItems(dropdown, names) {
    dropdown.removeAll();
    for (var i = 0; i < names.length; i++) {
    dropdown.add("item", names[i]);
    }
    if (dropdown.items.length > 0) {
    dropdown.selection = 0;
    }
    }

    function selectByName(dropdown, name) {
    for (var i = 0; i < dropdown.items.length; i++) {
    if (dropdown.items[i].text === name) {
    dropdown.selection = i;
    return;
    }
    }
    }

    function describeParagraphRules(style) {
    return [
    "Paragraph line",
    "",
    "Rule above: " + valueOf(style, "ruleAbove"),
    "Above weight: " + valueOf(style, "ruleAboveLineWeight"),
    "Above color: " + nameOf(valueOf(style, "ruleAboveColor")),
    "Above tint: " + valueOf(style, "ruleAboveTint"),
    "Above offset: " + valueOf(style, "ruleAboveOffset"),
    "Above left indent: " + valueOf(style, "ruleAboveLeftIndent"),
    "Above right indent: " + valueOf(style, "ruleAboveRightIndent"),
    "",
    "Rule below: " + valueOf(style, "ruleBelow"),
    "Below weight: " + valueOf(style, "ruleBelowLineWeight"),
    "Below color: " + nameOf(valueOf(style, "ruleBelowColor")),
    "Below tint: " + valueOf(style, "ruleBelowTint"),
    "Below offset: " + valueOf(style, "ruleBelowOffset"),
    "Below left indent: " + valueOf(style, "ruleBelowLeftIndent"),
    "Below right indent: " + valueOf(style, "ruleBelowRightIndent")
    ].join("\r");
    }

    function describeParagraphBackground(style) {
    return [
    "Paragraph background",
    "",
    "Shading on: " + valueOf(style, "paragraphShadingOn"),
    "Shading color: " + nameOf(valueOf(style, "paragraphShadingColor")),
    "Shading tint: " + valueOf(style, "paragraphShadingTint"),
    "Shading overprint: " + valueOf(style, "paragraphShadingOverprint"),
    "Top offset: " + valueOf(style, "paragraphShadingTopOffset"),
    "Bottom offset: " + valueOf(style, "paragraphShadingBottomOffset"),
    "Left offset: " + valueOf(style, "paragraphShadingLeftOffset"),
    "Right offset: " + valueOf(style, "paragraphShadingRightOffset")
    ].join("\r");
    }

    function describeGrepStyles(style) {
    var lines = ["GREP"];

    try {
    var grepStyles = style.nestedGrepStyles.everyItem().getElements();

    if (grepStyles.length === 0) {
    lines.push("");
    lines.push("No GREP styles.");
    } else {
    for (var i = 0; i < grepStyles.length; i++) {
    var grepStyle = grepStyles[i];
    lines.push("");
    lines.push((i + 1) + ". Expression: " + grepStyle.grepExpression);
    lines.push("Character style: " + nameOf(grepStyle.appliedCharacterStyle));
    }
    }
    } catch (e) {
    lines.push("");
    lines.push("Could not read GREP styles: " + e);
    }

    return lines.join("\r");
    }

    function updateGrepList(style, showList) {
    grepSelectLabel.visible = showList;
    grepList.visible = showList;
    grepList.removeAll();

    if (!showList) {
    w.layout.layout(true);
    return;
    }

    try {
    var grepStyles = style.nestedGrepStyles.everyItem().getElements();
    for (var i = 0; i < grepStyles.length; i++) {
    var label = (i + 1) + ". " + grepStyles[i].grepExpression + " [" + nameOf(grepStyles[i].appliedCharacterStyle) + "]";
    var item = grepList.add("item", label);
    item.grepIndex = i;
    }
    } catch (e) {
    var errorItem = grepList.add("item", "Could not read GREP styles: " + e);
    errorItem.grepIndex = -1;
    }

    w.layout.layout(true);
    }

    function copyParagraphRules(sourceStyle, targetStyle) {
    targetStyle.ruleAbove = sourceStyle.ruleAbove;
    targetStyle.ruleAboveLineWeight = sourceStyle.ruleAboveLineWeight;
    targetStyle.ruleAboveColor = getTargetSwatch(sourceStyle.ruleAboveColor, docA);
    targetStyle.ruleAboveOffset = sourceStyle.ruleAboveOffset;
    }

    function copyParagraphBackground(sourceStyle, targetStyle) {
    copyProperties(sourceStyle, targetStyle, [
    "paragraphShadingOn",
    "paragraphShadingColor",
    "paragraphShadingTint",
    "paragraphShadingOverprint",
    "paragraphShadingTopOffset",
    "paragraphShadingBottomOffset",
    "paragraphShadingLeftOffset",
    "paragraphShadingRightOffset"
    ]);
    }

    function copyGrepStyles(sourceStyle, targetStyle, selectedIndexes) {
    docA.importStyles(
    ImportFormat.CHARACTER_STYLES_FORMAT,
    sourceFile,
    GlobalClashResolutionStrategy.DO_NOT_LOAD_THE_STYLE
    );

    var sourceGrepStyles = sourceStyle.nestedGrepStyles.everyItem().getElements();
    for (var i = 0; i < selectedIndexes.length; i++) {
    var sourceIndex = selectedIndexes[i];
    if (sourceIndex < 0 || sourceIndex >= sourceGrepStyles.length) {
    continue;
    }

    var sourceGrepStyle = sourceGrepStyles[sourceIndex];
    var characterStyleName = sourceGrepStyle.appliedCharacterStyle.name;
    var targetCharacterStyle = docA.characterStyles.itemByName(characterStyleName);

    if (!targetCharacterStyle.isValid) {
    alert("Missing character style in target document: " + characterStyleName);
    continue;
    }

    if (hasMatchingGrepStyle(targetStyle, sourceGrepStyle.grepExpression, characterStyleName)) {
    continue;
    }

    targetStyle.nestedGrepStyles.add({
    grepExpression: sourceGrepStyle.grepExpression,
    appliedCharacterStyle: targetCharacterStyle
    });
    }
    }

    function getSelectedGrepIndexes() {
    var indexes = [];
    var selections = grepList.selection;

    if (!selections) {
    return indexes;
    }

    if (!(selections instanceof Array)) {
    selections = [selections];
    }

    for (var i = 0; i < selections.length; i++) {
    if (selections[i].grepIndex >= 0) {
    indexes.push(selections[i].grepIndex);
    }
    }

    return indexes;
    }

    function hasMatchingGrepStyle(style, expression, characterStyleName) {
    try {
    var grepStyles = style.nestedGrepStyles.everyItem().getElements();
    for (var i = 0; i < grepStyles.length; i++) {
    if (
    grepStyles[i].grepExpression === expression &&
    grepStyles[i].appliedCharacterStyle.name === characterStyleName
    ) {
    return true;
    }
    }
    } catch (e) {
    }

    return false;
    }

    function getTargetSwatch(sourceSwatch, targetDoc) {
    try {
    var existing = targetDoc.swatches.itemByName(sourceSwatch.name);
    if (existing.isValid) {
    return existing;
    }
    } catch (e) {
    }

    try {
    return targetDoc.colors.add({
    name: sourceSwatch.name,
    model: sourceSwatch.model,
    space: sourceSwatch.space,
    colorValue: sourceSwatch.colorValue
    });
    } catch (e2) {
    return sourceSwatch.name;
    }
    }

    function closeSourceDoc() {
    try {
    if (sourceDoc && sourceDoc.isValid) {
    sourceDoc.close(SaveOptions.NO);
    }
    } catch (e) {
    }
    sourceDoc = null;
    }

    function copyProperties(source, target, propertyNames) {
    for (var i = 0; i < propertyNames.length; i++) {
    try {
    target[propertyNames[i]] = source[propertyNames[i]];
    } catch (e) {
    // Some properties are version- or document-dependent.
    }
    }
    }

    function valueOf(object, propertyName) {
    try {
    return object[propertyName];
    } catch (e) {
    return "[not available]";
    }
    }

    function nameOf(value) {
    try {
    if (value && value.name) {
    return value.name;
    }
    } catch (e) {
    }
    return value;
    }

     

    dublove
    dublove作成者
    Legend
    May 14, 2026

    @Eugene Tyson 

    Your model is amazing.
    It seems that it did not read the style of the source file (B.indd)

    Perhaps I need to assign a lot of variables to the paragraph style options.

     

     

    Community Expert
    May 14, 2026

    Yeh, exactly - they’re not all assigned at the moment I was just testing Paragraph Rule Above, weight, offset etc. 

    Would need to add more to it. Not sure what your screenshots are about - it’s a lot of work by the looks of it. 

     

    It’s quite literal at the moment.

    But on a soft set of documents it worked.

     

    I don’t read your language, so the UI with overlays are not helpful in this instance. 

    rob day
    Community Expert
    Community Expert
    May 11, 2026

    ...How to access the style of B.indd without opening it.

     

    Hi ​@dublove , A document has the importStyles(format, file to get, globalStrategy) method, which lets you import paragraph styles from another closed or opened document and chose how to handle conflicts.

     

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

     

     

    dublove
    dublove作成者
    Legend
    May 14, 2026

    I don't know how to start.
    Can you help me read the style of an unopened file?