Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 5 down vote accepted

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

# Simulates hitting a key on OS X

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

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

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.