Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Pattern Script Not Working in Illustrator

Community Beginner ,
Mar 28, 2023 Mar 28, 2023

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;
    }
  }
}
TOPICS
Draw and design , Scripting , Tools
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Mar 28, 2023 Mar 28, 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 poin

...
Translate
Adobe
Guide ,
Mar 28, 2023 Mar 28, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 29, 2023 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2023 Mar 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 29, 2023 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2023 Mar 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2023 Mar 30, 2023

did you pay for the script?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 30, 2023 Mar 30, 2023

Unfortunately, yes

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2023 Mar 30, 2023

What the script purports to do is possible, and there are two ways to do it that I can see:

(a) not use patterns, but literally draw the dots inside a clipping path made by the filled path item, or

(b) you must create the patterns in advance (98 patterns in total if you want a different pattern for every 1% of tint) and have them available to the script in a file.

Either way, the (real!) script would be able to apply appropriate settings or pre-made pattern swatch based on the tint of the spot colored path item. One of those options would be my suggested way forward if you still have any wind in your sails.

 

Sadly, it appears that your script was written by ChatGPT, that wasn't able to know that Illustrator cannot generate patterns via the scripting API but made a valiant effort anyway. We are seeing more and more ChatGPT scripts posted here with the question "Why doesn't my script work?"

 

As for hiring a scripter to make a script to your specifications: one approach is to find an Illustrator scripter here on the forum and just look at their answering history by clicking on their username. You will quickly see if they are legitimate. A lot of us are available to hire. When you find someone who knows what they are doing, send them a PM.

- Mark

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 31, 2023 Mar 31, 2023

This was exactly my thought too, that even if OP didn't use ChatGPT it seems that whoever they hired did to get a defunct but at-a-glance reasonable script.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 02, 2023 Apr 02, 2023

I don't know anything about Scripting and didn't even know ChatGPT could even write code but overall, pretty disappointing.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2023 Apr 03, 2023
LATEST

Since you paid for a script that does not work, I would suggest to get back to the person you bought it from and ask for a fix or your money back.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines