Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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!)

share|improve this answer
up vote 3 down vote accepted

I found a way to do this in a bash script:

# A simple script that simulates hitting the key specified on OS X
# Based on http://stackoverflow.com/a/9418346/363078

echo "tell application \"System Events\" to keystroke \"$1\"" | osascript

Save as hitkey, chmod +x hitkey, hitkey k to hit K.

From there it's simple to use Automator to loop over the script several times or make an iCal alarm to initiate the script at a specific time.

Automator document types

share|improve this answer
Doesn't that miss the whole "repeat every 5 seconds for a total of 15 times" part? –  Daniel Sep 13 '12 at 16:35
I didn't know about the echo "script" | osascript syntax - I've always used osascript -e "script". So you get +1 for that. –  CajunLuke Sep 13 '12 at 16:53
@DanielLawson: yes, but having the shell script it's simple to loop it however you want. –  houbysoft Sep 13 '12 at 17:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.