Javascript to find all pantone swatches on a single layer
I have a script that creates two pull bands (large rectangles) to be added to a document. Currently it is filling the rectangles with colors created in the script. But then those have to be converted to the colors of the objects (or pathItems) on the "Art" layer. I'm hoping there's a way to get all the colors on the "Art" layer, let that total number determine how many bands are created and apply those colors instead. The colors are always pantones and never more than five. Any ideas? Any help is always appreciated!
Here's what I have so far...
var ppi = 72;
var pullBand = prompt("Off Knife:", ".125");
var pullBandKnife = pullBand * ppi;
var pastCut = prompt("Past Cut:", ".5");
var pastCut1 = pastCut * ppi;
var colorCount =prompt("If 2 or more colors: (2,3,4,5)", "X");
var offsetBand = prompt("Offset amout:", ".125");
var offsetBand1 = offsetBand * ppi;
var doc = app.activeDocument;
var dieLayer = doc.layers ["DIE"];
dieLayer.hasSelectedArtwork = true;
app.executeMenuCommand('group');
if(app.documents.length > 0)
{
mySelection = app.activeDocument.selection;
if (mySelection.length > 0)
{
for (var i=0; i<mySelection.length; i++)
{
var objWidth = mySelection.width;
var objHeight = mySelection.height;
var topCalc = mySelection.position[1];
var leftCalc = mySelection.position[0];
}
}
else
{
alert("Nothing is selected.");
}
redraw();
alert("Done!");
}
else
{
alert("No document open.");
}
var doc = app.activeDocument;
var newSpot = doc.spots.add();
var newSpotColor = new CMYKColor();
newSpotColor.cyan = 0;
newSpotColor.magenta = 0;
newSpotColor.yellow = 0;
newSpotColor.black = 100;
newSpot.name = "Color1";
newSpot.colorType = ColorModel.SPOT;
newSpot.color = newSpotColor;
var newSpotColor = new SpotColor();
newSpotColor.spot = newSpot;
var newSpot2 = doc.spots.add();
var newSpotColor2 = new CMYKColor();
newSpotColor2.cyan = 0;
newSpotColor2.magenta = 100;
newSpotColor2.yellow = 100;
newSpotColor2.black = 0;
newSpot2.name = "Color2";
newSpot2.colorType = ColorModel.SPOT;
newSpot2.color = newSpotColor2;
var newSpotColor2 = new SpotColor();
newSpotColor2.spot = newSpot2;
var newSpot3 = doc.spots.add();
var newSpotColor3 = new CMYKColor();
newSpotColor3.cyan = 75;
newSpotColor3.magenta = 50;
newSpotColor3.yellow = 25;
newSpotColor3.black = 0;
newSpot3.name = "Color3";
newSpot3.colorType = ColorModel.SPOT;
newSpot3.color = newSpotColor3;
var newSpotColor3 = new SpotColor();
newSpotColor3.spot = newSpot3;
var newSpot4 = doc.spots.add();
var newSpotColor4 = new CMYKColor();
newSpotColor4.cyan = 100;
newSpotColor4.magenta = 0;
newSpotColor4.yellow = 100;
newSpotColor4.black = 0;
newSpot4.name = "Color4";
newSpot4.colorType = ColorModel.SPOT;
newSpot4.color = newSpotColor4;
var newSpotColor4 = new SpotColor();
newSpotColor4.spot = newSpot4;
var newSpot5 = doc.spots.add();
var newSpotColor5 = new CMYKColor();
newSpotColor5.cyan = 100;
newSpotColor5.magenta = 75;
newSpotColor5.yellow = 0;
newSpotColor5.black = 0;
newSpot5.name = "Color5";
newSpot5.colorType = ColorModel.SPOT;
newSpot5.color = newSpotColor5;
var newSpotColor5 = new SpotColor();
newSpotColor5.spot = newSpot5;
var bandLayer = doc.layers.add();
bandLayer.name = "Pullbands";
//// PB Draw ///////
var band = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18, 18, objHeight + (pastCut1 * 2));
band.filled = true;
band.stroked = false;
band.fillColor = newSpotColor;
band.fillOverprint = true;
var bandTwo = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife, 18, objHeight + (pastCut1 * 2));
bandTwo.filled = true;
bandTwo.stroked = false;
bandTwo.fillColor = newSpotColor;
bandTwo.fillOverprint = true;
if (colorCount == 2){
var band2 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18 - offsetBand1, 18, objHeight + (pastCut1 * 2));
band2.filled = true;
band2.stroked = false;
band2.fillColor = newSpotColor2;
band2.fillOverprint = true;
var bandTwo2 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife + offsetBand1, 18, objHeight + (pastCut1 * 2));
bandTwo2.filled = true;
bandTwo2.stroked = false;
bandTwo2.fillColor = newSpotColor2;
bandTwo2.fillOverprint = true;
}
if (colorCount == 3){
var band2 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18 - offsetBand1, 18, objHeight + (pastCut1 * 2));
band2.filled = true;
band2.stroked = false;
band2.fillColor = newSpotColor2;
band2.fillOverprint = true;
var band3 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18 - (offsetBand1*2), 18, objHeight + (pastCut1 * 2));
band3.filled = true;
band3.stroked = false;
band3.fillColor = newSpotColor3;
band3.fillOverprint = true;
var bandTwo2 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife + offsetBand1, 18, objHeight + (pastCut1 * 2));
bandTwo2.filled = true;
bandTwo2.stroked = false;
bandTwo2.fillColor = newSpotColor2;
bandTwo2.fillOverprint = true;
var bandTwo3 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife + (offsetBand1*2), 18, objHeight + (pastCut1 * 2));
bandTwo3.filled = true;
bandTwo3.stroked = false;
bandTwo3.fillColor = newSpotColor3;
bandTwo3.fillOverprint = true;
}
if (colorCount == 4){
var band2 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18 - offsetBand1, 18, objHeight + (pastCut1 * 2));
band2.filled = true;
band2.stroked = false;
band2.fillColor = newSpotColor2;
band2.fillOverprint = true;
var band3 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18 - (offsetBand1*2), 18, objHeight + (pastCut1 * 2));
band3.filled = true;
band3.stroked = false;
band3.fillColor = newSpotColor3;
band3.fillOverprint = true;
var band4 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18 - (offsetBand1*3), 18, objHeight + (pastCut1 * 2));
band4.filled = true;
band4.stroked = false;
band4.fillColor = newSpotColor4;
band4.fillOverprint = true;
var bandTwo2 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife + offsetBand1, 18, objHeight + (pastCut1 * 2));
bandTwo2.filled = true;
bandTwo2.stroked = false;
bandTwo2.fillColor = newSpotColor2;
bandTwo2.fillOverprint = true;
var bandTwo3 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife + (offsetBand1*2), 18, objHeight + (pastCut1 * 2));
bandTwo3.filled = true;
bandTwo3.stroked = false;
bandTwo3.fillColor = newSpotColor3;
bandTwo3.fillOverprint = true;
var bandTwo4 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife + (offsetBand1*3), 18, objHeight + (pastCut1 * 2));
bandTwo4.filled = true;
bandTwo4.stroked = false;
bandTwo4.fillColor = newSpotColor4;
bandTwo4.fillOverprint = true;
}
if (colorCount == 5){
var band2 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18 - offsetBand1, 18, objHeight + (pastCut1 * 2));
band2.filled = true;
band2.stroked = false;
band2.fillColor = newSpotColor2;
band2.fillOverprint = true;
var band3 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18 - (offsetBand1*2), 18, objHeight + (pastCut1 * 2));
band3.filled = true;
band3.stroked = false;
band3.fillColor = newSpotColor3;
band3.fillOverprint = true;
var band4 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18 - (offsetBand1*3), 18, objHeight + (pastCut1 * 2));
band4.filled = true;
band4.stroked = false;
band4.fillColor = newSpotColor4;
band4.fillOverprint = true;
var band5 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc - pullBandKnife - 18 - (offsetBand1*4), 18, objHeight + (pastCut1 * 2));
band5.filled = true;
band5.stroked = false;
band5.fillColor = newSpotColor5;
band5.fillOverprint = true;
var bandTwo2 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife + offsetBand1, 18, objHeight + (pastCut1 * 2));
bandTwo2.filled = true;
bandTwo2.stroked = false;
bandTwo2.fillColor = newSpotColor2;
bandTwo2.fillOverprint = true;
var bandTwo3 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife + (offsetBand1*2), 18, objHeight + (pastCut1 * 2));
bandTwo3.filled = true;
bandTwo3.stroked = false;
bandTwo3.fillColor = newSpotColor3;
bandTwo3.fillOverprint = true;
var bandTwo4 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife + (offsetBand1*3), 18, objHeight + (pastCut1 * 2));
bandTwo4.filled = true;
bandTwo4.stroked = false;
bandTwo4.fillColor = newSpotColor4;
bandTwo4.fillOverprint = true;
var bandTwo5 = doc.pathItems.rectangle(topCalc + pastCut1, leftCalc + objWidth + pullBandKnife + (offsetBand1*4), 18, objHeight + (pastCut1 * 2));
bandTwo5.filled = true;
bandTwo5.stroked = false;
bandTwo5.fillColor = newSpotColor5;
bandTwo5.fillOverprint = true;
}