5 Tips for Effectively Recording Keystroke Macros

Keystroke macro recording has been around since the days of punch cards and Nixie tubes, yet somehow, it seems to be a well-kept secret. Quite simply, a macro is a sequence of events that is memorized once and then played back repeatedly, on-demand. When I was a kid I had a toy car that I could “program” by inserting a plastic disc that had notches around the edges (i.e. a cam). Depending on the depth of each notch, it would make the car either go left, straight, or right. In essence, each disc was a macro — a memorized sequence such as: straight, straight, left, straight, left, right, straight, right.

MS_Word_Macro_Menu.jpg

Most any type of editor program these days has keyboard macro recording capability. This is true for word processors such as MS Word, ASCII text editors such as TextPad, and graphics editors such as Photoshop. Unlike with my toy car, you don’t need an Xacto knife, a magnifying glass and steady hand to program your own keystroke macros. All you need to do is tell the editor to start recording, type away, and then tell it to stop recording. There are usually very few limits on the types of keystrokes that can be included in the macro. Not only can you use letters, numbers, and punctuation, but also control keys like Ctrl+V, function keys like Shift+F3, and navigation keys like Home and End. Between the control keys in the function keys, there are usually a myriad of editing functions that can be called upon in creative ways, such as capitalizing text, deleting the next word, deleting up to the next word, deleting to the end of line, and, of course, select, cut, copy, and paste.

Some of the most common uses for Macros are:

Delineation:

  • Adding a prefix and/or suffix to every line
  • “Commenting-out” lines
  • Adding separators between sections

Reformatting:

  • Swapping text/fixing a transposition
  • Applying formatting specifications to existing text
  • Removing extraneous line breaks
  • Realigning columns of text

Typing Shortcuts:

  • Entering frequently used text, such as company names and return addresses.
  • Typing in boilerplate text, where you then fill in the blanks (e.g. a macro that types “http://www..com”, and then leaves the cursor between the two dots, ready for you to enter the actual domain name).
  • Typing in special symbols and alphabets (e.g. a macro that types the word résumé, with the accents)

Note: I included this last section of typing shortcuts for completeness; however, there are often better ways to do these simple types of text expansion. In TextPad, the clip libraries are much better suited to this, and in MS Word you’ll want to look at using the auto-correct feature.

Tip #1: Assign Shortcut Keys to the most used macros — TextPad, for example, offers two ways to play back a macro. One is that you select the macro, by name, from the Macros drop-down menu. The other is that you assign a hot key to the macro. For this, you can use any key combination that isn’t already assigned (or even ones that have default assignments that you don’t use). For temporary macros that only need to live as long as the current project is active, I like to assign Alt+1, Alt+2, etc. For longer-lived macros, it’s a matter of finding a key combination that has meaning. For example, I use Ctrl+- (control dash) to insert a horizontal-rule separator between sections.

Tip #2: Start with the Intermission — One characteristic of keystroke macros is that they don’t usually know how to pause in the middle of a sequence and wait for user input. Typically, they just plow right through. So, what do you do if the sequence you are trying to memorize has a common beginning, and a common end, but the middle varies? One solution is to memorize two different macros, one for the beginning part, and one for the end part. Thus, you would play macro #1, type in the variable part for the first entry, play macro #2, play a macro #1, type in the variable part for the second entry, play a macro #2, etc.

Another solution is to use a single macro that picks up with the end of the first sequence and immediately continues with the start of the second sequence. When the macro stops playing you’ll be poised in the middle of the second sequence, ready a type in the variable part. Thus, you would play the macro, type in the variable part for the first entry, play the macro, type in the variable part for the second entry, etc. When you have finished typing in all of the entries, all that remains is to fix up the first and the last entries by moving the extra “part two” bit from before the first entry to after the last entry.

Tip #3: First Thing, Get to a Known Location — It’s always a good idea for the first keystroke or two of a macro to be ones that ensure the cursor starts out in a known location. For example, using the Home key or the End key to ensure that the cursor is either at the beginning of the line or the end of the line, respectively. For another example, the sequence of Home, Ctrl+Right, Ctrl+Right, will usually put the cursor at the beginning of the third word on the line.

Tip #4: End Up in a Known, Convenient Location — A good billiards player not only plans how to sink the shot, but also carefully considers where the cue ball will end up, setting up the table for the his next shot. You can do the same thing with your macro recordings. For example, say that you’re working on a task list and you want to add a prefix to each task to indicate its priority (”High: “, “Med: “, “Low: “). You can record three different macros for these, assigning them as Alt+1, Alt+2, and Alt+3. The Alt+1 macro would be programmed like this:

[Home] High: [Space] [Home] [Down]

In other words, start recording, hit the Home key, type in “High: “, hit the Home key again, hit the Down-arrow key, and stop recording. The first Home means that the cursor can be anywhere on the line to be prioritized when you hit Alt+1, but the “High: ” text will be inserted at the beginning of line regardless. The Home and Down at the end of the sequence “lines up the shot” for the next task to be prioritized. In this way, you can zip through a list by holding down the Alt key with one hand, and pressing either a 1, 2, or 3 with the other hand as appropriate. (The only time the flow would be interrupted is if a task takes up more than one line, in which case you would have to hit an extra down arrow or two before resuming.)

Tip #5: Pre-load the Find buffer, Then Record Find-Next — This is a variation of the previous tip. Say that you are converting a plain ASCII text document into a Word document. This document consists of eighteen chapters and the beginning of each chapter has a line that looks like “Chapter 2: Prerequisites”. In Word, you can write a macro that would delete the word “Chapter” and the following number and colon, and then change the rest of the line to be formatted with a style of “Heading 2″. Better yet, you can use the macro to help you find those chapter heading lines. First, open the find dialog (Ctrl+F) and search for the word “chapter”. If this doesn’t put you on the chapter heading line for chapter one, then hit Find Next, repeatedly, until you are there. Now, start recording the macro and perform the deletions and the style formatting, but don’t stop the recording quite yet. Click on Find Next first, and then finally stop the recording.

In this way, every time you execute the macro, it will automatically find the next occurrence of the word “chapter.” If this occurrence is in a chapter heading line, then you would simply play the macro again. If it is not, then you would click on Find Next instead, repeatedly, until you do arrive at the next chapter heading.

Note: When MS Word records Find Next in a macro, it specifically records the search pattern that is looking for at the time, and will always look for that exact same pattern every time the macro is played back. TextPad, on the other hand, merely records the fact that it needs to do a Find Next. So, when the TextPad macro is played back it will search for whatever was searched on last (i.e. whatever happens to be in the search buffer at playback time).

Trackbacks & Pings

Comments

  1. Great tips, as usual. I’d love to see some practical examples of macros, I’m never sure when to use them. The last time someone wanted a macro to print an excel sheet one line at a time, I ended up just creating a web app.

  2. thanks for the tips. i was grappling with this problem only yesterday and have still not figured out if it is possible to do what i want. maybe you could give me a quick pointer. for a quick summary of my request, please read the penultimate paragraph. i have included more details in the following paragraphs.

    essentially, i am scanning a few images into one openoffice (OO) file and movin backwards and forwards between OO and canoscan toolbox interface.

    my task is to go to the beginning of a page in a file and insert a picture (through a drop downmenu and select the acquiring of the image from a scanning source ) there through a scanning device. then we enter the scanning program, where i press scan. the image is scanned and i come back to the original OO file. then i press outside the boundary of the picture to return to a cursor a line below the picture file. i add a few more enters till i reach the beginning of a new page and the whole process is repeated

    i have recorded this macro in openoffice writer, which takes over from cursor at the line below an inserted picture. it adds the enter commands till the cursor reaches the start of a new page. then it calls the scanning program. then the macro ends.

    now i press scan in the scanning program and the scanned image is put at the top of the page. i press outside the image for the cursor to return to the line below the image.

    is there any way for me to have a macro which spans instructions over two separate programs, so that one press of a button would start a scan and insert the image at my chosen spot in the file.

    sorry about the long winded request.

  3. I am quite the noob to macros, but I telnet into numerous systems and after logging into each one, I have to type the environment conditions which is mind numbing. Is there a good way using a macro would simplify this whole situation? What would you recommend i read or site I could peruse for information?

  4. Meera: It’s tricky, but this might be possible using an external macro program such as AutoHotKey

    Douglas: Sounds like you need to write a bash startup script. If you’re an old time DOS user, it’s not that different from an autoexec.bat

  5. Douglas: If you don’t want your environment settings to activate every time you log in, but just when you are doing certain tasks, then you could put them in a separate script that you run on demand. In that case, be certain to run it using “. myscript” (with a space) as opposed to “./myscript” (with a slash). The former leaves the environment affected by the script, while the latter automatically resets the environment when the script is done (which is usually a good thing, but not in this case).

  6. mahalie: I agree that there’s a fine line between wasting time doing something by hand and wasting it to write a macro. I usually go by the volume of work to be done. If it will take me a lot of time to do it by hand, or more importantly, is possibly error prone to do it by hand, I’ll record a macro. My “a lot of time” is about 5 minutes. If it takes more than 5 minutes for me to do something by hand, I’ll jump to some more automated solution. Maybe I need to lower that now that I think about it…

  7. ooo…its nice…
    i use ProteMac KeyBag-nice too:)

  8. mmmm….ProteMac KeyBag good prog

  9. you can try to use ActyMac DutyWatch http://www.actymac.com/DutyWatch/.It’s tool for computer monitoring.I use it’s for record keystroke made by my kids



© 2006-2007 Maxim Software Corp.  All rights reserved.