Copy link to clipboard
Copied
Hello guys,
I'm trying to put together the script which change color to another swatch color.
So for example in this case, it should change stroke color to green:
I have this, I tried to modify it in a couple of ways, but it still doesnt work:
var swatches = app.activeDocument.swatches;
for (i=0; i<app.selection.length; i++)
{
if (app.selection[i].swatches.index < app.swatches.length-1)
app.selection[i].swatches = app.swatches.nextItem(app.selection[i].swatches);
}
A PageItem does not have a swatches property. It has a .fillColor and .strokeColor property. So, you might do something like:
for (i=0; i< app.selection.length; i++)
{
try { app.selection[i].fillColor = app.activeDocument.swatches.nextItem(app.selection[i].fillColor); }
catch(e) {}
}
Copy link to clipboard
Copied
A PageItem does not have a swatches property. It has a .fillColor and .strokeColor property. So, you might do something like:
for (i=0; i< app.selection.length; i++)
{
try { app.selection[i].fillColor = app.activeDocument.swatches.nextItem(app.selection[i].fillColor); }
catch(e) {}
}
Copy link to clipboard
Copied
Thank you, @brianp311 , this works, but my idea was the script switch the active swatch (of selected object). If I'll follow your idea, it means I'll need another scripts like this for .strokeColor and also for text .fillColor (if text is selected), which means too much shortcuts (I'll also go to create backward script for previousItem) for all of those scripts, I'd like to avoid that. 🙂
Copy link to clipboard
Copied
Sure. I don't know exactly what your goal is by use of NextItem. Just wanted to show the correct properties to use.
Copy link to clipboard
Copied
Swatches can be reordered in the panel, so using .nextItem is dangerous. As Brian mentioned, 'swatch' is not a property of page item, but you can create an object style, apply the swatch, and apply the object style to the page item.
P.
Copy link to clipboard
Copied
Thank you @Peter Kahrel, but I dont think I can use object style for my intention, because I alredy use for those items object styles which defines size of the object, but the color is everytime different, actually I need those objects in every color of the predefined Swatch list, so I think the best way is just copy the object (actually I copy the whole page) and then press the shortcut for switching between swatches.
Copy link to clipboard
Copied
Hi @sergio-cz , In this case I’m not sure you need the loop for a single selected page item. You could get your green swatch by name and apply it to selection[0]
c = app.activeDocument.swatches.itemByName("C=75 M=5 Y=100 K=0")
app.activeDocument.selection[0].strokeColor = c
or for a selection of more than one
c = app.activeDocument.swatches.itemByName("C=75 M=5 Y=100 K=0")
var s=app.activeDocument.selection;
for (var i = 0; i < s.length; i++){
s[i].strokeColor = c
};
Copy link to clipboard
Copied
Hi, @rob day thank you for your answer, but I think I need the loop, because its about 50 predefined colors in the Swatch. I have to create document with 50 pages, and I need the certain objects in all of those colors (always 1 page for 1 color). So I do it this way: (create the first page), copy the whole page, make some edits (like different text for everypage, etc...), select the object: and there is the time for the script: I'd like to press the shortcut to switch to the next swatch for the object. And then again: copy the whole page, ...
I can use the suggestion from brianp311 in the first answer, but it means I'll need different scripts for fill color, stroke color and text color (in case I have to color also text). So I think the better solution is one script based on active swatch (if stroke color is selected, it switches only stroke color, if fill color is active, script switches fill color).