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

Applying Swatch to item

Explorer ,
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

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!

 

TOPICS
Scripting

Views

334

Translate

Translate

Report

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

Community Expert , Jan 03, 2023 Jan 03, 2023

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;
    
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Explorer ,
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

Hey @Charu Rajput ,


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

Kernzy_0-1672762755701.png

 

Votes

Translate

Translate

Report

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 ,
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Explorer ,
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

Here you go!

SwatchTest.ai 

Votes

Translate

Translate

Report

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 ,
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

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();
Best regards

Votes

Translate

Translate

Report

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
Explorer ,
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

LATEST

winner, winner, chicken dinner!

Votes

Translate

Translate

Report

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