Simulating keypressed on macOS

Purpose

This is what a guy wanted to simulate over Fiverr:
"While in Google Chrome, simulate 8 times the right arrow key, followed by 2 times the left arrow key, with a 1 sec delay between each keypressed, everything in an infinite loop"

AppleScript

This is very easy to do with AppleScript:

tell application "System Events"
  delay 0.5
  keystroke space using command down
  delay 0.5
  keystroke "chrome"
  delay 0.5
  keystroke return
  delay 1
  repeat
    repeat 8 times
      key code 124
      delay 1
    end repeat
    repeat 2 times
      key code 123
      delay 1
    end repeat
  end repeat
end tell

Bash

The same script, which can be run from the Terminal:

osascript -e 'tell application "System Events"' -e 'delay 0.5' -e 'keystroke space using command down' -e 'keystroke "chrome"' -e 'delay 0.5' -e 'keystroke return' -e 'delay 1' -e 'repeat' -e 'repeat 8 times' -e 'key code 124' -e 'delay 1' -e 'end repeat' -e 'repeat 2 times' -e 'key code 123' -e 'delay 1' -e 'end repeat' -e 'end repeat' -e 'end tell'

Credits