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!
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;
...
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?
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Here you go!
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();
Copy link to clipboard
Copied
winner, winner, chicken dinner!