macOS Software – Simulate Key Presses at a Certain Rate

keyboardmacossoftware-recommendation

Is there a way or a 3rd party application which would let me specify, for example, that I want it to send a keyboard event (so that it has the same effect as physically hitting the key on the keyboard) every 5 seconds?

So, for example, I could tell it to hit K every 5 seconds, 15 times in total?

Basically, I'm looking for something like AutoHotkey but for OS X.

Best Answer

Here's an AppleScript to do what you want:

set i to 0
repeat while i < 15
    set i to i + 1
    delay 5
    tell application "System Events" to keystroke "k"
end repeat

You can inline it in a shell script like this:

echo "set i to 0
repeat while i < 15
set i to i + 1
delay 5
tell application \"System Events\" to keystroke \"k\"
end repeat" | osascript

(Thanks to @houbysoft for the echo "script" | osacript syntax!)