Question

I am tired of clicking through the color wheel to get custom colors and I would like to use AppleScript to automate with BTT a simple button to change the selected text color to a set of company standards. I have been able to get as far as opening up the Color Picker in Keynote with AppleScript, but I can't figure out how to click the pencil. Here is what Accessibility Inspector shows: Color picker Blueberry pencil

This is my code so far, but the selection to click Blueberry pencil keeps complaining that

System Events got an error: Can’t get window "Text Color" of application process "Keynote".

tell application "System Events"
    tell process "Keynote"
        tell radio group of toolbar of window 1
            -- This prevents it from toggling the button when already selected
            if value of radio button "Format" = {{0}} then
                click radio button "Format"
            end if
        end tell
        tell scroll area 1 of window 1
            click color well 1
        end tell
    end tell
    tell application "System Events"
        tell window "Text Color" of application process "Keynote"
            tell splitter group 1
                tell radio group "Pencils"
                    click radio button "Blueberry"
                end tell
            end tell
        end tell
    end tell
end tell

Any pointers would be greatly appreciated.

Was it helpful?

Solution

In Keynote, if I select some text and run the following example AppleScript code, the color of the text is set to Blueberry:

activate application "Keynote"
tell application "System Events"
    tell application process "Keynote"
        tell window 1
            tell radio group of toolbar 1
                if value of radio button 1 = {{0}} then
                    click radio button 1
                end if
            end tell
            click color well of scroll area 1
            click button 5 of toolbar 1
            click radio button 33 of radio group 1 of splitter group 1
            click button 1
        end tell
    end tell
end tell

If you want to use the pencil color name instead of the radio button number, then change:

click radio button 33 of radio group 1 of splitter group 1

To:

click (every radio button of radio group 1 of splitter group 1 whose description is "Blueberry")

      Hint: Mouse over and horizontal scroll to see full code.

You can then substitute any valid pencil color for a different color then "Blueberry".

Note: The example AppleScript code was tested and worked, as is, on my Mac running macOS High Sierra and Keynote version 8.1 (5683).


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

OTHER TIPS

Keynote has scripting support to change the colour (and size and font) of text, so you can make changes without relying upon UI scripting.

It uses an RGB model where you provide a 16-bit number for each colour. These values can be derived by multiplying the RGB value by 257. For example, Blueberry has values of {0, 0, 255}; multiply by 257 and you get the {0, 0, 65535} below. Conversely, if you divide each of the 'added text' colours of {25441, 10793, 42404} by 257, you end up with 99/42/165 (or #632AA5).

tell application "Keynote"
    tell slide 1 of document 1
        --Title
        set the color of object text of the default title item to {0, 0, 65535}
        set the font of object text of the default title item to "Impact"
        set size of the object text of default title item to 54

        --Body
        set the color of object text of the default body item to {8481, 32896, 16704}
        set the font of object text of the default body item to "Impact"
        set size of the object text of default body item to 36

        --Added text
        set color of the object text of text item 3 to {25441, 10793, 42404}
        set the font of object text of the text item 3 to "Impact"
        set size of the object text of text item 3 to 24
    end tell
end tell
Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top