Pattern Script Not Working in Illustrator
Hello All,
I have been given a Script for Adobe Illustrator but when I run it, nothing appears to occur. The Script is supposed to find colors that have a tint in them and create a dot pattern and apply it onto the color. I run the script and Illustrator thinks for a couple of seconds and then nothing, not even an error. It doesn't appear to be a complex one - any ideas where the problem is?
// Define the size of the dots in millimeters
var dotSize = 1.5;
// Define the patterns for each percentage range
var patterns = [
{
min: 10,
max: 20,
spacing: 3
},
{
min: 21,
max: 30,
spacing: 2.5
},
{
min: 31,
max: 40,
spacing: 2
},
{
min: 41,
max: 50,
spacing: 1.5
},
{
min: 51,
max: 60,
spacing: 1
},
{
min: 61,
max: 70,
spacing: 0.5
},
{
min: 71,
max: 80,
spacing: 0.25
}
];
// Get the active document and loop through its swatches
var doc = app.activeDocument;
for (var i = 0; i < doc.swatches.length; i++) {
var swatch = doc.swatches[i];
if (swatch.colorType == ColorModel.SPOT && swatch.tint != 100) {
// Create a new pattern swatch for the current color and tint
var pattern = doc.swatches.add();
pattern.colorType = ColorModel.PATTERN;
pattern.name = swatch.name + " Pattern";
pattern.pattern = doc.patterns.add();
pattern.pattern.name = swatch.name + " Pattern";
pattern.pattern.width = dotSize;
pattern.pattern.height = dotSize;
pattern.pattern.spaceBefore = 0;
pattern.pattern.spaceAfter = 0;
pattern.pattern.dotSize = dotSize;
pattern.pattern.angle = 0;
pattern.pattern.spacing = getSpacing(swatch.tint);
pattern.pattern.fillColor = swatch.color;
pattern.pattern.strokeColor = swatch.color;
// Apply the pattern swatch to all artwork that uses the current swatch
applyPatternSwatch(swatch, pattern);
}
}
// Get the spacing for the given tint
function getSpacing(tint) {
for (var i = 0; i < patterns.length; i++) {
var pattern = patterns[i];
if (tint >= pattern.min && tint <= pattern.max) {
return pattern.spacing;
}
}
// If the tint is over 80%, use solid fill color instead of pattern
return 0;
}
// Apply the given pattern swatch to all artwork that uses the given swatch
function applyPatternSwatch(swatch, patternSwatch) {
for (var i = 0; i < doc.pageItems.length; i++) {
var item = doc.pageItems[i];
if (item.fillColor && item.fillColor.spot && item.fillColor.spot.name == swatch.name) {
item.fillColor = patternSwatch.color;
item.fillColor.pattern = patternSwatch.pattern;
}
if (item.strokeColor && item.strokeColor.spot && item.strokeColor.spot.name == swatch.name) {
item.strokeColor = patternSwatch.color;
item.strokeColor.pattern = patternSwatch.pattern;
}
}
}