Hi, this is Gary with MacMost.com. Today let me show you how to repeat a series of key
presses on your Mac using only a single click or keyboard shortcut.
MacMost is supported by more than 500 viewers just like you. Go to MacMost.com/patreon to
read more about it. Join us and get exclusive content.
So let's say you have to repeat a series of keystrokes all the time in order to accomplish
a task. It would be great to have a shortcut that goes through that sequence rather than
having to do all those key presses. Well, you can do this many different ways on your
Mac. Here are three methods of repeating a sequence of keys. The example I'm going to
use here is switching two lines. So here I am in TextEdit and let's say I want to switch
this line and this line. Now I could do that by selecting the line any number of ways.
A triple click will do it or I can drag and slide from the beginning. I could Command
X to cut , click on the beginning of the next line and Command B to paste. Or I could just
user keyboard shortcuts for it.
So let's place the cursor in the middle of the line in a random spot. I could Command
left arrow to go to the beginning of the line. Shift Command right arrow to select everything
to the end of the line. Then a single Shift right arrow to select one more character.
The carriage return or line feed at the end there. So now I have the entire line selected.
Then I could Command X to cut. The down arrow to move down one line. Command V to paste.
For good measure the up arrow to go to the beginning of the line we just pasted. So I
did the entire thing with a short series of keyboard commands.
We could do that series of keyboard commands with just one action and we could do it several
ways. First let's start off by doing it in Automator using Watch Me Do. So in Automator
here I'm going to create a new document that's a Quick Action. I'm going to then go ahead
and set it to Receives No Input. Now I'm going to use the Record button here to record Watch
Me Do. But first let me set things up in this file here. I'm going to put the cursor in
some random spot in the middle of a line so it's ready to simulate what I want to do.
Then I'll go back here and I'll hit Record and now Automator is recording.
Now I'll click on TextEdit to activate it. Then I'm going to repeat my keyboard sequence.
So Command left arrow, Shift Command right arrow, Shift right arrow, Command X, down,
Command V, up. Then I'll press the button here to stop. Now in Automator I've got this
Watch Me Do action. It has a series here of events. Let me go to the very first one here
which is bringing the window for the TextEdit document to the front. I don't want that one
there because presumably I'm already there at that position and I'm probably not using
this particular document anyway. So I'm going to select that and hit the Delete key to delete
it.
Now I've got this series of keyboard commands here that fit what I was doing before. Now
I want to set this so the playback speed is maximized because I want it to happen quickly
with no timeout or anything. So I'm going to save it like that. Command S to save. I'm
going to call this Switch Lines 1 and save it as a Quick Action. So now in TextEdit I
can go to TextEdit Services and then switch lines one. It's going to work for me. It may
not work for you because you have to give it permissions. You could see here it's doing
each step kind of slowly. So it's not pretty quick. So this is not going to be the best
way to do it. If you get an alert there asking for permission what you need to do is go to
System Preferences and then go to Security & Privacy. Then go to Accessibility. Unlock
here at the bottom and make sure Automator is in here and checked. It may differ for
you. One time I had it at Automator. Another time I tried it I actually had to add the
script to this menu by dragging and dropping it in. So follow the instructions in the Alert
carefully to enable this script to run as a Quick Action.
So let's try doing something else in Automator but instead of using Watch Me Do we'll use
a script. I'm going to create a new document in Automator. I'm going to make that a Quick
Action as well. I'm going to set that to No Input. I'm going to use JavaScript here. You
can do a similar thing in AppleScript but I prefer using JavaScript because it's more
modern. I'm going to paste the code in here and then explain it to you.
The first line is just going to set a variable SE to Application system events. We need to
be able to send these keyboard strokes to system events in order to simulate the key
presses. Then we use se.keyCode to send key codes and se.keystrokes to send keystrokes.
The difference is key codes are numbers and we can use those for things like arrow keys.
Keystrokes are letters and we can use those for the regular keys on the keyboard. So se.keyCode123
is the left arrow. We're going to send it with command down by using curly brackets
using colon, square brackets, and then in single quotes command space down. Then 124
is the right arrow with command down and shift down. Then the right arrow with just shift
down. Then we'll send a keystroke x with command down. So command x. Then the keystroke 125,
the down arrow, then the letter v with command down. Then we'll go up and we'll just go up
one line. Just so it's the beginning of the line that was just pasted. That simulates
exactly what we were doing with Watch Me Do but it's going to do it a little quicker.
So we're going to Save this as Switch Lines 2.
Now we're going to hide Automator. Put the cursor right there. Go to TextEdit Services
and select Switch Lines 2. You can see it does it and quite a bit faster than using
Watch Me Do.
Now left me show you a way to do it that doesn't use Automator at all. You can use something
called the Accessibility Keyboard. Go to System Preferences and then to Accessibility and
then to Keyboard and click on Accessibility Keyboard here at the top. Click on Enable
Accessibility Keyboard and you get this keyboard here that you can use to type characters by
clicking on letters with your cursor. But you could also go to Panel Editor here and
Panel Editor brings up these different panels and you can create your own.
So I'm going to click Add Panel and create an empty keyboard. So it's this blank space
here. Close this to get it out of the way. You've got this blank space here and I'll
click Add Button. Then I've got this button here in the blank space. I'm going to name
that Switch Lines and I can set other things for how the button looks. Then i can set an
Action for this button. One of the actions I can do is Press Keys. So I do that and now
I get a sequence and I can hit Record. So let me try that. I'm going to hit Record.
Then I'm going to do the sequence just as we had before. So Command left arrow. Then
Shift Command right arrow. Shift right arrow. Then Command x. Down. Command v. and Up. Then
I'll Stop. That sequence is now assigned to this button. So how does this work?
I'm going to go and close and then I'm going to click Enable Accessibility Keyboard again.
If I had it still running up I'd have to relaunch it in order for it to take effect. I'll click
here and under Custom Panels, Empty, that one I created. I could have named it something.
It just has that one button in it. Now I can have this one button here. If I put the cursor
there and I hit Switch Lines it should execute all those keys. You could see it does it and
it does it lightning fast. So this allows me to lightning fast switch lines using this
button here. Of course I can add more buttons as well to do other keyboard combinations.
Now one advantage the Automator scripts have over this button here is I have to click on
this button with the cursor. There's no way to assign a keyboard shortcut to it. The whole
idea of these Accessibility Keyboards is to have something on the screen that you can
interact with and not have to use the keyboard. But these Services here, these Quick Actions,
I can have keyboard shortcuts for them by going to System Preferences and then Keyboard
and then Shortcuts and then if I go to App Shortcuts and hit the Plus button here I could
set a keyboard shortcut for all applications. I have to use the exact name of the menu item.
Switch Lines 2 and create a keyboard shortcut like Command Option t and Add. Now I can use
Command Option t. See if I actually go to TextEdit Services and Command Option t is
now the shortcut for Switch Lines 2. I can use that here.
Now you can use these methods for a variety of different key sequences. This is just one
example. So play around with this and think about how you can use this technique to improve
your productivity on your Mac.