user3381652
user3381652

Reputation: 1

Delay in an Alfred 2, using Automator and Apple Script to open "Stickies" and create a new note

Basically my goal is to code a key command (option-s) to activate Stickies and create a new note. Right now I have an Alfred 2 generated Automation which links the hot key to the following script:

on alfred_script(q)
    tell application "Stickies" to activate
    delay .2
    tell application "Stickies" to activate
    delay .01
    tell application "System Events"
        keystroke "n" using command down
    end tell
end alfred_script

The two activate commands are my attempt to deal with a bug where it opens the application, but doesn't bring it to front. It works seamlessly when the application is open in the background, but it's slow and creates a screen flash when the application isn't already running. The delay is not coming from the application itself because I can open the application and hit command-n as fast as possible, and it always works.

(By the way if you have an idea for how I could hide all other notes and just show the new one, that would be awesome!)

Upvotes: 0

Views: 747

Answers (2)

Lri
Lri

Reputation: 27603

Try this:

launch application "Stickies"
tell application "System Events" to tell process "Stickies"
    click menu item "New Note" of menu "File" of menu bar 1
    set frontmost to true
end tell

If you run the script by pressing option-s, there might not be enough time to release option before keystroke "n" using command down.

Or this doesn't raise the windows for other notes:

launch application "Stickies"
tell application "System Events" to tell process "Stickies"
    click menu item "New Note" of menu "File" of menu bar 1
end tell
do shell script "open -a Stickies"

activate app "Appname" and set frontmost of "Appname" to true raise all windows, but do shell script "open -a Appname" raises only one window.

Hotkeys also have a short delay by default in Alfred, but you can reduce it by changing the trigger behavior:

Upvotes: 2

adamh
adamh

Reputation: 3292

You could try this alternate way, might have a different effect.

tell application "System Events"
    tell process "Stickies"
        set frontmost to true
        keystroke "n" using command down
        keystroke "Hello World" & linefeed & "I'm a new note!"
    end tell
end tell

Hiding all other notes, i'd say start a new question for that.

Upvotes: 0

Related Questions