Customizing Pages ’09: Updated scripts for toggling All Caps and Small Caps

Posted by Pierre Igot in: Pages
May 5th, 2010 • 9:36 am

Last month I wrote a post about creating shortcuts for toggling the “All Caps” and “Small Caps” character formatting options in Pages ’09. If you just assign a keyboard shortcut to either command using Mac OS X’s built-in “Keyboard Shortcuts” feature in System Preferences, it will be a shortcut that only works one way, to apply the formatting option. In order to remove the formatting option, you have to assign and use a separate shortcut for the “None” formatting option. I much prefer using the same shortcut both to apply and to remove the formatting option.

As indicated in my earlier post, the solution is to use a combination of AppleScript and a third-party tool like Keyboard Maestro to assign the desired shortcut to an AppleScript script that tests the current formatting option of the selection and changes it to the other one:

tell application "Pages"
	set mySel to (get selection of document 1)
	if capitalization type of mySel is all caps then
		set capitalization type of mySel to normal capitalization
	else
		set capitalization type of mySel to all caps
	end if
end tell

Unfortunately, I have since discovered that, due to the limitations of AppleScript support in Pages ’09, this only works when the selection is a range of characters in the body text of a Pages ’09 document. If the selection is a range of characters inside a table cell or inside a footnote, the properties of the selection become inaccessible and the above script does not work. And neither do scripts for applying things like character styles and paragraph styles to the selection.

After a fair amount of experimenting with the help of an AppleScript expert on the Apple Discussions AppleScript forum, I was able to come up with a solution for applying character styles to a selection inside a table cell or footnote. The solution uses something called Graphic User Interface (GUI) Scripting, which works with any Mac OS X application that follows Mac OS X’s basic user interface guidelines, even if it does not support AppleScript or, as is the case with Pages ’09, only offers partial AppleScript support.

The solution for applying a character style to a selection inside a table cell or footnote was fairly complex, because character styles are not readily accessible via standard UI controls in the Pages ’09 interface. They are only accessible through the styles drawer and through a custom pop-up menu in the Format Bar, both of which come with additional limitations for GUI Scripting that require a clever workaround.

Thankfully, the solution for applying the “All Caps” and “Small Caps” character formatting options to a selection inside a table cell or footnote in Pages ’09 using AppleScript is not as complex. But it still requires the use of GUI Scripting, and it took me a while to figure out exactly how to do it.

Here’s the script that I ended up using for toggling the “All Caps” formatting option for a selection in a Pages ’09 document:

tell application "Pages"
	try
		set mySel to (get selection of document 1)
		if capitalization type of mySel is all caps then
			set capitalization type of mySel to normal capitalization
		else
			set capitalization type of mySel to all caps
		end if
	on error
		tell application "Pages" to activate
		tell application "System Events" to tell process "Pages"
			tell menu bar 1
				tell menu bar item "Format"
					tell menu "Format"
						tell menu item "Font"
							tell menu "Font"
								tell menu item "Capitalization"
									tell menu "Capitalization"
										try
											set myMark to value of attribute "AXMenuItemMarkChar" of menu item "All Caps"
											if myMark is "?" then
												click menu item "None"
											end if
										on error
											click menu item "All Caps"
										end try
									end tell
								end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
	end try
end tell

The first part of this script is the same as in the original script above. It works just fine if the selection is a range of characters in the body text of the current front-most Pages ’09 document window.

If the selection is a range of characters in a table cell or in a footnote, however, the selection object does not have properties, and so the “capitalization type” property does not exist and the “if capitalization type of mySel is all caps then” line produces an error.

Thankfully, AppleScript has a built-in mechanism for handling errors, with the try command, which lets you provide an alternative (introduced by on error) in case there is an error.

In this case, if there is an error, my script uses GUI Scripting (via the “System Events” process) to access the “Capilization” submenu in the “Font” submenu in the “Format” menu in Pages ’09’s menu bar.

It tests the “AXMenuItemMarkChar” attribute of the “All Caps” menu item and, if the menu item is checked, it clicks on the “None” menu item to remove the capitalization. If the menu item is unchecked, it clicks on it to apply the capitalization. (If the check mark is a dash, indicating a mixed selection, the script will just remove the capitalization from the portion of the selection that is capitalized.)

This part of the script uses yet another try… on error… structure because the very attempt to read the “AXMenuItemMarkChar” attribute causes an error if the menu item is not checked (instead of giving, for example, a value of "", which would be more logical).

There are minor variations that can be used instead of my approach, but they all essentially amount to the same thing, which is to use GUI Scripting to access the menu item when the proper AppleScript-based approach fails because of Pages ’09’s limited AppleScript support.

Of course, you could also use the GUI Scripting-based approach in all cases and not just for when the selection is a range of characters in a table cell or footnotes. There is nothing that prevents the GUI Scripting-based approach to work properly when the selection is a range of characters in the body text of the document.

But it offends my sensibility to have to use GUI Scripting when a proper AppleScript-based solution exists. In addition, it is possible that there is a slight performance hit when using a GUI Scripting-based approach, especially on slower machines. (I cannot verify this. On my 2009 Mac Pro, both approaches are fast enough.)

Naturally, one still hopes that some day Apple’s engineers will improve Pages ’09’s AppleScript support so that using GUI Scripting is no longer necessary. But in the meantime it works and helps improve the customizability of the Pages application on today’s machines with today’s available tools.

Obviously, the exact same script can be used to toggle other formatting options, such as the “Small Caps” capitalization option.

I should just note that this particular script is language sensitive because it refers to the menu items by their titles. If you want the script to work when the user interface language is not English, you’ll have to either change the menu item titles to their equivalent in the other language or refer to the menu items by number rather than by title. I find the script more readable with the menu item titles, and I don’t need to use it with a user interface language other than English at this point, so I am going to leave it the way it is.


Comments are closed.