Skip to main content
Participating Frequently
December 22, 2021
Answered

Add Keyboard Shortcut for the Foreground Color Picker

  • December 22, 2021
  • 2 replies
  • 5277 views

I’ve seen tutorials on how to add the Foreground Color Picker to the keyboard shortcuts available in Illustrator, but when I open the Keyboard Shortcuts (Mac using Command-option-shift-K) and look under tools, there is no option to assign a key to either the Foreground or Background Color Picker. It seems they used to be in the Keyboard Shortcut menu (around 2016 and 2017), but I’m not seeing them now. Is there another way to access those, other than double clicking on the squares in the tools panel? Or are they possibly called something else now, so I’m not seeing them?

 

Thanks in advance,

Tom Carlson

Correct answer Inventsable

You can prompt the color picker to open via scripting:

 

app.showColorPicker(
    app.activeDocument[
        "default" + (app.isFillActive() ? "Fill" : "Stroke") + "Color"
    ]
);

 

If you were to save the above as a text file with a name like "openColorPicker.jsx", you can place it in the Program Files for your startup scripts and record an Action with Insert Menu Item (searching for "openColorPicker") targeting this script which lets you trigger it via hotkeys assigned to the action like F12. Then I'd use macros from AutoHotKey, Keyboard Maestro, Sharpkeys or etc to remap some other key combination to send F12 and trigger the action.

2 replies

Doug A Roberts
Community Expert
Community Expert
December 22, 2021
quote

I’ve seen tutorials on how to add the Foreground Color Picker to the keyboard shortcuts available in Illustrator


By @TomMxEdit

 

Are you sure these weren't for Photoshop? Can you link to one?

TomMxEditAuthor
Participating Frequently
December 22, 2021

Thanks so much Doug and Ton. You are both correct and I will now wipe the egg off my face: That was a Photoshop tutorial I was watching -- I had done a search with "Illustrator" but what came up was a PS tutorial and I wasn’t paying close enough attention.

 

So, I would assume I can’t add the Color Picker as a keyboard shortcut then, is that correct? The reason I’d like to do this is to make the Color Picker more easily accessible to a keyboard macro program (Keyboard Maestro), which doesn’t seem to want to open it via a double-click. I’ll try playing with some of the parameters in KM and also try to open it as an action via Illustrator.

 

Thanks again for your help!

 

Tom

TomMxEditAuthor
Participating Frequently
December 27, 2021

Sorry, didn't realize that opening the picker from scripting doesn't fire a Set Color command like an action would if you were recording. Someone else may have an easier way of iterating through the selection and applying color but try this:

 

/**
 * This isn't as simple of an issue as it may seem. While the below script does work decently well, there are a few caveats:
 *   - It won't assign color to anything that doesn't already have a color already.
 *     If this weren't the case, it would paint CompoundPathItems with a fill color and cover their children PathItems.
 *   - It isn't truly recursive beyond the 2nd depth. Complicated nested compound paths or groups will have deep descendents ignored.
 */

function get(type, parent, deep) {
  if (arguments.length == 1 || !parent) {
    parent = app.activeDocument;
    deep = true;
  }
  var result = [];
  if (!parent[type]) return result;
  for (var i = 0; i < parent[type].length; i++) {
    result.push(parent[type][i]);
    if (parent[type][i][type] && deep)
      result = [].concat(result, get(type, parent[type][i], deep));
    else if (
      /(compound|group)/i.test(parent[type][i].typename) &&
      parent[type][i].pathItems
    )
      result = [].concat(result, get("pathItems", parent[type][i], deep));
  }
  return result;
}
Array.prototype.filter = function(callback) {
  var filtered = [];
  for (var i = 0; i < this.length; i++)
    if (callback(this[i], i, this)) filtered.push(this[i]);
  return filtered;
};
Array.prototype.forEach = function(callback) {
  for (var i = 0; i < this.length; i++) callback(this[i], i, this);
};

function openColorPickerAndSetColor() {
  var bool = app.isFillActive(),
    color;
  try {
    color = app.showColorPicker(
      app.activeDocument["default" + (bool ? "Fill" : "Stroke") + "Color"]
    );
  } catch (err) {
    // Specified value greater error, meaning there's more than one active fill/stroke color.
    // How to best handle this? No simple way unless we just create a null color instead:
    color = app.showColorPicker(new RGBColor());
  }
  var setProp, root;
  get("selection")
    .filter(function(item) {
      return /(path|textframe)/i.test(item.typename);
    })
    .filter(function(item) {
      if (/textframe/i.test(item.typename)) {
        return !/(nocolor|null)/i.test(
          item.textRange.characterAttributes[
            (bool ? "fill" : "stroke") + "Color"
          ] + ""
        );
      } else return item[(bool ? "fille" : "stroke") + "d"];
    })
    .forEach(function(item) {
      root = /textframe/i.test(item.typename)
        ? item.textRange.characterAttributes
        : item;
      setProp = (bool ? "fill" : "stroke") + "Color";
      root[setProp] = color;
    });
}
openColorPickerAndSetColor();

 

 

 


Wow! That certainly is an impressive bit of coding there to open up the Color Picker and have access to it. It seems opeing the Color Picker and preparing it to receive hex code input for a different fill color of text is more complex than it at first appears. I’m thinking this script must have the further functionality for either inputting values for the fill or stroke color of an object, is that correct? Does it test for the current focus of either fill or stroke? Or is there something else going on in the script?

 

Sorry for the newbie questions, but scripting isn’t something I have much experience with. That said, thanks for the script and for the informative comments as well. It’s an education following along!!

 

As far as my needs are concerned, this is solved again, Inventsable, and I’ve marked it as such.

 

Tom

Ton Frederiks
Community Expert
Community Expert
December 22, 2021

There is no foreground color in Illustrator, there is a Fill and Stroke color option.

Use X to switch between targeting Fill or Stroke color.

Use Shift X to switch the Fill and Stroke colors.