Customizing Pages ’09: Scripts unusable for text inside table cells

Posted by Pierre Igot in: Pages
April 22nd, 2010 • 2:52 pm

My recent surge of enthusiasm for Pages ’09 customizations using a combination of the application’s AppleScript support and Keyboard Maestro has, unfortunately, also led me to discover the limitations of said AppleScript support.

I already noted that the “Title Case” capitalization option in Pages ’09’s “Format” menu does not seem to have an AppleScript equivalent, which is frustrating.

Then I discovered that, when a Pages ’09 contains an automatic table of contents, AppleScript fails to count the text in the table of contents when computing the offset of the selection, which results in changes applied to the wrong range of characters in the document.

I’ve also discovered a pretty serious bug that can cause the Keyboard Maestro engine (the part of the program that needs to be running at all and catches your keystrokes) to crash. I have a pretty extensive discussion with Keyboard Maestro developer Peter Lewis about this, and he assures me that his engine should never crash and that the crashes I am experiencing are due to some kind of weird bug deep inside AppleScript that he cannot do anything about.

He was kind enough, however, to provide me with a workaround, which consists of executing the offending AppleScript script in Keyboard Maestro by using the shell command osascript instead of the default “Execute an AppleScript” action. This eliminates the crash, but of course the bug still exists.

I’ve reported it to Apple, but since it requires a number of elements and involves a third-party application, and the crash, even if it’s triggered by Mac OS X’s own AppleScript, actually happens in the third-party application itself, my hope of getting Apple’s engineers to even pay attention to this is pretty small.

And finally, today I’ve discovered that all my macros involving changing properties of the selection, such as the paragraph style, the character style or other formatting options, do not work in table cells at all.

After a bit of investigation, I was able to discover that, when the selection is a range of characters within the body of the document (not inside a table), the selection in AppleScript looks like this:

text from character 1 to character 4 of body text of document id 3539679 of application "Pages"

On the other hand, when the selection is a range of characters in the text within a table cell in a table in the document, the selection in AppleScript looks like this:

text from character 1 to character 4 of some table of document id 3539679 of application "Pages"

Notice anything in there? I do. It’s the key word some. What the hell is that? What happens if you have two tables in the same document? Or if you select something in a different cell in the same table?

Well, I’ve tried and it turns out that this is it. There is nothing more. When the selection is a range of characters inside a table cell, the selection is unable to specify which table (in case there are several tables) or even which cell within the table.

In other words, if you select chars 1 to 4 in the first cell of your first table, in the second cell of your first table, or in the first cell of your second table, the value of the selection in AppleScript is exactly the same!

No wonder my macros for changing the properties of the selection don’t work!

This is terrible. It means that basically Apple’s engineers did not bother to support document with tables in Pages ’09’s AppleScript architecture.

That already makes quite a range of issues that I have encountered in AppleScript since I started working on customizing Pages ’09. Some of these issues can be worked around, but some cannot, and we are entirely at the mercy of Apple’s engineers. Either they decide to devote some resources to improving AppleScript support in Pages, or they don’t. If they don’t, we’re stuck.

And it sucks.

Comments are closed.