In the previous chapter, I told you about system-wide automation technologies built into OS X. Although these technologies (particularly AppleScript and Automator) can also automate the actions of individual apps, there’s often a better—or, at least, more thorough—way of doing that within an app itself. That’s the topic of this chapter: using apps’ built-in automation capabilities.
Due to the breadth and depth of in-app automation features, I can only provide an overview, basic instructions, and a few examples. You’ll be able to accomplish some basic tasks and discover how to learn more.
I begin with Microsoft Office, partly because of its popularity and partly because of its extensive built-in programming language. I then move to Nisus Writer Pro, the very app I’m using to type these words, to illustrate a few different forms of automation that should be useful to anyone who works with words. Then I briefly discuss Google Apps Script, a macro language for Google Apps, and list the automation capabilities of several other popular apps.
Microsoft Office—which on the Mac comprises Word, Excel, PowerPoint, Outlook, and OneNote as its main components—is one of the world’s best-known software packages. Microsoft long ago added a programming language to the suite called Visual Basic for Applications (VBA), which enables users to write macros that automate Office apps, optionally embed those macros in documents, and run them (with some limitations) on either OS X or Windows. Microsoft removed VBA from Office 2008 for Mac, but brought it back again in Office 2011. Presumably, VBA will be in future versions too. (Microsoft Office also has excellent AppleScript support, which is an alternative way to accomplish many of the same tasks.)
What can you do with an Office macro? The sky’s the limit, but here are a few simple examples, any of which could be done with a single click or keystroke:
If you use Office extensively—and especially if you share documents with Windows users—it might be worth the effort to learn a bit of VBA since (unlike AppleScript) its macros work on both Windows PCs and Macs. But let me be frank: it’s not great for beginners. VBA was designed for programmers, not for ordinary users. It won’t do you any good beyond Office apps, and unlike AppleScript, VBA would never be called “English-like.” If you don’t know much about programming already, there’s a significant learning curve.
However, there’s a sneaky way to get your foot in the door—to write a VBA macro without knowing any VBA at all. Office lets you record macros—that is, turn on recording, do some stuff while Office watches, and then turn off recording. Office then attempts to make a VBA macro out of whatever you just did, which you can then replay at will. Sometimes these macros work fine as is; sometimes they require fiddling; and sometimes you’re out of luck.
So, my advice if you want to automate an Office app is to try recording a macro first. If that doesn’t work (and you can’t easily see how to fix it), move on to Automator. If Automator won’t do what you need either, try either AppleScript (if you need more control) or Keyboard Maestro (if you want a simpler interface). Write your own VBA macro from scratch only if no other tool does the trick.
Let’s walk through the process of recording and then playing back a simple macro. (I’ll use Word for this example, but the process is virtually identical in Excel and PowerPoint.)
Test
) and click OK.
First
, press Tab, type Second
, and press Return.Third
, press Tab, type Fourth
, and press Return.That’s it; you’ve recorded a macro. In theory, you can replay the exact actions you took again, at any time, in any document. So let’s try.
To play back your macro:
If everything is working correctly, your document will get two more table rows that look exactly like the two that were already there.
Curious to know what your macro looks like in VBA? Choose Tools > Macro > Macros, select your macro, and click Edit. You see something like Figure 21, along with other windows that you can ignore for now.
Beautiful, isn’t it? No, of course not, but if you look carefully, you can probably make out approximately what the commands do. If you were so inclined, you could edit the macro right here—for example, substitute different words in the Selection.TypeText Text:
lines. Then, to get out of the editor, choose File > Close and Return to Microsoft Word (Command-Q, just like Quit).
Going through Tools > Macro > Macros and a separate window whenever you want to run a macro is a drag. Fortunately, you don’t have to. You can assign a keyboard shortcut to your macro, put it on a menu, or both.
In Word or Excel (but not PowerPoint), you can assign a keyboard shortcut to your macro:
From now on, you can activate your macro with that keyboard shortcut.
Office apps don’t make it excessively easy to put your own macros on menus so you can run them with one click—but it can be done:
Normal.NewMacros.PastePlainText
. To give it a more readable name, click on the name of the menu (again, on that small toolbar) where you put the command, and then right-click (or Control-click) the command name and choose Properties from the contextual menu. Type a new name and click OK.Your new macro command should be on the menu (the real menu) where you placed it.
You can even add your own menu (and name it Macros
, for instance), by following the above procedure but selecting New Menu at the bottom of the Categories list and then dragging the New Menu command to the desired spot in the toolbar.
We’ve already been using VBA, but now I want to switch gears slightly to show you how to use a macro someone else wrote—for example, something you find on a Web page.
For this example, I’ll use a macro I wrote years ago to paste whatever’s on the clipboard as plain text, so that it assumes the style of the surrounding text. If you were to do this manually, the process would be: Choose Edit > Paste Special, select Unformatted Text, and click OK. A macro can reduce all that to one click or keystroke!
To use a macro someone else has written:
PastePlainText
) and click Create. Sub PastePlainText()
'
' PTT Macro
'
'
End Sub
Selection.PasteSpecial Link:=False, _
DataType:=wdPasteText, Placement:=wdInLine, _
DisplayAsIcon:=False
Sub PastePlainText()
'
' PTT Macro
'
'
Selection.PasteSpecial Link:=False, _
DataType:=wdPasteText, Placement:=wdInLine, _
DisplayAsIcon:=False
End Sub
Your macro is now ready to run. You can run it using Tools > Macro > Macros, assign a keyboard shortcut to your macro (such as Command-Shift-V) following the instructions in Assign a Keyboard Shortcut to a VBA Macro, or put it on a menu (see Add a VBA Macro to a Menu).
You should be able to turn up all sorts of VBA macros with a few Web searches. Here are a few resources to get you started:
To get help writing and editing VBA macros, try these sites:
Nisus Writer Pro is a powerful yet easy-to-use word processor. Wait, did I call it a word processor? That’s like saying El Capitan is a rock—it’s a bit of an understatement! Nisus Writer Pro is much more than a word processor; I like to think of it as a Programmable Everything Tool. I explain my history with Nisus (the product and the company) and why I’m so enamored of this app in my Macworld article Tools of the trade: Why I prefer Nisus Writer. For anyone who works with words, it’s an extraordinarily flexible tool—and capable enough that Take Control Books has left behind both Word and Pages and now creates ebooks (including this one) exclusively in Nisus Writer Pro. With the coupon at the end of this book, you can buy Nisus Writer Pro at a 20-percent discount.
One of the reasons I like Nisus Writer Pro so much is that it’s chock full of automation features that make my writing faster and more efficient. In this chapter I want to look at three of them: macros, multi-key shortcuts, and automatic numbers and cross-references.
As in other apps, macros in Nisus Writer Pro let you perform a single action, or a list of actions, with a menu command or keyboard shortcut. But Nisus Writer Pro macros can do much more than run sequences of commands; they can interact with files and folders on your Mac, ask for user input, make decisions based on complex logic, and access many capabilities in the app that don’t appear anywhere in the visible user interface. In other words, by writing macros, you can create entirely new features.
Nisus Writer Pro includes over 50 preinstalled macros on the Macro menu (or its submenus). Choose any macro name to run it. Some macros assume you have text selected first; if you try to run a macro and it won’t work in the current context, it’ll either beep or display an error message.
Here are a few you might try:
Take Control authors and editors have lots of specialized macros that aid in our workflow, such as:
If you’d like to find more macros you can install and run yourself, visit Nisus Software’s Nisus Writer Pro Macros forum. (That’s also a good place to find tips on writing your own macros.)
To make your own macro in Nisus Writer Pro, follow these steps:
prompt "Hello, world!"
Hello
) and choose a location. The ideal place to save it is ~/Library/Application Support/Nisus Writer Pro/Macros
, because that’s the default location for macros and any macros that you save there will automatically appear on the Macro menu; if you save it anywhere else, you’ll have to go through extra steps (which I don’t cover here). The macro appears on your Macro menu automatically.Now, to run your macro, choose its name from the Macro menu: Macro > Hello (or whatever you named it). You should see a little dialog with the text “Hello, world!” Click OK to dismiss it.
If you want to view or edit a macro that’s already in the Macro menu, the easiest way to do so is to hold down the Command key while choosing the macro from the menu—instead of running, it opens in a new window. You can edit it there; after you save it, choosing the macro name from the Macro menu in the normal way runs your updated version.
I gave you a one-line example macro, but what else can you put in a macro? I’m glad you asked. Let me begin with the easiest approach to writing your own macros.
First, the bad news: unlike Microsoft Office, Nisus Writer Pro has no recording capability—it can’t watch what you do and make a macro out of that for you. But now, the good news: it’s way easier to write macros for Nisus Writer Pro than for Word!
How easy? For the simplest things, like running menu commands, you just type a command (as it appears on a menu) on a line by itself. If the command includes an ellipsis (…), you can leave that off.
So, here’s a macro that turns the selected text bold, makes it 18 points, and then copies it to the clipboard:
bold
18
copy
And that’s a complete macro, by the way—no brackets, declarations, funky names, or obscure codes. Case doesn’t matter. (Spelling does matter!) Great, right?
A macro can do lots of things that aren’t merely menu commands, too. Want it to type the text “Hello, world!”? Do it like this:
type text "Hello world!"
Find all instances of the word Apple?
find all "Apple"
Set the line height to exactly 17 points?
set fixed line height 17
You can construct a macro with dozens or hundreds of commands like this, one after the other, and it will execute them all with a single click.
I gave find all "Apple"
as an example, but one of the most useful things you can do with a macro is automate more elaborate find-and-replace procedures—or a series of them. Nisus Writer Pro, like a few other apps mentioned in this book, lets you use a pattern-matching system known as regular expressions for finding and replacing text. (It can take those expressions even further by applying styles to portions of the expressions—a highly unusual feature.)
Any type of find or replace operation can go in a macro. For example, this macro line finds any sequence of two or more return characters and replaces it with one:
find and replace '\\n\\n+', '\\n', 'Ea'
This one finds any sequence of two capitalized words and underlines just the first one:
find and replace '([A-Z][a-z]+)( [A-Z][a-z]+)', '\\1\\2', 'Ea-iU'
It’s easy to make a macro that executes a series of simple commands, but you may want to do fancier things. You may want to use variables, arrays, objects, functions, loops, if/then/else conditionals, mathematical functions, string manipulation, and other sorts of things you’d normally find in a “real” programming language. All that, and much more, is well within the purview of Nisus Writer Pro macros too!
I’ve written many of these complex macros that involve serious programming, and while I can say confidently that it’s not nearly as bad as working in VBA or even AppleScript, it’s different. And the way you construct the commands and routines to do these nifty things is far from obvious.
Unfortunately, there’s not room here for me to get into the finer points of the language. You can get some guidance from the Nisus Writer Pro macro reference—choose Help > Macro Language Reference in Nisus Writer. That document contains all (well, nearly all) of the commands in the language, and a number of examples. But candidly, it wasn’t written for the layperson. The macro language itself isn’t unduly complicated, but the documentation makes it seem harder than it is. Someone ought to write a better guide. Maybe one day, somebody will.
In the meantime, I recommend the same thing for learning Nisus Writer Pro macros as I recommend elsewhere in this book for VBA and AppleScript: start with things other people have written (including the 50+ macros included with Nisus Writer Pro), try modifying them a little bit, and once you get the hang of that, start exploring new commands and features.
Nisus Writer Pro lets you assign a keyboard shortcut to any command, including macros you create yourself. That isn’t unusual. What is unusual, and extremely cool, is that you’re not limited to modifier keys and a single character (like Command-P or Command-Option-Shift-I). You can do all that, but you can also have keyboard shortcuts that are sequences of keys.
For example, there’s a menu command that capitalizes the first letter of each selected word: Edit > Convert > To Capitalized. You could assign Command-Control-Shift-C to it, but that’s hard to remember. What’s easy to remember is Command-C-A-P. That is, hold down Command while typing C, and then A, and then P. Cool, no?
Multi-key shortcuts are much easier to remember, because you can build in more mnemonic clues. How about:
I’m sure you get the idea. Here’s how you set up a shortcut:
All shortcuts must include Command, but you can optionally include Shift, Option, and/or Control key modifiers too. Any of these that you hold down while entering your shortcut are selected, but you can also select or deselect Shift, Option, or Control later to add or remove them as part of the shortcut.
Keyboard shortcuts are available immediately.
The final Nisus Writer Pro automation tool I want to mention is actually a combination of two features—automatic numbers and cross-references. I’ve used both dozens of times in this book. Let me tell you why they’re cool.
Nisus Writer Pro can automatically number almost anything that you like—pages, sections, lists (such as the seven steps just above), figure numbers (as seen throughout this book), tables, and the like. These numbers are variables that update themselves automatically. So, if I’m creating a list that has six items (numbered 1–6) and I press Return to create a seventh item, it’s numbered 7 automatically; if I insert, remove, or reorder part of the list, all the numbers update themselves. Similarly, if I use automatic numbering for figures, I can freely add, delete, or rearrange figures without ever worrying that the figure numbers in the captions will be out of order.
That part is useful but not terribly unusual; most word processors can do something of the sort. Where it gets cool is cross-references to the automatic numbers. For instance, I might have a graphic that’s labeled “Figure 12,” and near it I say, “see Figure 12.” I want that reference to update itself automatically if, later in the development of the book, that graphic turns out to be Figure 15 instead. So instead of just typing the reference, I insert a cross-reference to the text of that automatic number. As the figure number itself changes, so does the textual cross-reference!
A full explanation of how to use automatic numbering and cross-references in Nisus Writer Pro would take many pages, so I’ll walk you through just one example:
Figure
followed by a space. In the After Text field, type a colon (:
) followed by another space (Figure 23).
This is the caption.
See
and a space. Choose Insert > Cross Reference. Make sure the Insert Reference To pop-up menu says List Item, the Display Text pop-up menu says List Item Number, and the two checkboxes are deselected. Select Figure 2 in the list and click Insert.
Your text should now say “See Figure 2”.
You should see that the caption’s figure number updates itself, and the reference to that caption in the text updates itself to match!
You’ve seen that Microsoft Office and Nisus Writer Pro have built-in scripting languages, in addition to being controllable by AppleScript and external macro utilities. But you may be wondering: is that it? Are there no other Mac apps that have internal macro or automation features?
Indeed there are others! Here are a few prominent examples: