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…

Regis Zaleman
  • 3,182
  • 6
  • 29
  • 30

2 Answers2

1

Do you have a reason to examine only rectangles, ovals, and polygons? If not, you can use pageItems. Ditch the shape selection and the switch, and use:

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

Since you have each chapter in a separate document, you could also just change the definition of your object style:

oldStyle.fillColor = newSwatch;

so you don't have to loop over the actual objects. Not tested but it should work.

Jongware
  • 22,200
  • 8
  • 54
  • 100
0

Well, I found out. The hardest part was really to find a good documentation for the Indesign javascript scripting API... Adobe's documentation is either hard to find or lacking. In addition, they publish everything as PDFs, which IMHO is really annoying. I found a good online documentation for CS6. I am working on CC but everything I used seems the same. Anyhow, I have created the following script, very incomplete and not perfect, but works for me...

// 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();
Regis Zaleman
  • 3,182
  • 6
  • 29
  • 30