Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create customized Pages '09 letters from Address Book Apps
I send a lot of letters written in Pages '09 from myself personally, as well as from my business. Pages already offers "merge fields" that allow you to setup placeholder text which can be populated by dragging and dropping in a contact from Address Book, but you have to pre-choose the type of address when you set these fields up.

Normally, I set up all my templates using a person's work address, but sometimes, I need to send a letter (business or personal) to a person's home address (or another address), and I'm back to copying and pasting the address from Address Book again.

I've decided to deal with this the other way around. By using the following AppleScript, I can now send a letter by clicking on a person's record in my Address Book, then clicking the address I wish to use. A contextual menu appears with 'Send personal letter' as an option. When I choose this, Pages will launch, create a new Personal Letter from my template, then put the person's details into the template, ready to go.

I've set-up multiples of these, one for each letter template that I'm likely to use, but even one is great!

To use this, you'll need to do three things:
  1. Make sure you have the path and filename set correctly in the script below to point to your Pages template.
  2. Save the below AppleScript as a Script (not an Application or Script Bundle, etc.) into your Library here: ~/Library » Address Book Plugins » Personal Letter.scpt.
  3. Edit your template so that the you have the following key words in the right places: FirstName LastName JobTitle CompanyName WorkStreet WorkCity WorkState WorkZip. For example, your template could begin Dear FirstName LastName,.... Note that you can also use these words as conventional merge fields in Pages to get the normal drag-and-drop behavior from Address Book, too, but it's not necessary for this hint (but still useful).
Once you've done these things, you should be good to go (you shouldn't even need to quit and relaunch Address Book). Here's the code:
-- Set up the default template location in Pages

using terms from application "Address Book"
  on action property
    return "address"
  end action property
  
  on action title for thePerson with theEntry
    if the label of theEntry is equal to "address" then return "Send personal letter"
    return "Send personal letter"
  end action title
  
  on should enable action for thePerson with theEntry
    return true
  end should enable action
  
  on perform action for thePerson with theEntry
    newLetter(first name of thePerson, last name of thePerson, job title of thePerson, organization of thePerson, street of theEntry, city of theEntry, state of theEntry, zip of theEntry)
  end perform action
  
end using terms from

on newLetter(firstName, lastName, jobTitle, companyName, street, city, state, zip)
  tell application "Pages"
    activate
    set n to count of documents
    
    -- Be sure to set this to the correct path to your template
    set myTemplate to ((path to application support from user domain as text) & "iWork:Pages:Templates:My Templates:Personal Letter.template")
    
    open myTemplate (* create a new doc from the template *)
    
    -- Wait for the document to appear
    repeat
      if (count of documents) > n then
        exit repeat
      else
        delay 0.1
      end if
    end repeat
    
    -- We should now have an open document from the template selected
    -- replace all the placeholders with the contact info
    
    tell me to replaceAll("FirstName", firstName as string)
    tell me to replaceAll("LastName", lastName as string)
    tell me to replaceAll("JobTitle", jobTitle as string)
    tell me to replaceAll("CompanyName", companyName as string)
    tell me to replaceAll("WorkStreet", street as string)
    tell me to replaceAll("WorkCity", city as string)
    tell me to replaceAll("WorkState", state as string)
    tell me to replaceAll("WorkZip", zip as string)
    
  end tell
  
  return true
end newLetter

on replaceAll(theWord, replacement)
  tell application "Pages"
    
    tell body text of document 1
      set wordCount to the count of words
      repeat with i from wordCount to 1 by -1
        if word i is theWord then
          if replacement is "missing value" or replacement is "" then
            set word i to ""
          else
            set word i to replacement
          end if
        end if
      end repeat
    end tell
    
  end tell
end replaceAll
If you'd like to present multiple choices, e.g., "Send personal letter," "Send business letter,", etc., duplicate this script, update it with your other templates' paths, and also change the label that appears in the contextual menu to something appropriate by editing both occurrences of Send Personal Letter in the script. Save it with a different name in the Address Book Plugins folder, and you're done.

I'd like to also automatically delete the placeholder text for fields that aren't in use (e.g. JobTitle, CompanyName, etc.), but so far, I haven't yet found an elegant solution for this.

[robg adds: I tested this one, and it works as described.]
    •    
  • Currently 1.83 / 5
  You rated: 2 / 5 (6 votes cast)
 
[8,018 views]  

Create customized Pages '09 letters from Address Book | 0 comments | Create New Account
Click here to return to the 'Create customized Pages '09 letters from Address Book' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.