Customizing Pages ’09: Extra tools palette with DragThing

Posted by Pierre Igot in: Pages
April 26th, 2011 • 10:45 am

A while ago, I wrote about my use of Keyboard Maestro and AppleScript to create a keyboard shortcut for formatting options like “Keep with following paragraph” and “Keep lines together.”

With the default user interface in Pages ’09, the only way to access these commands is through the “More” tab of the Text inspector:

Keep with next

When you use these options regularly, as I do, this quickly becomes very tedious.

Fortunately, these commands can be controlled by AppleScript. And you can use Keyboard Maestro to assign a keyboard shortcut to your AppleScript script, which means that, with these two tools, you effectively have a way to assign the keyboard shortcut of your choice to any Pages ’09 command or option that can be controlled by AppleScript.

Unfortunately, there are some bugs in Pages ’09’s AppleScript support, which means that the task of developing a script that works appropriately in all circumstances is more complicated than it should be. But eventually one can get it to work, as I explained in another post earlier this year.

Still, even with a keyboard shortcut for an option such as “Keep with following paragraph,” I was not entirely satisfied. The reason for this was that, quite often, when I need to use this option, my hand is actually on the mouse. Ideally, therefore, I would also like to have a mouse-based alternative for applying the “Keep with following paragraph” option.

Of course, the Text inspector is a mouse-based alternative, but, as I said, it’s quite tedious, because you have to manually switch to the appropriate inspector and the appropriate tab and then move your mouse pointer to a rather small line with a checkbox. I decided that I would much prefer a simple button in a toolbar, directly accessible at all times.

I wondered if it were possible to add such a thing to Pages ’09. Of course, unlike Microsoft Word, Pages ’09 has no support for customization features such as user-defined toolbars and toolbar buttons. But I am also a long-time user of DragThing, and I wondered if it would not be possible to create a Pages ’09-specific DragThing dock that would act as a sort of toolbar, and to have buttons for AppleScript scripts in that toolbar.

It turns out that DragThing does offer such a option, because, among the dock options, you can specify that a given dock must only be shown in a specific application. So I created a DragThing dock and configured it so that it would only appear in Pages ’09:

Dock for Pages ’09

I also configured it so that the dock tiles would work as single-click buttons, just like buttons in a toolbar.

And then I created run-only scripts for the options I wanted in this toolbar, which included not only “Keep with following paragraph” and “Keep lines together” (“KWN” and “KLT” respectively here), but also buttons for increasing or decreasing space before and space after by 6 points at a time.

Much like the “Keep with following paragraph” and “Keep lines together” options, the Space Before and Space After options in Pages ’09 are only accessible via the Text inspector (and not in the same tab, of course), and they are quite tedious to use when, like me, you tend to use them all the time.

For the scripts, I just reused the scripts that I had created for Keyboard Maestro and saved them as run-only scripts. Here’s my script for “Keep with following paragraph,” for example:

tell application "Pages"
	tell front document
		set myStyles to name of paragraph style of paragraphs
	end tell
	tell application "Pages" to activate
	if myStyles does not contain "TOC Heading 1" then
		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
	else
		tell application "System Events"
			tell process "Pages"
				click radio button 4 of radio group 1 of window 1
				click radio button 4 of tab group 1 of group 1 of window 1
				click checkbox 2 of tab group 1 of group 1 of window 1
			end tell
		end tell
	end if
end tell

And here’s my script for adding 6 points to the space after the current paragraph:

tell application "Pages"
	tell front document
		set myStyles to name of paragraph style of paragraphs
	end tell
	tell application "Pages" to activate
	if myStyles does not contain "TOC Heading 1" then
		set mySel to (get selection of document 1)
		set mySpace to ((space after of mySel) + 6)
		set space after of mySel to mySpace
	else
		tell application "System Events"
			tell process "Pages"
				click radio button 4 of radio group 1 of window 1
				click radio button 1 of tab group 1 of group 1 of window 1
				click button 1 of incrementor 2 of tab group 1 of group 1 of window 1
				click button 1 of incrementor 2 of tab group 1 of group 1 of window 1
				click button 1 of incrementor 2 of tab group 1 of group 1 of window 1
				click button 1 of incrementor 2 of tab group 1 of group 1 of window 1
				click button 1 of incrementor 2 of tab group 1 of group 1 of window 1
				click button 1 of incrementor 2 of tab group 1 of group 1 of window 1
			end tell
		end tell
	end if
end tell

As noted earlier, I need the GUI-scripting-based alternative when the document contains a table of contents, because there is a bug in Pages ’09’s AppleScript support that causes it to fail to compute the position (offset) of the current selection properly when a TOC is present.

Once these scripts are saved as run-only, they can be triggered by simply clicking on a button in the DragThing dock. There is a quick switch of focus from Pages ’09 to DragThing and then back to Pages ’09 (thanks to the activate command in the script), but it’s something that I can live with.

As you can see in the script for adding 6 points of space after, in the GUI-scripting alternative, I had to use 6 clicks on the incrementor, because, as I far as I can tell, it’s not possible to properly retrieve and set the value of the Space After field itself.

This frustrates me, because I would also like to have buttons for changing Space After to a specific value, such as 12 pt, rather than adding or subtracting 6 pt. This can be done via AppleScript, but I cannot get it done via GUI scripting. I tried this:

tell application "System Events"
	tell process "Pages"
		click radio button 4 of radio group 1 of window 1
		click radio button 1 of tab group 1 of group 1 of window 1
		set value of text field 5 of tab group 1 of group 1 of window 1 to "12 pt"
	end tell
end tell

But while it does appear to change the value of the text field to 12 pt, the change does not actually take effect and is not applied to the selection in the Pages ’09 document.

So while I can already create dock buttons for setting the Space After/Before to 0 pt, 12 pt or 24 pt, I cannot get them to work with a GUI-scripting alternative in the presence of a TOC.

If anyone has any suggestions on how to get this to work via GUI scripting, I’d appreciate them.


4 Responses to “Customizing Pages ’09: Extra tools palette with DragThing”

  1. Customizing Pages « Clark's Tech Blog says:

    […] Customizing Pages. I link to it as it shows the value of a toolbar with custom Applescript. Quickeys supports custom toolbars quite easily. So I’ll probably be doing something similar soon. […]

  2. Betalogue » Customizing Pages ’09: Controlling ‘After Paragraph’ value with GUI scripting says:

    […] I posted about customizing the Pages ’09 user interface with a DragThing dock that performs AppleScript scripts earlier today, I decided to further investigate the reason why I was unable to directly control the […]

  3. Crea paletas de herramientas personalizadas para aplicaciones con Drag Thing says:

    […] específicas para Pages con opciones para modificar el formato de las hojas de estilo lo tienes en Betalogue con Applescripts incluidos, pero las opciones son extremadamente variadas, ya que Applescript y […]

  4. Betalogue » Customizing Pages ’09: Keyboard Maestro palettes says:

    […] reading my recent post about using DragThing to further customize my Pages ’09 work environment by adding an […]