Skip to main content
Participating Frequently
December 22, 2021
Answered

Add Keyboard Shortcut for the Foreground Color Picker

  • December 22, 2021
  • 2 replies
  • 5297 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

Inventsable
Legend
December 28, 2021

Thanks again, especially for the Rabbit Hole Journey here:

 

“This actually set me down a path of looking further into AIA text (like exporting Actions) because there is a "Set Color" command that Actions use which skips all of this.”

 

That is beautifully laid out, so that even a rank amature (before retirement, I was a film music editor) can sort-of follow it.

 

A couple of things I noticed were:

 

1) If run "directly" by selecting the script as a script menu item, the script opens up the Color Picker and allows manual input of a color code number. In my case, I run it from the script menu using Keyboard Maestro as such:

 

a) Use KM to prep the line of text, which is a title -- i.e set the font, style, size, underlining and kerning

b) Use KM to run your latest script from the script menu -- to open the Color Picker

c) Use KM to enter the color of this title by automatically typing it into the color input window and automatically clicking on "Okay"

d) Use KM to center the text and moves it to the top of the page.

This method works perfectly as long as I leave about .5 seconds between certain steps of the process.

 

2) If I try to run the script inside of an Illustrator action -- run by pressing F12 -- when I press that hotkey the Color Picker opens with focus on the hex input window, ready for me to type a number. I manually input my selected color number. However, when I hit "Okay," Illustrator freezes (spinning beachball).

 

So, although the latest script runs properly when selecting it manually as a script menu item or from KM as a script menu item, it freezes Illustrator when called from an Illustrator action. Is that what you would expect? Also, am I supposed to be entering the pre-determined color code someplace before running the script? Or does the script simply "take" you to the point of entering the hex number in the proper Color Picker window (which is what it seems to be doing)?

 

Thanks again for the very interesting solution to this and for providing the first baby steps in my JSON education. Do you have a recommendation for the best book(s) to learn about JSON? Or would you say that language is on the way out and one’s time would be better spent learning a different language?

 

Tom

 

 

 

 

 



quote

1) If run "directly" by selecting the script as a script menu item, the script opens up the Color Picker and allows manual input of a color code number. In my case, I run it from the script menu using Keyboard Maestro as such:

 

If I were doing this through AutoHotKey (which is very similar to Keyboard Maestro) I'd probably run a sequence of key presses starting with Alt + F (selects File menu), R (selects File > Scripts), then double up keys to run the last entry in the Scripts menu. It's not ideal but it could be an alternative that also works given the problem you note with #2.

 

quote

2) If I try to run the script inside of an Illustrator action -- run by pressing F12 -- when I press that hotkey the Color Picker opens with focus on the hex input window, ready for me to type a number. I manually input my selected color number. However, when I hit "Okay," Illustrator freezes (spinning beachball).

So, although the latest script runs properly when selecting it manually as a script menu item or from KM as a script menu item, it freezes Illustrator when called from an Illustrator action. Is that what you would expect?

 

I'd have to look into this. I've seen mentions on other threads about dealing with Actions that running one within another (like we're doing here, using an Action to run a script which itself creates and runs an Action) causes freezing. I wasn't sure if this was OS or app-version specific but it may just be a general rule-of-thumb. If this were the case (and it's very likely) then it does make sense here because the "inside" Action isn't opening the color picker but setting the color from our color value; the moment the picker closes would be the exact time freezing would occur.

 

quote

 Also, am I supposed to be entering the pre-determined color code someplace before running the script? Or does the script simply "take" you to the point of entering the hex number in the proper Color Picker window (which is what it seems to be doing)?

 

The script is supposed to take you there. Prompting the picker with a color value just sets the value to that given color, so if you were to have a green active fill then the picker should open on that green instead of coming up as black each time and so on.

 

quote

Thanks again for the very interesting solution to this and for providing the first baby steps in my JSON education. Do you have a recommendation for the best book(s) to learn about JSON? Or would you say that language is on the way out and one’s time would be better spent learning a different language?

 

Well, JSON isn't formally a part of Extendscript but otherwise it's very much standard practice and arguably more relevant in modern coding than ever before. JSON is Javascript Object Notation, which is (and this is somewhat a simplification) a way of writing a given Javascript value to a string for use in a text file or as something like a message between two endpoints (like how Slack in your browser can prompt or open the desktop app version of Slack through WebSockets). So for instance, if I have a Javascript variable like this:

 

var data = {
  foo: "bar",
  list: [1, 2, 3]
};
alert(data.foo); // "bar"

 

 

Let's say we need to write the content of the variable data and everything inside no matter the count, how deep or complex then how would we do this? If we try writing it to a text file:

 

var tempFile = File(Folder.desktop + "/tempFile.txt");
tempFile.open("w");
tempFile.write(data);
tempFile.close();

 

 

It would come out looking something like this:

 

{
  foo: "bar",
  list: [1, 2, 3]
}

 

 

That doesn't look bad and seems like it might be fine, but how do we get it out? How do we make it so that it's an object again?

 

var tempFile = File(Folder.desktop + "/tempFile.txt");
tempFile.open("r");
var data = tempFile.read();
tempFile.close();

alert(data); // "\{\r\n\tfoo:\s\"bar\",\r\n\tlist:\s[1,\s2,\s3]\r\n}"
alert(typeof data); // "String"

 

 

It looks a lot different when we try to retreive it and it's a string. How would we fix that? Well you could theoretically use eval() on it to try and evaluate it's value though eval is very dangerous (and deprecated specifically due to this) because you're trying to evaluate something you might not understand the value of or that value could be rewritten/reassigned just before evaluation in certain methods for hacking.

 

The way we solve for this entire problem is by using JSON:

 

var tempFile = File(Folder.desktop + "/tempFile.json");
tempFile.open("w");
tempFile.write(JSON.stringify(data)); // <== Note the difference here
tempFile.close();

// You cannot do this in scripting natively, you have to add JSON2.jsx or a similar polyfill.

 

 

This means our new file looks like this:

 

{
  "foo": "bar",
  "list": [1, 2, 3]
}

 

 

Your first reaction might be "That looks identical to the first time" and, well, yeah 😏 It's very close. The real difference is that I can re-integrate this value from an external source like a file very easily:

 

var tempFile = File(Folder.desktop + "/tempFile.json");
tempFile.open("r");
var data = tempFile.read();
tempFile.close();

alert(data); // "\{\r\n\tfoo:\s\"bar\",\r\n\tlist:\s[1,\s2,\s3]\r\n}"
alert(typeof data); // "String"

data = JSON.parse(data); // Because we can parse a JSON, it can become identical to it's original value.
alert(data); // "[Object Object]"
alert(data.foo); // "bar" 

 

 

And the difference looks very minor here only because I'm using such a simple example. JSON can't handle every kind of Javascript value though; it can't store functions or RegExp primitives mostly for security reasons; but you can store any amount of basic data like objects, arrays, strings, numbers, evaluated expressions, and so on regardless of the depth or complexity of that data.

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.