The following script, which is an adaptation of a script provided by a contributor to this site, worked under Mavericks. It doesn’t work properly under Yosemite (it hangs immediately if run directly, it hangs upon completion if run from the script editor. Any clues about why?
tell (current date)
set currentDate to its date string
set fileNameDateString to (its day as integer) & space & (its month as text) & space & year
end tell
set desktopFolder to path to desktop as text
set mobileFolder to (path to library folder from user domain as text) & “Mobile Documents:com~apple~Pages:Documents:”
– ~/Library/Mobile Documents/com~apple~Pages/Documents
tell application “Contacts”
###log (get its properties)
launch
activate
set theSelection to selection
if theSelection is {} or class of item 1 of theSelection is not person then return
set thisPerson to item 1 of theSelection
set companyName to organization of thisPerson – will be “missing value” if none
set personName to name of thisPerson
set lastName to (last name) of thisPerson
set firstName to (first name) of thisPerson
set nName to (nickname) of thisPerson
set salutName to firstName
if nName is not missing value then
set salutName to nName
end if
set nameVar to firstName & space & lastName
set theAddresses to addresses of thisPerson
if theAddresses is {} then return
if (count theAddresses) is 1 then
set currentAddress to item 1 of theAddresses
else
set addressList to {}
repeat with i from 1 to count theAddresses
set end of addressList to (i as text) & space & label of item i of theAddresses & space & street of item i of theAddresses
--set addressList to addressList & space & street of item i of theAddresses
end repeat
set chosenItem to choose from list addressList with title "Write a nice letter to " & nameVar with prompt "Pick an address"
if chosenItem is false then return
set addressIndex to (word 1 of item 1 of chosenItem) as integer
set currentAddress to item addressIndex of theAddresses
end if
set addressStreet to street of currentAddress
set addressCity to city of currentAddress & space & state of currentAddress & space & zip of currentAddress
set addressCountry to country of currentAddress
end tell
set buttonSmall to “Brief”
set buttonLarge to “Natter”
set thePrompt to “Are you going to be brief or natter on? (default is brief)”
display dialog thePrompt buttons {buttonSmall, buttonLarge} default button buttonSmall
set doWhat to button returned of result
set fileName to lastName & ", " & firstName & " " & fileNameDateString
tell (current date) to get day & space & (its month as string) & space & year
set letterDate to result as text
tell application “/Applications/Pages.app”
if doWhat is buttonSmall then
set theTemplate to "TNC Small"
else if doWhat is buttonLarge then
set theTemplate to "TNC Large"
end if
if theTemplate is false then error number -128
set currentDocument to ¬
make new document with properties {document template:template theTemplate}
tell currentDocument
###log (get its properties) # EDITED #######################
tell body text
set the first word of the first paragraph to letterDate
if companyName is not missing value then
set addressString to personName & linefeed & companyName & linefeed & addressStreet & linefeed & addressCity
else
set addressString to personName & linefeed & addressStreet & linefeed & addressCity
end if
if addressCountry is not missing value and addressCountry is not "United States of America" then
set addressString to addressString & linefeed & addressCountry
end if
set the first word of the third paragraph to addressString
set the first word of the second paragraph to linefeed & "Dear " & salutName & ":" & linefeed & linefeed & linefeed
end tell
end tell
end tell
delay 0.1
activate application “Pages”
tell application “System Events” to tell process “Pages”
# awful trick to select something so that other commands do their job
keystroke “a” using {command down}
key code 126 using {command down}
repeat 4 times
key code 125
end repeat
–save currentDocument in file (desktopFolder & fileName) – save to desktop
–save currentDocument in file (mobileFolder & fileName) – save to mobile folder
–save currentDocument in fileName
end tell
set the clipboard to fileName