-
1. Re: loadSwatches with replacing
Chinnadk May 17, 2014 3:02 AM (in response to RobertKyle)Hi Robert,
var array1 = [];
var array2 = [];
var doc1swatches = app.activeDocument.swatches;
for(var i =4;i<doc1swatches.length;i++)
{
array1.push(doc1swatches[i].name);
}
var doc2 = File("C:/test.indd");
app.open(doc2);
var doc2swatches = app.activeDocument.swatches;
for(var i =4;i<doc2swatches.length;i++)
{
array2.push(doc2swatches[i].name);
}
app.activeDocument.close();
for(var i =0;i<array1.length;i++)
{
for(var j=0;j<array2.length;j++)
{
if(array1[i] == array2[j])
{
app.activeDocument.swatches.item(array1[i]).remove();
}
}
}
app.activeDocument.loadSwatches(doc2);
Regards,
Chinna
-
2. Re: loadSwatches with replacing
RobertKyle May 19, 2014 8:06 AM (in response to Chinnadk)Ah, I was afraid there was a loop in my future.
I'll try this out later, but I'm afraid that removing the duplicate names before loading the new swatches will affect any objects that are using the old swatches. I don't see how they could possibly connect with the new swatch if it's not there when the old one disappears.
So maybe the answer is to load the new swatches first, then find all of the pairs made up of SwatchName and SwatchName 2, redefine SwatchName with the cmyk values of #2, then remove #2.
I was just hoping that there was some cool one-liner to overwrite the old swatch with the new one, as we can do with paragraph, object and character styles.
-
3. Re: loadSwatches with replacing
Jump_Over May 19, 2014 8:13 AM (in response to RobertKyle)Hi,
There is a 1-liner indeed. It is a swatch.remove(swatch) method.
While removing a swatch you can specify which one to replace with, so each occurrence of "old" one will be replaced with the "new" one.
Jarek
-
4. Re: loadSwatches with replacing
RobertKyle May 19, 2014 8:20 AM (in response to Jump_Over)OK, so that means the new swatches have to be loaded first. Then loop to find the Name + Name 2 pairs. Then Name.remove(Name 2). Then I'll probably have to change Name2 back the Name so that I don't get Name 2 2 next time.
Seems doable. Now all that I need is one of those Round Tuits.
Thanks to both.
-
5. Re: loadSwatches with replacing
RobertKyle May 27, 2014 7:47 AM (in response to RobertKyle)So Step 1 of this effort appears to be:
var currentSwatchNames = app.activeDocument.swatches.everyItem().name;
app.activeDocument.loadSwatches(styleFile); // stylefile previously defined
var updatedSwatchNames = app.activeDocument.swatches.everyItem().name;
In my limited experience with this, any new swatches appear to be added to the end of the array called updatedSwatchNames. So I'm wondering if it's safe to simply compare the lengths of the two arrays and grab the "extra" items from the end of updatedSwatchNames?
-
6. Re: loadSwatches with replacing
RobertKyle May 29, 2014 6:57 AM (in response to RobertKyle)In case anyone else might find this useful:
var styleFile=// your source file here var currentSwatchNames = app.activeDocument.swatches.everyItem().name;
app.activeDocument.loadSwatches(styleFile);
var updatedSwatchNames = app.activeDocument.swatches.everyItem().name;
var newSwatches = updatedSwatchNames.length - currentSwatchNames.length;
if newSwatches===0 { exit() }
for (var i = 1; i <= newSwatches; i++) {
var newSwatchName = updatedSwatchNames[updatedSwatchNames.length-i];
var rootName = newSwatchName.match(/.+(?= \d+$)/); // iow: any string followed by a space and a number. Null for no match
if (rootName!=null && app.activeDocument.swatches.item(rootName[0]).isValid) { // iow: Swatch "rootName" exists
app.activeDocument.swatches.item(rootName).remove(app.activeDocument.swatches.item(newSwat chName)); // replace old swatch with new app.activeDocument.swatches.item(newSwatchName).name=rootName; // apply old name to new swatch } // end if
}// end loop
I'm not entirely sure that any new swatches created by loadSwatch go the end of the collection, but I'll use this for a while and watch to see if I'm proven wrong. I'm hoping that someone can shed some light on the question of how InDesign orders the items in a collection. In this case, it seems to be oldest first, but there's another new thread focusing on how the pageItems collection is ordered, which seems to have something to do with layering.



