Script to Change selected item to a Swatch Color

I have a basic JavaScript that should change the selected item from CMYK Black to a Swatch Color Named Bronze but it's not working. Any advice on haw the code should look?

// Get the active document
var doc = app.activeDocument;

// Check if there is a selection
if (doc.selection.length > 0) {
  // Get the first selected object
  var obj = doc.selection[0];

  // Check if the object has a fill
  if (obj.fillColor > 0) {
    // Set the object's fill color to the "Bronze" swatch
    obj.fillColor = 5;
  }
}

Upvotes: 1

Views: 161

Answers (1)

Change your code to this, this works, I have tested it.

// Get the active document
var doc = app.activeDocument;

// Check if there is a selection
if (doc.selection.length > 0) {
  // Get the first selected object
  var obj = doc.selection[0];

    //change your code to lines below ---v
  
 if (obj.filled) 
   obj.fillColor = activeDocument.swatches["Bronze"].color  
 
}

Upvotes: 1

Related Questions