Pages ’09: Presence of automatic table of contents breaks ‘get selection’ in AppleScript

Posted by Pierre Igot in: Pages
April 19th, 2010 • 11:15 am

This is a bug that looks very weird… until you figure out what appears to be causing it.

Readers of my recent Betalogue posts know that I am in the process of using a combination of AppleScript and Keyboard Maestro to customize my work environment in Pages ’09, which is my main word processor.

In particular, I have scripts such as this one:

tell application "Pages"
	if selection is "" then
		set selection to "|"
	end if
	set character style of (get selection) to "Strong Emphasis"
end tell

And I have Keyboard Maestro macros that define keyboard shortcuts for triggering such scripts. In this particular case, I use the command-shift-E shortcut as a shortcut for triggering this script, which changes the character style of the selection to “Strong Emphasis.”

This morning I was working on a fairly large document that I have created in Pages ’09. I was trying to use the shortcut to apply the character style to the selection, and I saw that, instead of applying the style to the selection, AppleScript was applying to a range of text of the same length a couple of paragraphs higher up in the document!

That was definitely weird. I thought it was just a temporary glitch, so I quit everything and tried again. No change. I tried in a brand new Pages ’09 document and I was unable to reproduce the problem. So the problem was obviously linked to this particular document that I was working on.

I tried reproducing the bug with the same document and the same AppleScript script in a new user environment in Mac OS X, just to make sure it wasn’t some weird interference from another third-party product. I was able to reproduce the problem just fine. I was also able to verify that it had nothing to do with Keyboard Maestro, since I was able to trigger it just by pressing the “Run” button in the AppleScript Editor window for the script in question.

Then I went back to the very beginning of the document and tried to reproduce the problem. I wasn’t able to. And I went down a few pages. And I was able to reproduce the problem. Obviously it was something in this particular document, in the first few pages.

It didn’t take me long to figure out that it was the automatic table of contents that I had inserted in my document using Pages ’09’s built-in “Insert › Table of Contents” command. If I deleted the table of contents, the problem was gone. If I put it back, the problem was back for any selection coming after the table of contents.

So to me it looks like there is obviously a bug in Pages ’09’s AppleScript support that causes it to fail to compute the offset of the current selection in a document if that selection comes after a section of the document that contains an automatic table of contents.

Needless to say, I submitted a bug report to Apple with supporting evidence. If you are able to reproduce the problem on your computer, I urge you to do the same.

Fortunately, in my workflow, I typically only insert the table of contents towards the very end of my work on the document, so this problem does not affect me during most of my work on the document. But it’s still annoying to be unable to use all my AppleScript scripts once I have an automatic table of contents in my Pages ’09 document!

Comments are closed.