4

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
0

2 Answers 2

3

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.

0
0

In Notes, under the format menu, there is a font menu item and under this is an option to show colors. Opening this and clicking on a color will change the font to the selected color.

So a direct method isn't there. If the complexity is worth it:

  • use cliclick. Brew has it.

  • Find out the coordinates of the color you want to use, using command+shift+4.

Add this part in the automator:

  • Using automator, activate the color panel. Format menu > Font > show colors.

  • Emulate a click on the coordinates using cliclick using "do shell script"

8
  • I think they want it in automator, not via GUI
    – anki
    May 19, 2020 at 15:28
  • you are right but it sounded to me like OP wasn't aware of how to change the font color so I used that as my answer. Once he knows how to do it OP can write the script. But if you think I should delete my answer i will. just say the word. Thanks.
    – Natsfan
    May 19, 2020 at 15:43
  • Oh I didnt downvote it. Since the color button isn't in the menus, I don't think automator action is possible easily. .
    – anki
    May 19, 2020 at 16:16
  • Thanks guys, I'm not really bothered if it runs in Automator or as a stand alone script but need help with the actual scripting. It seems to me that the colour menu doesn't always appear in the same place, so not sure how the GUI will work?
    – Niscars
    May 19, 2020 at 20:50
  • @ankii, While cliclick is a nice utility, it's not well suited for what the OP wants to do, as it is much easier to use UI Scripting in the use case. There would be a considerable amount of coding to get cliclick to do the same thing I did with UI Scripting in the answer I posted. May 20, 2020 at 2:14

You must log in to answer this question.

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