Skip to main content
Participating Frequently
March 29, 2023
Answered

Pattern Script Not Working in Illustrator

  • March 29, 2023
  • 2 replies
  • 1632 views

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;
    }
  }
}
This topic has been closed for replies.
Correct answer femkeblanco

Were you given this as an Illustrator script?  Because, at a glance, it doesn't seem to be.  The script doesn't do anything because it doesn't proceed past this line

if (swatch.colorType == ColorModel.SPOT && swatch.tint != 100) {

because Illustrator swatches don't have such properties as "colorType" and "tint".  You can change the line to

if (swatch.color.typename == "SpotColor" && swatch.color.tint != 100) {

but this brings up errors related to creating a pattern.  I've not looked beyond this point because as far as I know—and I'm happy to be corrected on the point—you can't directly create a pattern with a script in Illustrator

2 replies

m1b
Community Expert
Community Expert
March 29, 2023

Hi @Impression Sandbox, did you receive this script from ChatGPT perchance? It has all the hallmarks: looks reasonable, structured sensibly, doesn't work. Unfortunately, in this case it cannot work for the reasons that @femkeblanco mentioned. The best you can probably do is look at the link shared by @femkeblanco, but if you aren't somewhat familiar with ExtendScript, it won't be a simple task, I'm afraid.

- Mark

Participating Frequently
March 29, 2023

I was given this, I asked someone to do this for me and I thought they were able to based on what they told me. Disapointing to see that the script might be totally useless.

m1b
Community Expert
Community Expert
March 29, 2023

Oh dear—that is quite strange. Yes, to me the script appears to be useless. But perhaps the bot person who wrote it could shed some light? Maybe it was written to be used with a third-party plug-in or something?

femkeblanco
femkeblancoCorrect answer
Legend
March 29, 2023

Were you given this as an Illustrator script?  Because, at a glance, it doesn't seem to be.  The script doesn't do anything because it doesn't proceed past this line

if (swatch.colorType == ColorModel.SPOT && swatch.tint != 100) {

because Illustrator swatches don't have such properties as "colorType" and "tint".  You can change the line to

if (swatch.color.typename == "SpotColor" && swatch.color.tint != 100) {

but this brings up errors related to creating a pattern.  I've not looked beyond this point because as far as I know—and I'm happy to be corrected on the point—you can't directly create a pattern with a script in Illustrator

Participating Frequently
March 29, 2023

So this Script is completely useless? Yes I was given this, I asked someone to do this for me and I thought they were able to.