On (1): Cell styles (and everything else) are identified not by name or by index in the styles array, but by a document-wide unique identifier -- the id
element, which can be found in all of InDesign's native base objects. This is guaranteed to be a unique number throughout the entire document; it's one of the building blocks of ID itself.
Although the basedOn
property appears to point to a cell style, in reality it contains a number: the id number of the basedOn
cell style.
Even though the names of the cell styles can be the same in two documents, that is not true of their respective id's. id's are created on a first-served base while constructing new objects, but even if you constructed the two documents in the same way, at the same time, I would really not count on having the same id's then.
You do know, though, the name of the basedOn
cell style; and if there is a cell style in the other document with that same name, you can work around it using this:
value = cStyle_source.basedOn.name;
cStyle_target.basedOn = app.documents[1].cellStyles.item(value);
On (2): Strangely enough, the obvious approach copies only the cell style name
, and it needs a bit of copying to get the other elements over as well:
alert (app.activeDocument.cellStyles[2].name);
myProperties = app.activeDocument.cellStyles[2].properties;
cs = app.documents[1].cellStyles.add();
for (i in myProperties)
try {
cs[i] = myProperties[i];
} catch (e)
{
// ..
}
It needs a try..catch
because not all properties are read/write; it would fail on a read-only property such as id
. In addition, all properties that refer to other ID objects cannot be copied as well (basedOn, colors, line styles..). But it works well for what remains -- line thickness, for one.
What does this mean?
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.
You can loop over the files in your folder and do an app.activeDocument.import (ImportFormat.CELL_STYLES_FORMAT, from, globalStrategy)
for each of them.
See #importStyles for the full explanation of parameters.
Only thing is, there seem to be no way to import some styles -- you must set up your source file with all, and only, cell styles you want in the target files.