0

I'm very not tech savvy and I just heard about AppleScript that can automate some simple typing for me. I just want to continuously type in a loop. Could someone please help me translate the instructions below into an AppleScript loop? I appreciate it so much!

click the right arrow key
delay 2 seconds
type "l"
delay 2 seconds
type "c"
delay 2 seconds
type "cool!"
delay 2 seconds
hit return
delay 2 seconds
hit escape
delay 2 seconds
repeat this whole thing

Can anybody help me with that? Further background information: I'm already in the program so I don't need this to open it up for me. I just need the script to immediately go to the right and then start typing.

Please help! So many thanks!

  • Those commands were supposed to be one per line! But everything is separated by "delay 2 seconds" in case you're reading this!! – James Mar 29 '17 at 4:59
  • 1
    Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. – grg Mar 29 '17 at 7:06
1

First of all, be careful with this kind of thing. System automation utilizing something like a virtual keyboard like this can lead to unfortunate side-effects if you end up sending key strokes to an unintended target.

However, here:

repeat
    tell application "TextEdit"
        activate
        tell application "System Events" to key code 124
        delay 2
        tell application "System Events" to keystroke "l"
        delay 2
        tell application "System Events" to keystroke "c"
        delay 2
        tell application "System Events" to keystroke "cool!"
        delay 2
        tell application "System Events" to keystroke return
        delay 2
        tell application "System Events" to key code 53
        delay 2
    end tell
end repeat

Let's go through this code:

First, we are doing a simple repeat without end. This is not a good idea, but you asked for it and that's what you get. If you wanted just a finite number of times, you could add # times after repeat. Ex: repeat 3 times. There are many other methodologies to repeat beyond this answer.

Then you are going to create a tell block and activate the application you are sending these commands to. If you do not do this, you will be sending keystrokes willy nilly to whatever happens to be the active application at the time.

You will then do a series of having the "System Events" application emulate a keystroke and delaying for a count. delay accepts decimals as well as integers for number of seconds. Note that for the Right Arrow and Escape I used key code 124 and key code 53 respectively. The list of key codes can be found in various locations online with a Google search for 'applescript key codes'. Here is a current one at the time this answer was written: http://eastmanreference.com/complete-list-of-applescript-key-codes/

Finally, you will end the tell block, no longer sending AppleScript to "TextEdit" and then you will end your repeat block.

  • you can generate SE's code blocking by selecting your text & ctrl/k - it's in the Help at the top of the answer space. – Tetsujin Mar 29 '17 at 6:48
  • I appreciate this so much and it's incredibly helpful. However, when trying this, the commands do not happen within the program I want it to happen in. If I am in program 'ABC', how do I ensure this query happens in 'ABC'? Does that question make sense? – James Mar 29 '17 at 13:32
  • (Thank's Tetsujin, I'll keep that in mind) At this point, I've already gone above and beyond. I'm going to have to ask you to do a little bit of research in AppleScripting, James. Suffice it to say, you'll like tell the application you are trying to type in rather than TextEdit as in my example. – EricDAltman Mar 30 '17 at 4:19
0

Without the whole script (and the context behind this) it's a bit hard to be sure about an answer - especially an efficient one.

Nevertheless, below is something that may help (although I'm not in front of a computer right now that I can test it with):

tell application "System Events" to keystroke (key code 124) -- right
delay 2
tell application "System Events" to keystroke "1"
delay 2
tell application "System Events" to keystroke "c"
delay 2
tell application "System Events" to keystroke "cool!"
delay 2
-- simulate pressing the Return/Enter key
tell application "System Events" to keystroke (key code 36)
delay 2
-- simulate pressing the ESC key
tell application "System Events" to keystroke (key code 53)
delay 2

Now, depending on you overall script you may not need all the tell application "System Events" to lines.

Also, every key has a character, an ASCII code, a hexadecimal code and a decimal code. I've tried to comment the above script to explain what the key codes equate to (e.g. key code 53 refers to the decimal code for the ESC key).

Let me know how you go.

  • I appreciate this so much and it's incredibly helpful. However, when trying this, the commands do not happen within the program I want it to happen in. If I am in program 'ABC', how do I ensure this query happens in 'ABC'? Does that question make sense? – James Mar 29 '17 at 13:34

You must log in to answer this question.

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