How to make Automator insert “AW” into a Stickies Note

automator

I tried using "Watch Me Do" in Automator to insert text "AW" and "Return" in a Stickies note. It recorded the following (Events were "Type 'AW'" and "Press Return", the rest is the expansion into code):


— Type 'AW'

    delay 0.315949
    set timeoutSeconds to 2.000000
    set uiScript to "keystroke \"AW\""
    my doWithTimeout( uiScript, timeoutSeconds )

— Press Return

    delay 2.538645
    set timeoutSeconds to 2.000000
    set uiScript to "keystroke \"
    \" "
    my doWithTimeout( uiScript, timeoutSeconds )

    on doWithTimeout(uiScript, timeoutSeconds)
        set endDate to (current date) + timeoutSeconds
        repeat
            try
                run script "tell application \"System Events\"
    " & uiScript & "
    end tell"
                exit repeat
            on error errorMessage
                if ((current date) > endDate) then
                    error "Can not " & uiScript
                end if
            end try
        end repeat
    end doWithTimeout

In System Preferences, Security & Privacy, Privacy I allowed Automator.app and Stickies.app to control my computer.

I saved the above example as a service and tried executing the service in an empty Stickies note. Nothing happened.

I'm new at trying to use Automator. This is such a simple action that I believe that I have misunderstood something.

If I add to the workflow an existing Automator action that launches the Stickies application, then "AW" does get inserted, followed by "Return".

Question How to create a simple keyboard macro with Automator? shows a much simpler Applescript that could be adapted to doing what I describe (I've done such) but I'm frustrated that my simple "Watch Me Do" doesn't work. What am I overlooking or not understanding?

Best Answer

The Stickies app is not scriptable, meaning it does not directly support AppleScript via a Stickies AppleScript Dictionary, so most coding is going to use System Events and what's generally referred to as UI Scripting to manipulate the Stickies app.

The example code below, will do the following:

  • Open Stickies.app and, or if already open, bring it to the front.
  • Create a new Stickies note.
  • Move the new note to the top left corner of the screen and set its size to the default size.
  • Type "AW" and "return", with "return" simulating the Enter key being pressed.

tell application "Stickies"
    activate
    delay 1
end tell
tell application "System Events"
    keystroke "n" using command down
    set properties of window 1 of application process "Stickies" to {position:{0, 23}, size:{300, 200}}
    keystroke "AW" & return
end tell

This AppleScript code can be placed in a Run AppleScript action in an Automator Service workflow.


Note: One of the drawbacks of UI Scripting, is at times the needed use of the delay command to ensure the targeted UI Element is available to be acted upon. You may/will need to either adjust the value of any delay command and or add/remove additional delay commands as needed/wanted through any given UI Scripting scenario. The example code above, as is, worked on my system. If need be, make use of the delay command and adjust its value as/where needed/wanted.

Also the above example code does not employ any form of absolute error handling and is meant only to show how the individual events can be coded to achieve the objective. The onus is always upon the User to add/use appropriate error handling as needed/wanted.