I've never actually created scripts before so trying to create code from scratch is totally foreign to me, but I'm trying to make one by using various bits of code that I've found in the default Adobe scripts and other pieces that I've found online. Essentially I want it to do the following one one step:
1. Copy and paste the artwork from an RGB workspace to a CMYK workspace, converting all colour values to CMYK
2. Change all RGB blacks to 0%, 0%, 0%, 100%
3. Round the CMYK decimal values to whole numbers.
I've managed to successfully complete the first 2 steps with the below, but I'm having trouble with the rounding. I sourced the script to round off CMYK decimals from a forum comment by CarlosCanto and since it requires adding the artwork colours to the Swatches panel before running it, I'm trying to incorporate the script for that in there too, so it'll happen in one step. The swatches are being added to the panel with the script I've put together, but I still can't get the decimals to round after that. I've noticed that, once the swatches are added, the swatch is named 'Untitled' rather than named with it's CMYK values, could this be why?
Any help would be really appreciated!
Here's what I'm using below:
#target illustrator
doc=activeDocument;
for (i=0; i<doc.layers.length; i++){doc.layers[i].hasSelectedArtwork=true;} // select all
copy();//
doc2=documents.add(DocumentColorSpace.CMYK,doc.width,doc.height,doc.artboards.length); // new doc CMYK
doc2.rulerOrigin=doc.rulerOrigin; //
doc2.pageOrigin=doc.pageOrigin; //
paste(); //
for (i=0; i<doc2.pageItems.length; i++){doc2.pageItems[i].position=doc.pageItems[i].position;}
var myDoc = app.activeDocument;
// to tweak the find CMYK minimum values adjust these
var myMinC = 60
var myMinM = 60
var myMinY = 60
var myMinK = 60
// leave the rest as is
var myCf = 0
var myMf = 0
var myYf = 0
var myKf = 0
var myFill = 0
var myCs = 0
var myMs = 0
var myYs = 0
var myKs = 0
var myStroke = 0
var lengthPI = myDoc.pathItems.length;
for (var i = 0; i < lengthPI ; i++ ) {
pathRef = myDoc.pathItems[i];
if(pathRef.editable) {
myCf = pathRef.fillColor.cyan;
myMf = pathRef.fillColor.magenta;
myYf = pathRef.fillColor.yellow;
myKf = pathRef.fillColor.black;
myFill = myCf + myMf + myYf + myKf;
if (myCf > myMinC && myMf > myMinM && myYf > myMinY && myKf > myMinK ||
myFill > 299 || myKf === 100 && myFill - myKf !== 0) {
pathRef.fillColor.cyan = 0;
pathRef.fillColor.magenta = 0;
pathRef.fillColor.yellow = 0;
pathRef.fillColor.black = 100;
}
myCs = pathRef.strokeColor.cyan;
myMs= pathRef.strokeColor.magenta;
myYs = pathRef.strokeColor.yellow;
myKs = pathRef.strokeColor.black;
myStroke = myCs + myMs + myYs + myKs;
if (myCs > myMinC && myMs > myMinM && myYs > myMinY && myKs > myMinK ||
myStroke > 299 || myKs === 100 && myStroke - myKs !== 0) {
pathRef.strokeColor.cyan = 0;
pathRef.strokeColor.magenta = 0;
pathRef.strokeColor.yellow = 0;
pathRef.strokeColor.black = 100;
}
}
}
var lengthTI = myDoc.textFrames.length;
for (var i = 0; i < lengthTI ; i++ ) {
textRef = myDoc.textFrames[i].textRange;
myCf = textRef.fillColor.cyan;
myMf = textRef.fillColor.magenta;
myYf = textRef.fillColor.yellow;
myKf = textRef.fillColor.black;
myFill = myCf + myMf + myYf + myKf;
if (myCf > myMinC && myMf > myMinM && myYf > myMinY && myKf > myMinK ||
myFill > 299 || myKf === 100 && myFill - myKf !== 0) {
textRef.fillColor.cyan = 0;
textRef.fillColor.magenta = 0;
textRef.fillColor.yellow = 0;
textRef.fillColor.black = 100;
}
myCs = textRef.strokeColor.cyan;
myMs= textRef.strokeColor.magenta;
myYs = textRef.strokeColor.yellow;
myKs = textRef.strokeColor.black;
myStroke = myCs + myMs + myYs + myKs;
if (myCs > myMinC && myMs > myMinM && myYs > myMinY && myKs > myMinK ||
myStroke > 299 || myKs === 100 && myStroke - myKs !== 0) {
textRef.strokeColor.cyan = 0;
textRef.strokeColor.magenta = 0;
textRef.strokeColor.yellow = 0;
textRef.strokeColor.black = 100;
}
}
// Check there is at least 1 document open
if (app.documents.length > 0 ) {
var docRef = app.activeDocument;
// Check there is a selection in the document
if (docRef.selection.length > 0 ) {
var paths = docRef.selection;
// Add a new swatch group if it does not already exist
var swatchGroup = null;
var swatchGroupExists = false;
// Iterate through selected items
IterateSelection(paths);
}
else
alert("Select some path art with colors applied before running this script");
}
else
alert("Open a document containing some colored path art before running this script");
function IterateSelection(selectedItems)
{
// Get the fill color of each selected item
for (i = 0; i < selectedItems.length; i++) {
var pathRef = selectedItems[i];
// Iterate path items within group items
if (pathRef.typename == "GroupItem") {
var groupPaths = pathRef.pathItems;
IterateSelection(groupPaths);
}
else if (pathRef.typename == "PathItem") {
// Iterate through existing swatches, checking if the color already exists in a swatch
var swatchExists = false;
for (var j = 0; j < docRef.swatches.length; j++) {
var currentSwatchColor = docRef.swatches[j].color;
if (ColorEquals(pathRef.fillColor, currentSwatchColor)) {
}
}
if (swatchExists == false) {
// Create a new swatch in the swatch group
var newSwatch = docRef.swatches.add();
newSwatch.color = pathRef.fillColor;
}
}
}
}
function ColorEquals(fillColor, swatchColor)
{
var colorEquals = false;
// Compare colors
if (fillColor.typename == swatchColor.typename) {
switch (swatchColor.typename) {
case "CMYKColor":
colorEquals = CMYKColorEquals(fillColor, swatchColor);
break;
case "RGBColor":
colorEquals= RGBColorEquals(fillColor, swatchColor);
break;
case "GrayColor":
colorEquals = GrayColorEquals(fillColor, swatchColor);
break;
case "LabColor":
colorEquals = LabColorEquals(fillColor, swatchColor);
break;
case "SpotColor":
colorEquals = SpotColorEquals(fillColor, swatchColor);
break;
case "GradientColor":
colorEquals = CMYKColorEquals(fillColor, swatchColor);
break;
case "NoColor":
break;
case "PatternColor":
break;
default:
break;
}
}
return colorEquals;
}
function CMYKColorEquals(fillColor, swatchColor)
{
var colorEquals = false;
if ((fillColor.cyan == swatchColor.cyan) &&
(fillColor.magenta == swatchColor.magenta)&&
(fillColor.yellow == swatchColor.yellow) &&
(fillColor.black == swatchColor.black)) {
colorEquals = true;
}
return colorEquals;
}
function RGBColorEquals(fillColor, swatchColor)
{
var colorEquals = false;
if ((fillColor.red == swatchColor.red) &&
(fillColor.green == swatchColor.green)&&
(fillColor.blue == swatchColor.blue)) {
colorEquals = true;
}
return colorEquals;
}
function GrayColorEquals(fillColor, swatchColor)
{
var colorEquals = false;
if ((fillColor.gray == swatchColor.gray)) {
colorEquals = true;
}
return colorEquals;
}
function LabColorEquals(fillColor, swatchColor)
{
var colorEquals = false;
if ((fillColor.l == swatchColor.l) &&
(fillColor.a == swatchColor.a)&&
(fillColor.b == swatchColor.b)) {
colorEquals = true;
}
return colorEquals;
}
function SpotColorEquals(fillColor, swatchColor)
{
var colorEquals = false;
switch (swatchColor.spot.color.typename) {
case "CMYKColor":
colorEquals = CMYKColorEquals(fillColor.spot.color, swatchColor.spot.color);
break;
case "RGBColor":
colorEquals = RGBColorEquals(fillColor.spot.color, swatchColor.spot.color);
break;
case "GrayColor":
colorEquals = GrayColorEquals(fillColor.spot.color, swatchColor.spot.color);
break;
case "LabColor":
colorEquals = LabColorEquals(fillColor.spot.color, swatchColor.spot.color);
break;
default:
break;
}
return colorEquals;
}
var idoc = app.activeDocument;
var sws = idoc.swatches;
for (i = 0; i<sws.length; i++) {
var sw = sws[i];
if (sw.color.typename == "CMYKColor")
roundColorValues (sw.color);
else if (sw.color.typename == "SpotColor") {
//$.writeln(sw.color.spot.getInternalColor());
roundColorValues (sw.color.spot.color);
//$.writeln(sw.color.spot.getInternalColor());
}
}
function roundColorValues(color) {
var col = color;
col.cyan = Math.round(col.cyan);
col.magenta = Math.round(col.magenta);
col.yellow = Math.round(col.yellow);
col.black = Math.round(col.black);
}