I'm new to scripting was handed a tool that pulled ink sport colors and made chips and have been saddled with a hard task, and need some help.
I need to create a tool wich will;
- look at a file with multiple pieces of artwork on multiple layers,
- create one color chip for each of the fill colors of the objects on each layer without repeating a chip,
- generate a text field next to the chip that pulls the name of the fill color for the chip from the swatches pallet,
- then align the chips for each layer to the artwork on that layer so they make nice neat columns below the artwork.
I have been able to creat something that looks at the swatch pallet and generates color chips for each swatch, and then name them the swatch name but I'm having trouble with aligning them, having it run once for each layer, and limiting the chips created to only one per fill color of objects.
This is the working script I have made.
function chipperMod()
{
this.windowRef = null;
}
chipperMod.prototype.run=function()
{
var docRef=app.activeDocument;
var swatchXAnchor=646;
var swatchYAnchor=42;
var win = new Window("dialog", "Chipper Mod", [200, 200, 380, 395]);
this.windowRef=win;
win.scmPanel=win.add("panel", [10, 10, 170, 150], "Template Size");
win.scmPanel.ltrPort=win.scmPanel.add("radiobutton", [10, 10, 180, 30], "Letter - Portrait");
win.scmPanel.ltrLand=win.scmPanel.add("radiobutton", [10, 40, 180, 60], "Letter - Landscape");
win.scmPanel.tabPort=win.scmPanel.add("radiobutton", [10, 70, 180, 90], "Tabloid - Portrait");
win.scmPanel.tabLand=win.scmPanel.add("radiobutton", [10, 100, 180, 120], "Tabloid - Landscape");
win.scmPanel.tabLand.value=true; //automaticly checks tabloid
win.scmPanel.ltrPort.onClick=win.scmPanel.ltrLand.onClick=win.scmPanel.tabPort.onClick=wi n.scmPanel.tabLand.onClick = function()
{
if(win.scmPanel.ltrPort.value)
{
swatchXAnchor=35;
swatchYAnchor=72;
}
else if(win.scmPanel.ltrLand.value)
{
swatchXAnchor=35;
swatchYAnchor=72;
}
else if(win.scmPanel.tabPort.value)
{
swatchXAnchor=35;
swatchYAnchor=77;
}
else if(win.scmPanel.tabLand.value)
{
swatchXAnchor=646;
swatchYAnchor=42;
}
}
win.goBtn = win.add("button", [10, 160, 80, 175], "Chip It");
win.goBtn.selected = true;
win.noBtn = win.add("button", [100, 160, 170, 175], "Cancel");
win.goBtn.onClick = function ()
{
for(i=docRef.swatches.length-1;i>=0;i--) // for i variable = # of inks used ; list length greater then 0 ; list length decreasing
{
var swatchesRef=docRef.swatches[i];
var swatchesRefName=swatchesRef.name;
var swatchGroup=docRef.groupItems.add(); // adds a group
var swatchBox=swatchGroup.pathItems.rectangle(swatchYAnchor, swatchXAnchor, 22, 15); // adds a 22x15 swatch at some ancor points bassed on paper chosen
swatchBox.fillColor=docRef.swatches.getByName(swatchesRefName).color; //fills with swatch color
swatchBox.stroked=true;
swatchBox.strokeWidth=.5;
swatchBox.strokeColor=new GrayColor();
swatchBox.strokeColor.gray=50;
var swatchLabelX= swatchBox.left+swatchBox.width+6;
var swatchLabelY = swatchBox.top-7; //setting margins
var swatchLabel = swatchGroup.textFrames.pointText([swatchLabelX,swatchLabelY]);
swatchLabel.contents = swatchesRefName;
swatchLabel.textRange.characterAttributes.size=8;
swatchLabel.textRange.characterAttributes.fillColor=new GrayColor();
swatchLabel.textRange.characterAttributes.fillColor.gray=50;
swatchXAnchor+=(84);
}//end for
win.close();
}
win.noBtn.onClick = function ()
{
win.close();
}
win.show();
}
if(typeof(chipperMod_unitTest) == "undefined") // is equal to
{
new chipperMod().run();
}
And this is how I have tryed to change it to compare the swatches to fill of path objects.
function chipperMod ()
{
this.windowRef = null;
}
chipperMod.prototype.run=function()
{
var docRef=app.activeDocument;
var swatchXAnchor=646;
var swatchYAnchor=42;
var win = new Window("dialog", "Chipper Experiment", [200, 200, 380, 395]);
this.windowRef=win;
win.scmPanel=win.add("panel", [10, 10, 170, 150], "Template Size");
win.scmPanel.ltrPort=win.scmPanel.add("radiobutton", [10, 10, 180, 30], "Letter - Portrait");
win.scmPanel.ltrLand=win.scmPanel.add("radiobutton", [10, 40, 180, 60], "Letter - Landscape");
win.scmPanel.tabPort=win.scmPanel.add("radiobutton", [10, 70, 180, 90], "Tabloid - Portrait");
win.scmPanel.tabLand=win.scmPanel.add("radiobutton", [10, 100, 180, 120], "Tabloid - Landscape");
win.scmPanel.tabLand.value=true; //automaticly checks tabloid
win.scmPanel.ltrPort.onClick=win.scmPanel.ltrLand.onClick=win.scmPanel.tabPort.onClick=wi n.scmPanel.tabLand.onClick = function()
{
if(win.scmPanel.ltrPort.value)
{
swatchXAnchor=35;
swatchYAnchor=72;
}
else if(win.scmPanel.ltrLand.value)
{
swatchXAnchor=35;
swatchYAnchor=72;
}
else if(win.scmPanel.tabPort.value)
{
swatchXAnchor=35;
swatchYAnchor=77;
}
else if(win.scmPanel.tabLand.value)
{
swatchXAnchor=646;
swatchYAnchor=42;
}
}
win.goBtn = win.add("button", [10, 160, 80, 175], "Chip It");
win.goBtn.selected = true;
win.noBtn = win.add("button", [100, 160, 170, 175], "Cancel");
// I HATE THIS FUNCTION
//
//
//
// HATE!!!
win.goBtn.onClick = function ()
{
for(i=docRef.swatches.length-1;i>=0;i--) // for i variable = # of swatches used -1; list length greater then 0 ; list length decreasing
{
var swatchesRef=docRef.swatches[i];
var swatchesRefName=swatchesRef.name;
for(ii=docRef.pathItems.length-1;ii>=0;ii--) // for ii variable = # of paths used -1; list length greater then 0 ; list length decreasing
{
var pathRef=docRef.pathItems[ii];
docRef.groupItems.add();
// problem seems to be this if
if(docRef.swatches.getByName(swatchesRefName).color==pathRef.fillColor) // if the color of the swatch = the fill color of the path then
{
var swatchGroup=docRef.groupItems.add(); // adds a group
var swatchBox=swatchGroup.pathItems.rectangle(swatchYAnchor, swatchXAnchor, 22, 15); // adds a 22x15 swatch at some ancor points bassed on paper chosen
swatchBox.fillColor=docRef.swatches.getByName(swatchesRefName).color; //fills with swatch color
swatchBox.stroked=true;
swatchBox.strokeWidth=.5;
swatchBox.strokeColor=new GrayColor();
swatchBox.strokeColor.gray=50;
var swatchLabelX= swatchBox.left+swatchBox.width+6;
var swatchLabelY = swatchBox.top-7; //setting margins
var swatchLabel = swatchGroup.textFrames.pointText([swatchLabelX,swatchLabelY]);
swatchLabel.contents = swatchesRefName;
swatchLabel.textRange.characterAttributes.size=8;
swatchLabel.textRange.characterAttributes.fillColor=new GrayColor();
swatchLabel.textRange.characterAttributes.fillColor.gray=50;
swatchXAnchor+=(84);
}//end if
}//end for
}
win.close();
}
win.noBtn.onClick = function ()
{
win.close();
}
win.show();
}
if(typeof(chipperMod_unitTest) == "undefined") // is equal to
{
new chipperMod().run();
}
Any help anyone can give me, would be much aprechiated. Maybe I have been going at this all wrong, I'm just not sure. I've tested the above and know it runs threw both of the loops and think that the problem is in if(docRef.swatches.getByName(swatchesRefName).color==pathRef.fillColor) but even after I get that figured out I dont know how to make it not repeat making a chip unless its used on multiple layers. Or how to align them based on art in layers instead of just a place on the artboard. Thanks for reading and once again any help is apprechiated.