2

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.

1

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.

| improve this answer | |
  • Thank you that was perfect – naven87 Feb 8 at 18:10
  • Can this same code work for Numbers? The menu seems to have the same structure and it gets as far as selecting the Text radio button, but the click color well action doesn't open the color well so the next command fails. – naven87 Mar 2 at 21:51
  • @naven87, Assuming the target is selected text in a cell then here's the UI Scripting code reworked for Numbers: paste.ee/p/XZxzW – user3439894 Mar 2 at 23:16
  • Thank you again, how are you identify that it was scroll area 4 in the Paste link. I used the accessibility inpector tool, but couldn't see anything that indicated there was more than one scroll area. I even experimented with incrementing the scroll area index, but quit at 3 :( – naven87 Mar 3 at 2:41
  • @naven87,Using Accessibility Inspector and holding it over the color well I saw its description was "color well", and knowing its hierarchy was AXApplication > AXWindow:ACStandardWindow > AXScrollArea > AXColorWell I used the query, tell application "System Events" to get every UI element of every scroll area of window 1 of application process "Numbers" whose description = "color well" which returned {{}, {}, {}, {color well 1 of scroll area 4 of window "Untitled" of application process "Numbers" of application "System Events"}}, cont. ... – user3439894 Mar 3 at 3:06
0

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
| improve this answer | |
  • The issue is the text is already written and I'm trying to only colorize selected text within an existing text item. I couldn't see a way to set the color property for the currently selected text without automating the UI or going through some cut and paste gymnastics. – naven87 Mar 2 at 21:48
  • Makes sense. Apple seems to have issues with using applescript to work with selected text. – Mockman Mar 6 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .