AppleScript – Change Text Color and Strikethrough in Notes

applescriptautomator

I have an Automator Quick Action routine that changes the font of selected text to Strikethrough. Is it possible to also change the colour of the selected text to Blue please?

on run {input, parameters}

    tell application "Notes" to activate

    tell application "System Events"
        click menu item "Strikethrough" of menu of menu item "Font" of menu "Format" of menu bar 1 of application process "Notes" of application "System Events"
    end tell

return input
end run

Best Answer

To automate changing the color of selected text in Notes it can be done with UI Scripting, which you are already using to click Strikethrough on the menu.

Since you did not specify a color or which tab on the Colors window you want to work with, I'll show two examples, one for the Pencils tab and the other for the Color Palettes tab.

With the text already selected in Notes, the following example AppleScript code works for me in macOS Catalina, using US English for the Language:

For the Pencils tab:

tell application "Notes" to activate

delay 0.25

tell application "System Events"

    --  # Click the Strikethrough menu.

    click menu item "Strikethrough" of ¬
        menu "Font" of ¬
        menu item "Font" of ¬
        menu "Format" of ¬
        menu bar item "Format" of ¬
        menu bar 1 of ¬
        application process "Notes"

    --  # Show the Colors window, if not visible.

    if not (exists window "Colors" of application process "Notes") then
        keystroke "c" using {shift down, command down}
        repeat until (exists window "Colors" of application process "Notes")
            delay 0.01
        end repeat
    end if

    --  # Change the color of the selected text.

    tell application process "Notes"
        tell window "Colors"
            --  # Click the Pencils tab.
            click (every button of toolbar 1 whose description is "Pencils")
            --  # 
            --  # Valid colors for the Pencils tab are:
            --  # 
            --  # "Licorice", "Lead", "Tungsten", "Iron", "Steel", "Tin", "Nickel", "Aluminum", "Magnesium", "Silver", "Mercury", "Snow",
            --  # "Cayenne", "Mocha", "Asparagus", "Fern", "Clover", "Moss", "Teal", "Ocean", Midnight", "Eggplant", "Plum", "Maroon", 
            --  # "Maraschino", "Tangerine", "Lemon", "Lime", "Spring", "Sea Foam", "Turquoise", "Aqua", "Blueberry", "Grape", "Magenta", "Strawberry",
            --  # "Salmon", "Cantaloupe", "Banana", "Honeydew", "Flora", "Spindrift", "Ice", "Sky", "Orchid", "Lavender", "Bubblegum", "Carnation"
            --  # 
            set myColor to "Tangerine"
            click (every radio button of ¬
                radio group 1 of ¬
                splitter group 1 whose description is myColor)
            click button 1 --   # Close the Colors window.          
        end tell
    end tell

end tell


For the Color Palettes tab, use the following block of example AppleScript code in place of the same tell application process "Notes" block of code in the code shown above:

--  # Change the color of the selected text.

tell application process "Notes"
    tell window "Colors"
        --  # Click the Color Palettes tab.
        click (every button of toolbar 1 whose description is "Color Palettes")
        --  #       
        --  # Valid colors for the Color Palettes tab are:
        --  # 
        --  # "Black", "Blue", "Brown", "Cyan", "Green", "Magenta", "Orange", "Purple", "Red", "Yellow", "White"
        --  # 
        set myColor to "Red"
        try
            select (every row of ¬
                table 1 of ¬
                scroll area 1 of ¬
                splitter group 1 ¬
                    whose value of text field 1 is myColor)
        end try
        click button 1 --   # Close the Colors window.          
    end tell
end tell

Note: The example AppleScript code is just that and, sans the included try statement in the code for the Color Palettes tab, does not contain any additional 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.