I want to create a script in script editor that will press a key and then press enter every 60 seconds. However, I don't know how to make a script that presses a key. Does anybody know how to make a script that does this? I tried to keystroke "enter" but what is did was type out the word enter. Also, sometimes the code will crash and an error message will pop up saying that script editor cannot do keystrokes.

The aim for this script is to create an xp farming discord bot to farm xp on a server by sending messages.

  • Does the key press need to get sent to a specific window/application? How do you want to get out of the loop? – nohillside Jun 9 '20 at 11:33

How to send enter key using Applescript

In order to send the enter key in applescript you can use:

tell application "System Events"
  key code 36
end tell


tell application "System Events"
  key code 76
end tell

The Difference between the two:

As shown here key code 36 is used to send the key Return.

key code 76 on the other hand is used to send Enter.

(@user3439894 Pointed out that key code 36 is typically used.)

Fixing Applescript not being able to send keystrokes

Go to System Preferences --> Security & Privacy --> Accessibility. From there delete the application and add it again.


More Info

Sending Keystrokes: How to send special keys Applescript, How do I automate a key press in AppleScript? and List of Apple Key Codes

Fixing applescript not being able to send keystrokes: Application is not allowed to send keystrokes

  • Whoever downvoted this answer could you please tell my why so that I could fix it? – Roxiun Jun 9 '20 at 11:27
  • I assume you got the downvote on the first version of the answer which was a bit thin on information, something you've addressed in your edit. – nohillside Jun 9 '20 at 11:32
  • Which key does key code 76 represent? – nohillside Jun 9 '20 at 11:33
  • @nohillside, key code 76 is the enter key on the numeric keypad, if it exist. For practical purposes key code 36 should be used. – user3439894 Jun 9 '20 at 11:46
  • @user3439894 Does it make a difference in discord? One of the links I posted did say that either 36 or 76 could be used. I'm adding 36 to the answer so the OP will know either can be used. – Roxiun Jun 9 '20 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .