So, as of today I'm new to scripting for Illustrator, I'm using the ExtendScript Toolkit. However I'm not new to scripting my own solutions and I do have some experience with JavaScript already.
What I'm trying to do is this: For all selected Items->Duplicate selected item and make a simple clipping group with it
What I end up with visually doesn't change the picture, but gives me a lot of clipped colored areas which I then can edit in the isolated mode, allowing me far faster and better shading. Doing that by hand takes hours on some pictures, it'd take a second with a script.
The script itself already perfectly works for normal PathItems. As obvious in the title, as soon as I have to apply the same to CompundPathItems things stop working. My issue is somewhat similar to this old thread [Problem with compound path clipping], but I couldn't find a solution there because I get different behavior.
When I run the very same script that perfectly works with the normal PathItems with CompoundPathItems I get this: Error 9046: The top item in the group must be a path item to create a mask
Well that's a problem. In the GUI there is absolutely no difference between making a clipping mask with a simple path and a compound path. The reference guide has frankly not helped me with this issue at all, the only thing I learned from that is that the CompoundPathItem object doesn't have a clipping attribute, but those included PathItems do.
Here's what I have so far:
if ( selected[i].typename == "PathItem" ) { | |
var newGroup = doc.groupItems.add(); | |
copy = selected[i].duplicate(newGroup,ElementPlacement.PLACEATEND); | |
selected[i].moveToBeginning(newGroup); | |
newGroup.clipped = true; | |
} |
As I said, this part perfectly does what it's supposed to do for normal Paths.
For CompoundPaths I use this workaround.
if ( selected[i].typename == "CompoundPathItem" ) { | |
var newGroup = doc.groupItems.add(); | |
copy = selected[i].duplicate(newGroup,ElementPlacement.PLACEATEND); | |
selected[i].moveToBeginning(newGroup); | |
compoundItems = selected[i].pathItems; | |
compoundIndex = compoundItems.length; | |
for ( f = 0; f < compoundIndex; f++ ) {compoundItems[f].clipping=true;} | |
var lineList = new Array(10); | |
for ( l = 0; l < lineList.length; l++ ) {lineList[l] = new Array( i * 10 + 50, ((i - 5) ^ 2) * 5 +50);} | |
newPath = app.activeDocument.pathItems.add(); | |
newPath.setEntirePath(lineList); | |
newPath.moveToBeginning(newGroup); | |
newGroup.clipped = true; | |
newPath.remove(); |
Mind you, this workaround does work in so far that it bypasses that annoying and wrong error, and the Compound Clipping Path also works, with the only problem being that the Compound Clipping Path created like this is still displayed in its original colors in the Layers section, and is still selectable. When I lock the Compound Clipping Path I can work with it but still...
So the question is, what am I missing here? Surely there must be a proper way to do this.