Pages ’09 Tip: Shortcut for creating hyperlink

Posted by Pierre Igot in: Pages
February 28th, 2012 • 4:20 pm

A couple of years ago, I wrote about the annoying default behaviour of the “Link” inspector in Pages ’09, which is used to turn the selected text into a link:

Link inspector in Pages ’09

What’s annoying about it isn’t just that the “Link” inspector is the only way to create such a link, but also that, each and every time you click on the “Enable as a hyperlink” checkbox, by default Pages ’09 applies the default URL http://livepage.apple.com to the selected text in your Pages ’09 document. (UPDATE: Other Betalogue readers report getting a different URL, but the problem remains the same, in that the URL is always the same. It seems that Pages ’09 somehow remembers the very first URL you ever used and then always uses that one — although I don’t remember ever using this particular URL myself.)

It’s a pretty stupid behaviour, because, let’s face it, how many times are you going to want to link to this particular page on the Apple web site? (Besides, the site does not even exist anymore and redirects you to http://www.apple.com/startpage/ instead.)

And it’s particularly stupid when you have copied a URL to the Clipboard and just want to turn the selected text into a link to that URL. It would be perfectly logical for Pages ’09 to recognize that you have a URL in your Clipboard and just use that as the default value for the newly created link. But no… that is too much to ask.

Today, I just got tired of this annoying default behaviour and of seeing http://livepage.apple.com each and every time I create a link in Pages ’09. So I decided to try and do something about it. Of course, as regular Betalogue readers know, my approach to customizing and improving Pages ’09 involves a combination of AppleScript scripting and Keyboard Maestro for triggering the scripts.

My first thought was that there might be something in Pages ’09’s AppleScript dictionary for links, but I couldn’t find anything, so naturally the solution was to switch to GUI scripting instead. This is a far less intuitive approach (if the regular AppleScript 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 code.

So here’s the script that I ended up using, with control-A used as the trigger in Keyboard Maestro:

tell application "System Events"
	tell process "Pages"
		click radio button 9 of radio group 1 of window 1
		click radio button 1 of tab group 1 of group 1 of window 1
		click checkbox 2 of tab group 1 of group 1 of window 1
		click text field 1 of group 1 of tab group 1 of group 1 of window 1
		set value of text field 1 of group 1 of tab group 1 of group 1 of window 1 to the clipboard as text
		confirm text field 1 of group 1 of tab group 1 of group 1 of window 1
	end tell
end tell

The four click actions are pretty straightforward once you know what UI elements the various numbered items refer to.

The set value line is what changes the stupid default value in the URL field from http://livepage.apple.com to the URL in the Clipboard.

And finally the confirm line is what gets Pages ’09 to recognize the changed value and record it properly. If you don’t include that line, although the value of the URL field changes visibly in the “Link” inspector, Pages ’09 does not actually register the change and the URL does not change from http://livepage.apple.com.

As usual, this script assumes that there is an inspector window open in Pages ’09. If you don’t usually have an inspector window open, you’ll have to add extra lines to test for its presence and open it if necessary. I always have an inspector window open, so I don’t see the need for it myself.

And of course, this script assumes that you have copied the desired URL to your Clipboard in the first place.


Comments are closed.