Automate sequence of key strokes in Mac

62,216

Solution 1

To do this, I created a automator workflow with a Run AppleScript object with this content:

on run {input, parameters}

tell application "Google Chrome" to activate

tell application "System Events"
    keystroke "A"
    keystroke "B"
    keystroke "C"
end tell

return input
 end run

This worked fine for me

Solution 2

In addition to writing your own script, Keyboard Maestro can be used to compose key sequences like this.

Solution 3

You could create a macro with an application like Keyboard Maestro, iKey or QuicKeys. Many of them also support something like the quick macros in Keyboard Maestro. You can press ⌃F1 to start or stop recording a macro, and then play it back with ⌥F1.

You can also emulate keypresses with AppleScript. The delay at the start is not needed if the script is run with FastScripts.

delay 0.5 -- if the script is run with a shortcut that has modifier keys
activate application "TextEdit"
tell application "System Events"
    keystroke "aa"
    key code 123 using {shift down, command down}
end tell

The keystroke command can only be used to insert characters that are included in the current keyboard layout. If the text is long enough, there's also a visible delay when it's inserted.

Another way to insert text is to use the clipboard:

set the clipboard to "aa"
delay 0.05
tell application "System Events" to keystroke "v" using command down

Solution 4

I've authored a tool called sendkeys which can be used to send keystrokes and mouse events to an application.

Example, the following script will activate Google Chrome, open a new tab and navigate to Google:

sendkeys -a 'Google Chrome' -c '<c:t:command>www.google.com<c:enter>'

It can be installed with brew:

brew install socsieng/tap/sendkeys
Share:
62,216

Related videos on Youtube

Daniel Cukier
Author by

Daniel Cukier

Daniel is a technology innovator, currently exploring web3 projects. Former CTO in Brazilian startups such as Pravaler - a fintech that offers accessible student loans - also founder and CTO at Playax - an audience development platform for music professionals based on BigData - he also worked for two years as CTO at Elo7 – the biggest crafts marketplace in Brazil. Experienced working in different programming languages such as Elixir, Ruby, JavaScript and Java, Daniel helped many startups as venture advisor at Monashees Capital and other accelerator programs in Brazil. He is also PhD in Computer Science at University of São Paulo – IME-USP. His PhD research is about Software Startups Ecosystems and Entrepreneurship. Daniel mastered in Computer Science in University of São Paulo in 2009, with the Thesis Patterns for Introducing New Ideas in the Software Industry. Daniel is a Cloud Computing GDE (Google Developer Expert). Daniel started developing software in Brazil when he was 10, on his TK-3000 Basic 2MB RAM computer. He worked as a consultant and software developer in many companies. In 2001, he worked for an Internet startup in Italy. In 2006 he joined Locaweb, the biggest web hosting company in Brazil and worked there for 5 years as developer and tech lead in infrastructure team. Daniel is an active member in the agile and software development communities, speaker in many conferences such as Elixir Brasil, QCON, Agile Brasil, TDC, DevCamp, Agile Trends and others. Studying other Arts beside software development, like Theatre, musical instruments and compositions, dance and writing, he acted in five musical plays and has a poetry book published. Daniel is a Vipassana meditation student and is very interested in topics related to human consciousness.

Updated on September 18, 2022

Comments

  • Daniel Cukier
    Daniel Cukier over 1 year

    I want to automate a long sequence of keystrokes that I have to type every time I use a specific web site. How can I do it in Mac OS X. I've tried the Automator. I record the sequence, but when I run it I got this error:

    The action “Watch Me Do” encountered an error.

    Check the actionʼs properties and try running the workflow again.

  • Eneko Alonso
    Eneko Alonso almost 8 years
    On OS X El Capitan I got an error when including the first and last two lines. Removing them made the script work fine.
  • JESii
    JESii over 7 years
    I've used Keyboard Maestro -- excellent product and great support!