0

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 09 '20 at 11:33

1 Answers1

-1

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

Alternatively:

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.

Article

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

Roxiun
  • 998
  • Whoever downvoted this answer could you please tell my why so that I could fix it? – Roxiun Jun 09 '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 09 '20 at 11:32
  • Which key does key code 76 represent? – nohillside Jun 09 '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 09 '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 09 '20 at 11:53
  • Roxiun, Not having Discord I cannot definitively answer that; however, in messaging app I'd imagine it would work. That said, key code 36 is what's typically used. Also, in addition to what nohillside said, let me add that answers should stand alone and not rely on links for missing information. You should answer the other part of the question too. – user3439894 Jun 09 '20 at 12:06
  • @user3439894 I have added info about using key code 36 vs key code 76. I have also added info about the other part. – Roxiun Jun 09 '20 at 12:41