From 8e7e660d548df5f243b799877ce088ac6693e5df Mon Sep 17 00:00:00 2001 From: Robert Estelle Date: Fri, 30 Aug 2019 17:17:38 -0400 Subject: [PATCH] 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: https://github.com/clap-rs/clap/issues/1041 --- src/cli/cli.rs | 3 ++- src/cli/colorpicker.rs | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/cli/cli.rs b/src/cli/cli.rs index e85c2b3b..cedaa132 100644 --- a/src/cli/cli.rs +++ b/src/cli/cli.rs @@ -175,7 +175,8 @@ pub fn build_cli() -> App<'static, 'static> { - grabc (https://www.muquit.com/muquit/software/grabc/grabc.html)\n \ - colorpicker (https://github.com/Jack12816/colorpicker)\n \ - chameleon (https://github.com/seebye/chameleon)\n \ - - KColorChooser (https://kde.org/applications/graphics/org.kde.kcolorchooser)") + - KColorChooser (https://kde.org/applications/graphics/org.kde.kcolorchooser)\n \ + - macOS built-in color picker") ) .subcommand( SubCommand::with_name("format") diff --git a/src/cli/colorpicker.rs b/src/cli/colorpicker.rs index 9912a997..5e5ca896 100644 --- a/src/cli/colorpicker.rs +++ b/src/cli/colorpicker.rs @@ -61,9 +61,27 @@ struct ColorPickerTool { version_output_starts_with: &'static [u8], } -/// Run an external X11 color picker tool (e.g. gpick or xcolor) and get the output as a string. +/// Run an external color picker tool (e.g. gpick or xcolor) and get the output as a string. pub fn run_external_colorpicker() -> Result { let tools = [ + #[cfg(target_os = "macos")] + ColorPickerTool { + command: "osascript", + // NOTE: This does not use `console.log` to print the value as you might expect, + // because that gets written to stderr instead of stdout regardless of the `-s o` flag. + // (This is accurate as of macOS Mojave/10.14.6). + // See related: https://apple.stackexchange.com/a/278395 + args: vec!["-l", "JavaScript", "-s", "o", "-e", " + const app = Application.currentApplication();\n + app.includeStandardAdditions = true;\n + const rgb = app.chooseColor({defaultColor: [0.5, 0.5, 0.5]})\n + .map(n => Math.round(n * 255))\n + .join(', ');\n + `rgb(${rgb})`;\n + "], + version_args: vec!["-l", "JavaScript", "-s", "o", "-e", "'ok';"], + version_output_starts_with: b"ok", + }, ColorPickerTool { command: "gpick", args: vec!["--pick", "--single", "--output"],