Skip to content

Commit

Permalink
Add macOS built-in color picker capability
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
rwe authored and sharkdp committed Sep 1, 2019
1 parent 5c8fc9e commit 8e7e660
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/cli/cli.rs
Expand Up @@ -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")
Expand Down
20 changes: 19 additions & 1 deletion src/cli/colorpicker.rs
Expand Up @@ -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<String> {
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"],
Expand Down

0 comments on commit 8e7e660

Please sign in to comment.