0

Is there a way to use variables in styles (script?) to share styles with some variations, like color?

Example: I am laying out a book, with multiple chapters. Each chapter is an InDesign document. I would like to use common styles for all the documents in the book, but they would vary in color. So instead of having multiple objects styles like: RoundedBox-red, RoundedBox-blue etc, I would only have one style, RoundedBox, and just input the value of the color variable somewhere…

4

2 回答 2

1

你有理由只检查矩形、椭圆和多边形吗?如果没有,您可以使用 pageItems。放弃形状选择和开关,并使用:

shapes = myDoc.allPageItems;
for (var i=0; i<shapes.length; i++)
{
    if (shapes[i].appliedObjectStyle.name === oldStyle.name)
    {
        shapes[i].applyObjectStyle( newStyle );
    }
}

由于您将每一章都放在单独的文档中,因此您也可以只更改对象样式的定义:

oldStyle.fillColor = newSwatch;

因此您不必遍历实际对象。未经测试,但它应该可以工作。

于 2013-07-11T23:18:12.767 回答
0

嗯,我发现了。最难的部分真的是为 Indesign javascript 脚本 API 找到一个好的文档…… Adob​​e 的文档要么很难找到,要么缺乏。此外,他们将所有内容都发布为 PDF,恕我直言,这真的很烦人。我找到了一个很好的 CS6 在线文档。我正在研究 CC,但我使用的一切似乎都一样。无论如何,我创建了以下脚本,非常不完整且不完美,但对我有用......

// Setup the dialog UI
   var myDialog = app.dialogs.add({
       name: "Style Variables",
       canCancel: true
   });

// I usually never use 'with', but this is how it is done
// in Adobe's documentation...
with(myDialog.dialogColumns.add()) {
    staticTexts.add({staticLabel: "Main Color swatch name:"});
    staticTexts.add({staticLabel: "Style to replace:"});
    staticTexts.add({staticLabel: "Replace style with:"});
    staticTexts.add({staticLabel: "Choose shape type to target:"});
}
with(myDialog.dialogColumns.add()){
    var swatchField     = textEditboxes.add({editContents:'', minWidth:180}),
        oldStyleField   = textEditboxes.add({editContents:'', minWidth:180}),
        newStyleField   = textEditboxes.add({editContents:'', minWidth:180}),
        shapeTypeField  = dropdowns.add({stringList:['Rectangles', 'Ovals', 'Polygons']});     // Defaults to rectangles
}

// Get the user input and do stuff with it
var myResult = myDialog.show();
if (myResult === true)
{
    var docs        = app.documents,
        myDoc       = docs[0],
        allStyles   = myDoc.objectStyles,
        oldStyle    = allStyles.itemByName(oldStyleField.editContents),
        newStyle    = allStyles.itemByName(newStyleField.editContents),
        swatches    = app.documents[0].swatches,
        newSwatch   = swatches.itemByName(swatchField.editContents),
        shapes;

// Get the shape type we are targetting:
switch(shapeTypeField.selectedIndex)
{
    case 0:
        shapes = myDoc.rectanges;
        break;
    case 1:
        shapes = myDoc.ovals;
    break;
case 2:
    shapes = myDoc.polygons;
    break;

default:
    shapes = myDoc.rectangles;
}
// Set the base style color to the user chosen swatch:
newStyle.fillColor = newSwatch;

    for (var i=0; i<shapes.length; i++)
    {
        if (shapes[i].appliedObjectStyle.name === oldStyle.name)
        {
            shapes[i].applyObjectStyle( newStyle );
        }
    }
}
else
{
    alert('Script cancelled, nothing was done.');
}

// Destroy dialog box
myDialog.destroy();
于 2013-07-10T19:14:25.780 回答