macoscolorsapplescriptosx-yosemitecolor-codes

AppleScript: choose color, return color name


What I'm trying to make is this:

set retry to true
repeat while retry is true
    display dialog "In the next window you'll have to choose a color!" buttons {"Cancel", "COLORR!"} cancel button 1 default button 2
    
    set Chosencolor1 to (choose color)
    
    
    display dialog "The chosen color is: " & Chosencolor1 & " Is this right?" buttons {"NO", "YES"}
    if the button returned of the result is "no" then
        set retry to true
        display dialog "Retry is now: " & retry
    else
        set retry to false
        display dialog "Retry is now: " & retry
    end if
end repeat
end

But when I run this code it will return a color code as numbers. But what I want is that it will return a color name like: light blue, blue, green etc.

The only way I know is to do something like:

if the chosencolor1 is "000" then
set the chosencolor1 to "Black"
end if
end

is there any other, more simple way to do this? Or is this just not possible?


Solution

  • From the AppleScript Language Guide for choose color:

    Result The selected color, represented as a list of three integers from 0 to 65535 corresponding to the red, green, and blue components of a color; for example, {0, 65535, 0} represents green.

    As you have surmised, the only way to get a textual response is to program it yourself using the values of the list that your user chooses.

    Here is a very simple example that adds all the values of the chosen RGB integers, and determines if it closer to black or white:

    set myColor to choose color
    display dialog ("You have chosen: " & my fetchColorName(myColor))
    
    to fetchColorName(clst)
        set totalColorValue to ((item 1 of clst) + (item 2 of clst) + (item 3 of clst))
        set blackWhiteMidpoint to (196605 / 2)
        if totalColorValue < blackWhiteMidpoint then
            set colorName to "A color closer to black than white."
        else
            set colorName to "A color closer to white than black."
        end if
        return colorName
    end fetchColorName