Copy link to clipboard
Copied
Hi All,
I'm having trouble writing a script to tell me which swatches are used in the active document and I think the problem comes down to the Swatches method getSelected(), see code below. I'm storing a list of all the pageItems and the swatches in the activeDocument. Then I select each pageItem and use getSelected which should return an array of swatches for my selection. However I've noticed it only returns the strokeColor or fillColor of the pathItems for example. Is there a way to fix this? Any help would be greatly appreciated
var _pageItems = app.activeDocument.pageItems;
var _pageItemsLength = _pageItems.length;
if (_pageItemsLength > 0)
{
var _placedItemFileSwatches = app.activeDocument.swatches;
for (var i = 0; i < _pageItemsLength; i++)
{
_pageItems.layer.visible = true;
if (_pageItems.name != "")
{
app.activeDocument.selection = [_pageItems];
var _selectedSwatches = _placedItemFileSwatches.getSelected();
var _selectedSwatchesLength = _selectedSwatches.length;
for (var j = 0; j < _selectedSwatchesLength; j++)
{
if (_selectedSwatches
!= "") {
alert("Swatch '{0}' is assigned to pageItem '{1}'".fmt(_selectedSwatches
.name, _pageItems.name) ); }
}
}
}
}
hard to say exactly what the problem is without being able to test it, but my guess is that the problem lies with the way the UI displays any given swatch. For example, you always have either "fillColor" or "strokeColor" at the forefront of your swatches panel. And if you select an object that has a "Black" fill and a "White" stroke, then the result of the getSelected() method would presumably be tied to the active color/swatch type (for lack of better way to say that). So if your swatches panel
...Copy link to clipboard
Copied
hard to say exactly what the problem is without being able to test it, but my guess is that the problem lies with the way the UI displays any given swatch. For example, you always have either "fillColor" or "strokeColor" at the forefront of your swatches panel. And if you select an object that has a "Black" fill and a "White" stroke, then the result of the getSelected() method would presumably be tied to the active color/swatch type (for lack of better way to say that). So if your swatches panel has the "stroke" in front, then your swatches panel should have "White" as the selected swatch in your swatches panel.
If you're already looping the pageItems in the document, you can ignore the swatches panel and access the fillColor and strokeColor properties of that object directly.
Beware, however, this will require additional logic in order to handle groupItems or compoundPathItems (since fillColor and strokeColor are not properties of those objects).
Copy link to clipboard
Copied
You are correct It is returning whatever is at the forefront of the swatches panel. If I can switch that through scripting then that would solve my problem. I'm going to investigate further and see if there's a way...
Copy link to clipboard
Copied
If that's the only hangup, you should be able to create an action that swaps fill/stroke (but be conscious of the fact that you can't explicitly say "set the active color option to stroke" or something. you can just toggle the setting).
check Silly-V​'s post about creating dynamic actions here:
Creating a dynamic action to use with app.doScript() method.
Copy link to clipboard
Copied
Actually I think it has the 'set fill/set stroke' commands. But I'm not too sure if it always works.
Copy link to clipboard
Copied
you can check whether the fill or stroke is active
alert(app.isFillActive());
Copy link to clipboard
Copied
is it settable though? OP was saying that they were unable to read both values using the getSelected() method.
Copy link to clipboard
Copied
no, it's not settable, it will just assist setting the proper action to toggle fill/stroke
Copy link to clipboard
Copied
ah yes. good point.