Using AppleScript to change text color in Keynote

applescriptkeynote

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.

Best Answer

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.