Convert Pages 3 full underlines to per-word underlines

Feb 07, '08 07:30:04AM

Contributed by: HairyPotter

I write books about Photoshop and web stuff. Since 2004, I've been using Pages to do all the work, and I never regretted leaving Word behind forever. After recently updating to Pages 3 (in iWork '08), and revising a book for its third edition, I noticed that all titles I have created in Pages 1 and 2 using per-word underlines were converted to full underlined paragraphs. As an example, here's a before-and-after view of a sample title:

This is a title ... This is a title

So, I was forced to build the following AppleScript code to solve this mess:

tell application "Pages"
  tell front document
    repeat with one_paragraph in paragraphs
      if paragraph style of one_paragraph is paragraph style "Heading 2" then
        repeat with one_char in characters of one_paragraph
          if (one_char as string is (ASCII character 32) and underline type of one_char is single underline) or (one_char as string is tab and underline type of one_char is single underline) then
            set underline type of one_char to none
          end if
        end repeat
      end if
    end repeat
  end tell
end tell
display dialog "That's all!"
This code will remove all underlines from spaces or tab characters that may be present on any paragraph whose style's name is "Heading 2" -- change this to your own paragraph style name. Of course, you can use this code to do other stuff, as a selective find and replace that will work inside just one or selected paragraph styles. To search inside multiple paragraph styles, just add more options to the fourth line of code, transforming it into something like this:

if paragraph style of one_paragraph is paragraph style "Style 1" or paragraph style of one_paragraph is paragraph style "Style 2" or paragraph style of one_paragraph is paragraph style "Style 3" then

[robg adds: This script worked as described when I tested it. To make it easier to use, you can save it as a script in your user's Library » Scripts » Applications » Pages folder (create the folders as necessary). You can then run it from the Scripts menu in the menu bar when in Pages.]

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080131100846646