March 30th, 2010 • 3:11 pm
Today, I decided I was tired of waiting for Apple to add to Pages ’09—which is my main word processor these days—the functionality that I need to be really productive with it and which I have been living without for the past few years.
One of my main issues with Pages is the lack of customizability with keyboard shortcuts. In Pages ’09, you have the following options
- You can assign custom keyboard shortcuts to any menu command using Mac OS X’s system-wide keyboard shortcuts feature (in System Preferences).
- You can assign the eight “shortcuts” (more accurately keys) F1 to F8 to specific paragraph, character or list styles in the styles drawer.
This is far from sufficient, for several reasons. First of all, the vast majority of Pages’s functionality is available exclusively through the inspector palettes and not via any menu in the Pages menu bar. There is no option to assign keyboard shortcuts to commands or settings that are only accessible via these inspectors.
There are also significant issues with the way shortcuts are stored, which make it easy to lose them during system upgrades/reinstalls.
As for styles, I have way more than eight styles that I would like to have keyboard shortcuts for, and the F1–F8 shortcuts are not particularly convenient, because the keys themselves are small with hard-to-read labels (on the standard Apple aluminum keyboards and on the laptop keyboards). They are also difficult to memorize, and too easy to hit accidentally.
The initial version of Pages didn’t have any AppleScript support, so there was no option to explore at the time. You had to use it or leave it.
Since then, however, Apple has added AppleScript support to iWork.
The problem for me is that I am not a developer and I have limited experience with AppleScript. Even though I used to be a amateur developer and did quite a lot of programming (in BASIC, Pascal, and then assembly language) in the 1980s (on a Sharp MZ80K and then on a Commodore 64—those were the days) and then in HyperTalk in Apple’s HyperCard on the Mac in the 1990s, I never really found the time or inclination to devote a lot of resources to AppleScript.
For a long time, Microsoft Word was my main word processor on the Mac and so I tried to use its own languages. First it was WordBasic. Then VisualBasic for Applications. I always found these scripting languages pretty atrocious, though. And then I got sick of Word itself, with or without customizations, and embraced Pages instead. (And then Microsoft dropped VBA support altogether, which made me not regret my decision one bit.)
But AppleScript… Well, I just don’t seem to be able to get my mind around it. Every time I want to try a little something, I take a look at the application’s dictionary, then I try to write something, and I fail at the first hurdle, because I am unable to do the most basic stuff properly.
And so it proved again today when I tried to write my first customization script for Pages ’09. I wanted a simple script to toggle the “Keep with following paragraph” option on and off in the “Text” inspector. This is one of those options that I use all the time and is only accessible via an inspector. So it’s impossible to assign a keyboard shortcut to it in System Preferences.
It seemed fairly simple: Just write a script that tests the current value of the “Keep with following paragraph” option for the selection and changes it to the other one.
Through my exploration of the AppleScript dictionary for Pages, I was able to figure out that this was done with a boolean property called
keep with next paragraph. But the problem was, as I knew it would be, how to refer to the current selection. That’s the most fundamental thing, but the most fundamental thing is pretty much impossible to figure out just by looking at the dictionary.
So I ended up trying all kinds of things. I managed to get this working:
tell application "Pages" get properties of selection end tell
The result would indeed be the list of the values of all the properties, including the value of the
keep with next paragraph one, for the current selection.
But then, I couldn’t figure out why the following wouldn’t work:
tell application "Pages" set keep with next paragraph of selection to true end tell
I kept getting this very helpful error message:
Pages got an error: Can’t set keep with next paragraph of selection to true.
How is one supposed to understand why “
selection” as a reference is good enough to retrieve the values of the properties, but not good enough to set the value of one of the properties?
I tried all kinds of things, to no avail. I tried to find examples of AppleScripts scripts for Pages ’09 with Google and could only find a few scripts that had nothing to do with what I was looking for.
And then I found a bunch of sample scripts provided by a French Mac user named Yvan Koenig. I downloaded a few of the most promising ones and finally figured out, based on some of this scripts, that I could get things working by referring to the selection with the phrase “
get selection of document 1.”
Obvious, isn’t it?
So I ended up writing the following script:
tell application "Pages" set mySel to (get selection of document 1) if keep with next paragraph of mySel is false then set keep with next paragraph of mySel to true else set keep with next paragraph of mySel to false end if end tell
And finally it worked!
Now I just had to find a way to assign a keyboard shortcut to this script. Unfortunately, several years ago Apple eliminated application-specific script menus in all but a handful of applications. (The only Apple application that has one on my machine these days is iTunes.) Because of this, it was not possible to assign a shortcut to the script via Mac OS X’s System Preferences.
Then I remembered that blog post from several years ago where I had talked about that very issue, because I remembered the comments included a possible solution. And sure enough, I found this old comment by Daniel Jalkut of Red Sweater Software plugging his own FastScript utility.
I went ahead and downloaded it and installed it, and sure enough I was able to use it to assign a convenient keyboard shortcut (shift-option-command-K) to my new “Keep With Next” script for use within Pages.
Now that I had it figured it out, it didn’t take me long to create other scripts for similar things and assign keyboard shortcuts to them. So now I have:
- shift-option-command-K for Keep With Next on/off toggle
- shift-option-command-L for Keep Lines Together on/off toggle
- control-1 for my level 1 bullet paragraph style (which I use when I don’t want to use automatic list styles)
- control-2 for my level 2 bullet paragraph style (ditto)
- control-3 for my level 3 bullet paragraph style (ditto)
- shift-option-command-Space for my default body paragraph style
- control-command-Space for the “None” character style (for stripping character-level manual formatting)
And they all work like a charm.
The script for applying a paragraph style is the following:
tell application "Pages" set mySel to (get selection of document 1) set paragraph style of mySel to "Body" end tell
And the script for applying a character style is the following:
tell application "Pages" set mySel to (get selection of document 1) set character style of mySel to "None" end tell
I still have a few things to figure out. For example, when I use my current F8 shortcut (as defined within Pages) for my body style, it also strips the selection of extra paragraph-level manual formatting, whereas my script does not. So there’s obviously a difference between the way the styles drawer works and the way the script commands work in Pages.
But it’s a minor inconvenience. The most important thing is that I am no longer limited to the F1–F8 shortcuts for styles and I can assign shortcuts to settings only accessible via the inspector. Pages is now customizable!
I still have no inclination to explore AppleScript further, but at least I can get things done with the little I know. And maybe this post can be useful to other Pages users.