applescript for address book

Is there a way to click on a contact name, have apple script capture first name, last name, address, then print these in a document? I know I can use mail merge in Pages, but can't in other text editors

MacBook Pro, Mac OS X (10.5.8)

Posted on Oct 28, 2009 8:54 AM

Reply
Question marked as Top-ranking reply

Posted on Oct 28, 2009 9:49 AM

tell application "Address Book"
set theContacts to the selection
set theText to ""
repeat with thisPerson in theContacts
set theText to theText & name of thisPerson & return
try
set theText to theText & formatted address of first address of thisPerson & return
end try
set theText to theText & return
end repeat
end tell
tell application "TextEdit"
set theNewDoc to make new document
set text of theNewDoc to theText
print theNewDoc
end tell

that's the basics - save it as a script, put it in and run it from the script menu (or quicksilver, or whatever...). it will grab the selected contacts in Address book.
24 replies

Nov 1, 2009 6:10 PM in response to jamesabomb

I'm looking at this you posted:

new guy
1234 1st St.Apt. 2234
blah, ks, 66220

is your concern that the 'Apt. 2234' is on the same line as the '1234 1st St.' bit? I don't think there's any way around that - AB is formatting the address correctly according to standard mail conventions by placing name, street address and city/state/zip on three separate lines. if you edit the address in address book, you'll see that even there the entire street address is considered as a single text block. you can add extra whitespace, but AB strips it out (assuming that that is a typo). This is a deeper issue having to do with system-wide data formating (google 'os x data detectors'): it's what allows you (for instance) to import a contact's name, email, address and phone number to address book from an email with a single click.

I know you can disable data detectors in Mail; you can probably do the same for address book (or maybe disabling it in Mail disables it in address book as well). that might fix the problem you're having (assuming I've understood your problem correctly, above). that seems a bit like cutting off your hand to cure a hangnail, though.

Nov 1, 2009 7:11 PM in response to twtwtw

Ok, here's the new script (I know I could have made it simpler):


tell application "Address Book"
set theContacts to the selection
repeat with thisPerson in theContacts
set theStreet to the street of first address of thisPerson
end repeat
set theStreet1 to the character 1 of theStreet
set theStreet2 to character 2 of theStreet
set theStreet3 to character 3 of theStreet
set theStreet4 to character 4 of theStreet
set theStreet5 to character 5 of theStreet
set theStreet6 to character 6 of theStreet
set theStreet7 to character 7 of theStreet
set theStreet8 to character 8 of theStreet
set theStreet9 to character 9 of theStreet
set theStreet10 to character 10 of theStreet
set theStreet to "1: " & theStreet1 & return & "2: " & theStreet2 & return & "3: " & theStreet3 & return & "4: " & theStreet4 & return & "5: " & theStreet5 & return & "6: " & theStreet6 & return & "7: " & theStreet7 & return & "8: " & theStreet8 & return & "9: " & theStreet9 & return & "10: " & theStreet10 & return
end tell
tell application "TextEdit"
set theNewDoc to make new document
set text of theNewDoc to theStreet
end tell


the script takes this 2-line street address in my Address Book:
12 1st
apt.1234

and the result is:


1: 1
2: 2
3:
4: 1
5: s
6: t
7:
8: a
9: p
10: t


AHA, character 7 is a RETURN and the applescript "sees it," but the "street" property for scripting the Address Book isn't picking it up. I think we're done, until Apple makes the script for Address Book live up to the promise in its dictionary!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

applescript for address book

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.