MacOS – create a script in script editor that will press a key and then press enter every 60 seconds

applescriptmacmacosmojavescript

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.

Best Answer

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