Customizing Pages ’09: Controlling ‘After Paragraph’ value with GUI scripting

Posted by Pierre Igot in: Pages
April 26th, 2011 • 6:53 pm

After 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 value of the “After Paragraph” text field in the “Text” tab of the Text inspector.

I had the following script:

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

This script was able to use GUI scripting (i.e. System Events) to change the value of the “After Paragraph” text field (text field 5 here) to 12 pt, but unfortunately, this change was not reflected in the currently selected paragraph in the frontmost Pages ’09 document window.

In other words, Pages ’09 did not seem to “register” the changed value in the field, and just moving away and then back to the paragraph would cause the field to revert back to the existing value.

I figured that the problem might be due to the fact that, during the execution of the script, the focus was not on the inspector itself — as opposed to what would happen when I changed the value manually with the mouse and then the keyboard — so I tried various things to try and reproduce the focus-changing behaviour of my mouse/keyboard actions via GUI scripting.

In the process, I ended up purchasing a copy of UI Browser, even though it’s quite expensive ($55 US), because I really needed a fairly user-friendly way to explore the various attributes of UI elements.

(TIP: If you are purchasing UI Browser at Digital River from a non-US country, check your exchange rate. The Canadian dollar is currently higher than the US dollar, and yet Digital River still wanted to charge me more than $55 in Canadian dollars. I changed the currency back to US dollars. This should bring the value down to approximately $53 CDN on my VISA statement, which is where it should be. I did the same thing a few weeks ago with another software vendor, and the value was indeed lower on my VISA statement.)

I saw that the text field has a boolean attribute called “focus,” but setting its value to “true” didn’t make any difference.

I also thought I could try clicking on the text field and then pressing the Return key, but that too failed to make any difference. (The Return keystroke would result in the insertion of a return char in my document window, so the simulated click obviously didn’t change the focus properly.)

Finally, while exploring the UI Browser help, I saw a section about performing actions on UI elements, and it indicated that you could get all available actions for any UI element in the “Actions” drawer in UI Browser.

It turns out that the “After Paragraph” text field supports only one action, which is called… confirm.

I tried to use UI Browser’s AppleScript menu to generate the proper command to perform this action, but only got

(null) text field 5 of tab group 1 of group 1 of window 1

which obviously wasn’t right. But I then thought of simply trying this in my script:

confirm text field 5 of tab group 1 of group 1

And guess what? It worked! If I simply add this line after the line that sets the value of the text field, Pages ’09 actually registers the value change and applies it to the current selection.

Yey!

So to recap, the final script is this:

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

This script changes the value to 12 pt and applies the change to the current selection.

It now provides me with a GUI-scripting-based alternative for my space adjusting scripts when the default AppleScript commands do not work properly because of the bug in Pages ’09’s AppleScript support with documents containing tables of contents.

So, for example, a script that will change “After Paragraph” to 12 pt in all cases will look like this:

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 space after of mySel to 12.0
	else
		tell application "System Events"
			tell process "Pages"
				tell window 1
					click radio button 4 of radio group 1
					click radio button 1 of tab group 1 of group 1
					click text field 5 of tab group 1 of group 1
					set value of text field 5 of tab group 1 of group 1 to "12 pt"
					confirm text field 5 of tab group 1 of group 1
				end tell
			end tell
		end tell
	end if
end tell

This script checks the document’s style sheet to see if it contains the “TOC Heading 1” style. If it does not, then it normally means that there is no TOC in the document and so the bug in Pages ’09’s AppleScript support won’t affect the regular AppleScript commands. The regular “set space after” command can be used.

If the style sheet does contain the TOC style, then the script switches to the GUI-scripting approach and manually changes the value of the text field in the Text inspector and then confirms the change.

(REMINDER: This script assumes that an inspector is always open. Otherwise, you need to add more code to open an inspector first.)

The same method can be used with text field 4, which is the field for “Before Paragraph” spacing.

And now I just need to add buttons to my DragThing dock for the run-only versions of these scripts, and I will have simple toolbar buttons to perform these actions in Pages ’09.


One Response to “Customizing Pages ’09: Controlling ‘After Paragraph’ value with GUI scripting”

  1. Betalogue » Pages ’09 Tip: Shortcut for creating hyperlink says:

    […] can be called intuitive, that is), but it’s the only one available and, with the help of UI Browser, it’s not too painful to figure out the needed […]