Copy link to clipboard
Copied
if (!(colorID in swatchlist)) {
missingswatches[colorID] = colorNodeName;
return;
}
var targetLayerName = tpNodeID + '_' + tpNodeName,
this_doc = app.activeDocument,
this_cw = this_doc.groupItems.getByName(mycw),
myswatch = swatchlist[colorID],
myswatchname = swatchlist[colorID].name,
swatchexists = false,
newswatch;
try {
var checkExistence = this_doc.swatches[myswatchname];
swatchexists = true;
//alert('swatch ' + myswatchname + ' existed');
}
catch (err) {
//alert('swatch ' + myswatchname + ' did not exist');
newswatch = this_doc.swatches.add();
newswatch.name = myswatchname;
newswatch.color = myswatch.color;
newswatch.typename = myswatch.typename;
}
if (swatchexists === true) {
var operateOnTrue = checkPathType(this_cw.pageItems.getByName(targetLayerName));
operateOnTrue.fillColor = this_doc.swatches[myswatchname].color;
} else {
var operateOnFalse = checkPathType(this_cw.pageItems.getByName(targetLayerName));
operateOnFalse.fillColor = newswatch.color;
}
That's a function that I've written to look at certain documents and pull from their swatches. We have a naming system for our swatches and it matches the IDs to determine which one to add.
My problem is that it adds the swatches perfectly fine(both to the item and to the swatch library for the current document with the name, id, and correct information), but then when adding a new swatch, it completely ERASES the swatches in the current document's library, while keeping the color on the layers.
Is there something that I'm doing incorrectly? It seems like a bug in AI more than anything - why would adding a swatch completely wipe out my swatch library?
Thanks.
I'm not sure if this applies to you but I had what seems to me like a similar issue a little while ago on a project that involved copying swatches between documents.
The gist of the issue was that when copying a swatch from one document to another the swatch.color object stores the parent document as a property depending on the colour type (ie SpotColor). When you put this swatch.color in a new or different document this property keeps its parent document value and seems to cause issues like disa
...Copy link to clipboard
Copied
Hey, could you please post a screenshot of the before & after of your swatches panel? I am curious to see what exactly gets erased or kept. For example, if you have every swatch erased, including the No-color and [Registration], it would most certainly result in the application crash.
From looking at it at a glance, I wonder about the typename, as that's a read-only property.
After thinking about it some more, I wonder if thee's something outside of this function that may be tripping up the process. Are you by any chance shuffling between several documents while this is going on, or anything?
Copy link to clipboard
Copied
Hi Silly,
Unfortunately, I cannot post a screenshot of the before & after showing the swatches due to confidentiality and whatnot. I can delete the data out though, and explain what's happening so you can see it.
Before we get to that, let me answer your last question: Yes, I am shuffling through documents to grab colors. During this, it could be grabbing any type of color, pattern, gradient, spot, process.
But, after hitting the "Apply" button, those colors are in the scene as swatches(from the code above)
This is the before, with no colors in the scene at all.
This is after hitting the "Apply" button which opens some documents and grabs colors and applies them.
In that list, there are proc colors, gradient colors, and pattern colors(no spot).
In this image, this is what happens when I apply just ONE color from another palette to a pathItem/compoundPathItem.
It erases all of the swatches from the Swatches tab, but doesn't delete them from the artboard. AND if I save the scene, it corrupts.
Sorry for the long message, but that's the best way I could explain it.
Copy link to clipboard
Copied
Hmmm, this is a long-shot, but it could be an issue with document state refreshing whilst in process of being in the script. I am assuming that the Apply button is from a dialog window. Again, it's hypothetical troubleshooting here, I have experienced difficulties when moving variables between documents. My specific issues were when referring to document objects such as pageitems or layers from one document to another. After much debugging, I noticed that my variables were referring to different things relative to which document is currently active: the variable created based on one document would refer to a whole different item when I am in another document. And yes, it was very very uncool since I've developed a 'smart' object for my pageItems, one that I technically should have been able to reference from anywhere, to do anything with. However, it was not so, and I had to re-structure my 'smart' logic into something dumber: copying the items to the new document, and building my structure from scratch in the new document when it's activated. I discovered that redraw() (in some cases), and document.activate() would help the references stay put.
As for your issue, I suspect it may be related. What happens if you save your document after the Apply button, but before adding the new swatch?
Copy link to clipboard
Copied
It's completely unpredictable based on situations, but mostly repeatable.
If I have pattern swatches or gradient swatches, the file corrupts/there is data loss. If I save and then reopen, this AI alert pops up and tells me that "so and so pattern swatch" was :
If I don't have pattern or gradient swatches and only have spot or proc swatches and save the file, close it, and reopen it, then apply a color, they will ALL stay.
If I don't have pattern or gradient swatches and only have spot or proc swatches and only save the file, do not close it, then apply a color, the behavior of removing all swatches that are 'referenced' will happen.
It seems weird that it would be referencing when I'm telling it to create a new swatch with the information. Unfortunately, I don't see another way around setting stuff up, because half the code has to be in JSX and the other has to be in JS because I'm using libraries from node.js.
My coworker just mentioned the redraw method just a few minutes ago, so I'm going to give that a shot and the document.active as well. I'll report back with what I find. Thanks, Silly-V.
Copy link to clipboard
Copied
Regarding the pattern & gradient swatches, I hate suggesting things like this as a viable alternative, but these days with all the wacky things each CC update may do, it could be a last resort: copy the path items which are filled with a pattern or gradient from a catalog document into your target document. Theoretically, the swatch information will appear in the swatch panel without a necessary process to manipulate the swatch panel directly, so, who knows.
On a side note, wow you guys must be really advanced at your work! I think most places do not even have a single person who does this kind of stuff, much less a team of people.
Copy link to clipboard
Copied
Just to follow up - neither of these have worked yet, but I have deduced that Illustrator is indeed creating a link between the documents which is HORRIBLE. I went to the source document where it's grabbing the color information from and changed color and name, and it adjusted in the target document. Given the code above, you can see that I'm adding new swatches to the active document, and not doing anything to create a link.
UHHHHH. No idea. If you have anymore, I'm all ears. Thanks for helping, Silly.
Copy link to clipboard
Copied
Bummer, very uncool indeed. But, similar to my situation, when you are able to get an idea of what it's doing, it's possible to create much-less-elegant workarounds. This is probably the worst part about AI scripting, second to not having much of the UI functions available. (Debugging bridgetalk scripts is a good tie for 2nd.) I wish you luck, please let us know if there is any breakthrough.. it will go a long way to help out folks in the future.
Edit: maybe if the 'swatchlist' you are making is based on pushing actual swatch objects to an array object, (if that is the problem), you could make a function to copy all the properties of a swatch as strings into a JSON object, and use that to build the swatches in other documents?
Copy link to clipboard
Copied
I'm not sure if this applies to you but I had what seems to me like a similar issue a little while ago on a project that involved copying swatches between documents.
The gist of the issue was that when copying a swatch from one document to another the swatch.color object stores the parent document as a property depending on the colour type (ie SpotColor). When you put this swatch.color in a new or different document this property keeps its parent document value and seems to cause issues like disappearing swatches or swatches getting renamed to "deleted global color" since I guess the swatches parent is linked to a different document.
So where I orginally had somthing like the following for all colours:
var swatchInfo = oldDocRef.swatches.getByName('MyColor');
var newSwatch = newDocRef.swatches.add();
newSwatch.color = swatchInfo.color;
newSwatch.name = swatchInfo.name;
It needed to be added differently depending on the colour type:
var swatchInfo = oldDocRef.swatches.getByName('MyColor');
switch(swatchInfo.color.typename) {
case 'CMYKColor':
var newSwatch = newDocRef.swatches.add();
newSwatch.color = swatchInfo.color;
newSwatch.name = swatchInfo.name;
break;
case 'SpotColor':
var newSwatch = newDocRef.spots.add();
newSwatch.color = swatchInfo.color.spot.color;
newSwatch.colorType = swatchInfo.color.spot.colorType;
newSwatch.name = swatchInfo.name;
break;
}
So basically if it is a spot colour you need to drill down into the colour object so you don't copy the parent property. Again not sure if this is your exact issue but maybe worth a check.
Copy link to clipboard
Copied
I was 2 minutes into rewriting what I was doing with attempting to do the workaround method that Silly mentioned earlier - this sounds promising, I'll give it a shot and report back with my findings in a few days(the office is moving around today, so I won't be fully working on this today).
Thanks!
Copy link to clipboard
Copied
Holy schnike, it fixed most of the issues - it worked for spot and process colors. I just need to figure out the properties for patterns and graphics and set it up similarly. I think the switch/break is the most important part of it, where the break doesn't allow it to assign the parent attr. You're awesome, majorLaser . And you too, Silly-V .
Copy link to clipboard
Copied
This is an excellent gem which will save thousands of headaches down the road!
Copy link to clipboard
Copied
The only thing that won't work yet is Patterns - Everything else, including Gradients worked great. I fear this may require the workaround that was discussed prior, simply because it's not just color information. Is there anything you might be aware of that would be a hidden property that a pattern would need that others would not?
My (current) code is:
case 'PatternColor': | |
/* | |
Using swatches.add will create it correctly, but keep the parent connection. | |
Using patterns.add will create the type correctly, but not keep | |
the pattern information. | |
*/ | |
newswatch = this_doc.swatches.add(); | |
newswatch.name = myswatchname; | |
newswatch.color.typename = myswatch.color.typename; | |
newswatch.color = myswatch.color; | |
break; |
I tried all manners of variations, changing newswatch to be this_doc.patterns.add(); which will create the pattern but not take the info, whereas swatches.add() will create everything correctly but keep the parent relationship, which then breaks.
I also tried variations of newswatch.color = myswatch.color to be newswatch.color.pattern.color or newswatch.color.pattern or newswatch.pattern (which is the name alone, I believe ) - Perusing through the Scripting Reference documentation did not lead me to anything. I also tried messing around with the matrix and whatnot, but it didn't lead to anything(yet) - If you have any ideas with this, I'll be going home a happy man Silly-V majorLaser
Copy link to clipboard
Copied
Hmm, there is a PatternColor object which had pattern as a property.
Copy link to clipboard
Copied
I took a quick look and it doesn't seem like there is any way to actually define the pattern. You can add a pattern with doc.patterns.add() but the returned Pattern object has no methdds or properties to set what the pattern should be. I think this is just one of those things in Illustrator that isn't exposed to javascript unless there's a hidden property you can find.
I'll leave this here as it is as close as I could get but you end up with the default pattern since there is no obvious way to grab the pattern pathitems from the source document. I'd expect there to be a swatchRef.color.pattern.pathItems but unfortunately not 😕
case 'PatternColor':
var newPatternColor = new PatternColor();
newPatternColor.matrix = swatchRef.color.matrix;
newPatternColor.reflect = swatchRef.color.reflect;
newPatternColor.reflectAngle = swatchRef.color.reflectAngle;
newPatternColor.rotation = swatchRef.color.rotation;
newPatternColor.scaleFactor = swatchRef.color.scaleFactor;
newPatternColor.sheerAngle = swatchRef.color.sheerAngle;
newPatternColor.sheerAxis = swatchRef.color.sheerAxis;
newPatternColor.shiftAngle = swatchRef.color.shiftAngle;
newPatternColor.shiftDistance = swatchRef.color.shiftDistance;
var newPattern = newDocRef.patterns.add();
newPattern.name = swatchRef.name;
newPattern.reflect = swatchRef.color.pattern.reflect;
newPatternColor.pattern = newPattern;
break;
Maybe worth making a new thread in case someone knows more about scripting patterns.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now