Как скопировать цвет с помощью Color Picker Apple Script? - PullRequest
0 голосов
/ 03 октября 2018

Мне нужно улучшить этот Apple Script: https://gist.github.com/mariocesar/b15cddd184481f25390e0a6e5cff2d40

# Open the color picker
on convertRGBColorToHexValue(theRGBValues)
    set theHexList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
    set theHexValue to ""
    repeat with a from 1 to count of theRGBValues
        set theCurrentRGBValue to (item a of theRGBValues) div 256
        if theCurrentRGBValue is 256 then set theCurrentRGBValue to 255
        set theFirstItem to item ((theCurrentRGBValue div 16) + 1) of theHexList
        set theSecondItem to item (((theCurrentRGBValue / 16 mod 1) * 16) + 1) of theHexList
        set theHexValue to (theHexValue & theFirstItem & theSecondItem) as string
    end repeat
    return ("#" & theHexValue) as string
end convertRGBColorToHexValue

set theRGBValues to (choose color default color {255, 255, 255})
set hexValue to (convertRGBColorToHexValue(theRGBValues))

set the clipboard to hexValue as text

В настоящее время этот скрипт копирует в буфер обмена значение цвета hexa, ПОСЛЕ того, чтобы вручную нажимать кнопку «ОК» в окне «Палитра цветов».

Я хотел бы скопировать цвет в буфер обмена, как только я нажму на него, чтобы не нажимать кнопку "ОК".

Я пытался использовать click button "OK" безуспешно.

enter image description here

1 Ответ

0 голосов
/ 05 октября 2018

интересный вопрос.Я нашел отличный ответ, используя множество Applescript-ObjC от Applescript Guru Шейн Стэнли внешняя ссылка на MacScripter.net

Я немного попробовал и адаптировал (главным образомскопировал ...) его обработчик.Решение:

use scripting additions
use framework "Foundation"
use framework "AppKit"

property theRGBValues : missing value

-- check we are running in foreground
-- when starting from Script Editor, please use CTRL-CMD-R !
if not (current application's NSThread's isMainThread()) as boolean then
    display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
    error number -128
end if

set my theRGBValues to missing value -- start off empty
-- get color panel
set thePicker to current application's NSColorPanel's sharedColorPanel()
current application's NSColorPanel's setPickerMode:(current application's NSWheelModeColorPanel)
-- get the action called by the eye-dropper button; hackish...
set theViews to thePicker's contentView()'s subviews()
repeat with aView in theViews
    if aView's |class|() is current application's NSButton then
        set theAction to aView's action()
        exit repeat
    end if
end repeat
-- set what happens when you click
thePicker's setTarget:me -- message will be sent to this script
thePicker's setAction:"colorPicked:" -- message will call handler of this name
-- show the panel
thePicker's orderFront:me
-- click the eye-dropper
aView's setState:(current application's NSOnState)
thePicker's performSelector:theAction

repeat -- loop until the user clicks
    if theRGBValues is not missing value then exit repeat
    delay 0.05
end repeat

set hexValue to (convertRGBColorToHexValue(theRGBValues))

set the clipboard to hexValue as text

(*****************************************
************* Handlers *********************
******************************************)

-- gets called when you click
on colorPicked:sender
    -- get the color
    set theColor to sender's |color|()
    -- convert to correct colorspace
    set newColor to theColor's colorUsingColorSpace:(current application's NSColorSpace's deviceRGBColorSpace())
    -- get components
    set theRed to newColor's redComponent()
    set theBlue to newColor's blueComponent()
    set theGreen to newColor's greenComponent()
    -- close panel
    sender's orderOut:me
    -- set property
    set my theRGBValues to {theRed * 255 as integer, theGreen * 255 as integer, theBlue * 255 as integer}
end colorPicked:

on convertRGBColorToHexValue(givenRGBValues)
    set theHexList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
    set theHexValue to ""
    repeat with anRGBValue in givenRGBValues
        set theFirstItem to item ((anRGBValue div 16) + 1) of theHexList
        set theSecondItem to item (((anRGBValue / 16 mod 1) * 16) + 1) of theHexList
        set theHexValue to (theHexValue & theFirstItem & theSecondItem) as string
    end repeat
    return ("#" & theHexValue) as string
end convertRGBColorToHexValue

Я изменил обработчик convertRGBColorToHexValue для работы с данными, возвращаемыми палитрой цветов.

Получайте удовольствие!Майкл / Гамбург

...