Skip to content

Commit 362143d

Browse files
committed
Add macOS built-in color picker capability
`osascript` is the built-in automation program in macOS. The method of triggering the macOS color picker from `osascript` is documented here: https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/PromptforaColor.html#//apple_ref/doc/uid/TP40016239-CH86-SW1 There is a weirdness of `osascript` in that `console.log` only writes to `stderr` regardless of the documented "write to stdout" flag. A workaround was found here and linked in the relevant comment: https://apple.stackexchange.com/a/278395 The picker is only attempted when `#[cfg(target_os = "macos")]`. However it's still listed in the "Supported tools" help text because `clap` make it somewhat hard to customize that help text (e.g. generating from the list of tools) because it does not take ownership of strings. Related: clap-rs/clap#1041
1 parent 0db8ba1 commit 362143d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Diff for: src/cli/cli.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ pub fn build_cli() -> App<'static, 'static> {
175175
- grabc (https://www.muquit.com/muquit/software/grabc/grabc.html)\n \
176176
- colorpicker (https://github.com/Jack12816/colorpicker)\n \
177177
- chameleon (https://github.com/seebye/chameleon)\n \
178-
- KColorChooser (https://kde.org/applications/graphics/org.kde.kcolorchooser)")
178+
- KColorChooser (https://kde.org/applications/graphics/org.kde.kcolorchooser)\n \
179+
- macOS built-in color picker")
179180
)
180181
.subcommand(
181182
SubCommand::with_name("format")

Diff for: src/cli/colorpicker.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,27 @@ struct ColorPickerTool {
6161
version_output_starts_with: &'static [u8],
6262
}
6363

64-
/// Run an external X11 color picker tool (e.g. gpick or xcolor) and get the output as a string.
64+
/// Run an external color picker tool (e.g. gpick or xcolor) and get the output as a string.
6565
pub fn run_external_colorpicker() -> Result<String> {
6666
let tools = [
67+
#[cfg(target_os = "macos")]
68+
ColorPickerTool {
69+
command: "osascript",
70+
// NOTE: This does not use `console.log` to print the value as you might expect,
71+
// because that gets written to stderr instead of stdout regardless of the `-s o` flag.
72+
// (This is accurate as of macOS Mojave/10.14.6).
73+
// See related: https://apple.stackexchange.com/a/278395
74+
args: vec!["-l", "JavaScript", "-s", "o", "-e", "
75+
const app = Application.currentApplication();\n
76+
app.includeStandardAdditions = true;\n
77+
const rgb = app.chooseColor({defaultColor: [0.5, 0.5, 0.5]})\n
78+
.map(n => Math.round(n * 255))\n
79+
.join(', ');\n
80+
`rgb(${rgb})`;\n
81+
"],
82+
version_args: vec!["-l", "JavaScript", "-s", "o", "-e", "'ok';"],
83+
version_output_starts_with: b"ok",
84+
},
6785
ColorPickerTool {
6886
command: "gpick",
6987
args: vec!["--pick", "--single", "--output"],

0 commit comments

Comments
 (0)