0

I've been trying to do my own AppleScript to make my Mac press C 13 times, press the left arrow key, press C 13 times, press the right arrow key and start from the beginning repeatedly at 1.7 seconds intervals.

Currently I have only managed to do a working script that makes it press C multiple times. Could someone please help me with this? This is what I have at the moment

set i to 0
repeat while i < 1.0E+300
    set i to i + 1
    delay 1.7
    tell application "System Events" to keystroke "c"
end repeat
1

To start off, the number 1.0E+300 is a really REALLY big number. That's more than the number of atoms in the entire universe by several orders.

Your script isn't far off. To press "c" thirteen times, you can either do this:

repeat 13 times
    tell application "System Events" to keystroke "c"
    -- delay 0.1
end repeat

(you potentially need a small delay there to register individual keystrokes, but you can try it without and see which works);

or you can do this:

tell application "System Events" to keystroke "ccccccccccccc"

which is the equivalent of the repeat loop without the delay.

The left and right arrow keys are key code 123 and key code 124, respectively. So, adding those in between the keystrokes:

tell application "System Events"
    keystroke "ccccccccccccc"
    delay 0.1
    key code 123 -- left arrow
    delay 0.1
    keystroke "ccccccccccccc"
    delay 0.1
    key code 124 -- right arrow
end tell

OR:

tell application "System Events"
    repeat 13 times
        keystroke "c"
        delay 0.1
    end repeat

    key code 123 -- left arrow
    delay 0.1

    repeat 13 times
        keystroke "c"
        delay 0.1
    end repeat

    key code 124 -- right arrow
end tell

Finally, doing this ad infinitum on a loop at 1.7 second intervals, will produce something resembling this:

tell application "System Events" to repeat
    repeat 13 times
        keystroke "c"
        delay 0.1
    end repeat

    key code 123 -- left arrow
    delay 0.1

    repeat 13 times
        keystroke "c"
        delay 0.1
    end repeat

    key code 124 -- right arrow

    delay 1.7
end repeat

Note that the outside-most repeat loop has no while, until, or times to limit its continuation. It will loop forever, until you stop the script manually.

I also didn't factor the 0.1-second delays into the overall 1.7-second delay. The smaller delays will exceed 1.7 seconds in total, but I'll let you adjust the timings to your needs.

  • thanks alot! the script works but I found it wasn't able to achieve what I wanted it to. how can I edit the script so I can hold down both arrow keys for 1.5 seconds each time? – kiara Aug 18 '18 at 14:47
  • I don't believe that's possible, @kiara. – CJK Aug 18 '18 at 15:14

You must log in to answer this question.

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