Skip to main content
Kernzy
Inspiring
January 3, 2023
Answered

Applying Swatch to item

  • January 3, 2023
  • 2 replies
  • 836 views

I've seen numerous previous posts on this topic that seem to work, but I can not get mine to work.
I feel like I'm spoon-feeding it precisely what it needs, but it is not applying the '100% Black' Swatch.

var doc = app.activeDocument;

// Create a new window
var win = new Window('dialog', 'Apply Swatch');

// Create an edittext box and a button
var swatchName = win.add('edittext', undefined, '100% Black');
    swatchName.preferredSize.width = 100;
var applyButton = win.add('button', undefined, 'Apply');

// Add a click event listener to the button
applyButton.onClick = function() {
  // Get the name of the swatch from the edittext box
  var name = swatchName.text;

  // Get all swatches in the document
  var swatches = app.activeDocument.swatches;

  // Find the swatch with the specified name
  for (var i = 0; i < swatches.length; i++) {
    alert(swatches[i].name);
    if (swatches[i].name == name) {
      // Apply the swatch as the fill color of the selected object
      alert('swatch ' + swatches[i].name + ' applied');
      doc.selection[0].fillColor = swatches[i].color;
      break;
    }
  }
  win.close();
}

// Show the window
win.show();

 Any help is appreciated!

 

This topic has been closed for replies.
Correct answer Charu Rajput

Here you go!

SwatchTest.ai 


HI @Kernzy , Try following

var doc = app.activeDocument;

// Create a new window
var win = new Window('dialog', 'Apply Swatch');

// Create an edittext box and a button
var swatchName = win.add('edittext', undefined, '100% Black');
swatchName.preferredSize.width = 100;
var applyButton = win.add('button', undefined, 'Apply');

// Add a click event listener to the button
applyButton.onClick = function () {
    // Get the name of the swatch from the edittext box
    var name = swatchName.text;
    try {
        var _swatch = doc.swatches[name];
        if (app.selection[0].typename == 'CompoundPathItem') {
            doc.selection[0].pathItems[0].fillColor = _swatch.color;
        } else { // Required more handling in case if it is group
            doc.selection[0].fillColor = _swatch.color;
        }
    } catch (e) { }
    win.close();
}

// Show the window
win.show();

2 replies

Charu Rajput
Community Expert
Community Expert
January 3, 2023

Also, you don't need to traverse the whole list of swatches to find the swatch with specific name. The more clearner version of your script

var doc = app.activeDocument;

// Create a new window
var win = new Window('dialog', 'Apply Swatch');

// Create an edittext box and a button
var swatchName = win.add('edittext', undefined, '100% Black');
swatchName.preferredSize.width = 100;
var applyButton = win.add('button', undefined, 'Apply');

// Add a click event listener to the button
applyButton.onClick = function () {
    // Get the name of the swatch from the edittext box
    var name = swatchName.text;
    try {
        var _swatch = doc.swatches[name];
        doc.selection[0].fillColor = _swatch.color;
    } catch (e) { }
    win.close();
}

// Show the window
win.show();

 

The issue why it is not working for you may be what item you have selected in the document. It will be better if you post a screenshot of the Layer panel while selecting an item.

Best regards
Kernzy
KernzyAuthor
Inspiring
January 3, 2023

Hey @Charu Rajput ,


I was unable to get your script to work. Hopefully this screenshot can help!

 

Charu Rajput
Community Expert
Community Expert
January 3, 2023

Please share your ai file, as I can't see anything in Layers panel. You can share via Google drive or dropbox link.

Best regards
Charu Rajput
Community Expert
Community Expert
January 3, 2023

Hi,

Did you get any error? For me it is working assuming, the swtach with name "100% Black" exists in Swatches panel and item is selected.

Is your selection is a group or pathitem or compoudPath? 

Best regards