1

I want to code a script (in Javascript/Extendscript) that copies cell styles from a source document to one or more target documents. The functionality should be much like the existing 'Load cell styles' option in InDesign, except it should be able to do this on a batch folder instead of one document at a time.

Questions:

1)

The code below works:

cStyle_source = app.documents[0].cellStyles[1]
cStyle_target = app.documents[0].cellStyles[2]

value = cStyle_source.basedOn
cStyle_target.basedOn = value

It correctly copies the cell style chosen under 'Based on' in the source cell style to the target style.

I now try to copy the basedOn property to a target style in another open document:

cStyle_source = app.documents[0].cellStyles[1]
cStyle_target = app.documents[1].cellStyles[2]

value = cStyle_source.basedOn
cStyle_target.basedOn = value

The new document is a copy of the first one, and it contains all the same styles. However, running this code gives me the error message:

Invalid value for set property 'basedOn'. Expected CellStyle or string, but received CellStyle.

Is it me, or does that message not make sense? Why does this work when applying the property value to a style in the same document, but not to another document?

2)

Why does the example code below work for paragraph styles and character styles, but not for object styles and cell styles?

myProperties = app.activeDocument.paragraphStyles[1].properties
app.activeDocument.paragraphStyles[2].properties = myProperties

This very handily copies all properties from style 1 to style 2, and it even works across documents. Easy peasy.

Why are cell styles (and object styles) different in this regard?

It seems I will need to loop through each property to copy a complete cell style, and it is therefore that my problem in 1) above appeared.


UPDATE:

I wanted to add my solution to copying the entire cell style, including the 'other ID' objects like basedOn, appliedParagraphStyle and swatches.

function AddCStyles() {
    var cStyles_source = app.documents[1].cellStyles;
    var cStyles_target = app.activeDocument.cellStyles;
    var loopLength = cStyles_target.length
    for (var i in selectedCStyles) {
        var myProperties = cStyles_source.item(selectedCStyles[i]).properties;
        var incomingName = selectedCStyles[i];
        for (var j=0; j < loopLength; j++) {
            if (incomingName === cStyles_target[j].name) { // Checks if cell style already exists. If yes, loop through properties and overwrite
                for (var k in myProperties) {
                    try {
                    if (myProperties[k] == "[object CellStyle]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        cStyles_target[j][k] =  app.documents[0].cellStyles.item(value);
                        continue
                        }
                    if (myProperties[k] == "[object ParagraphStyle]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        cStyles_target[j][k] =  app.documents[0].paragraphStyles.item(value);
                        continue
                        }
                    if (myProperties[k] == "[object StrokeStyle]" || myProperties[k] == "[object Color]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        cStyles_target[j][k] = value;
                        continue
                        }
                    cStyles_target[j][k] = myProperties[k];
                    }
                    catch (e) {}
                    }
                }
            if (j === loopLength - 1 && cStyles_target.item(incomingName).isValid === false) {  // If cell style doesn't exist, create new and loop through properties
                var newCStyle = cStyles_target.add();               
                for (var k in myProperties) {
                    try {
                    if (myProperties[k] == "[object CellStyle]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        newCStyle[k] =  app.documents[0].cellStyles.item(value);
                        continue
                        }
                    if (myProperties[k] == "[object ParagraphStyle]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        newCStyle[k] =  app.documents[0].paragraphStyles.item(value);
                        continue
                        }
                    if (myProperties[k] == "[object StrokeStyle]" || myProperties[k] == "[object Color]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        newCStyle[k] = value;
                        continue
                        }
                    newCStyle[k] = myProperties[k];
                        }
                    catch (e) {}
                    }
                }
            }                
        }
    }
4

1 に答える 1