Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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 Best 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 6:30 PM in response to twtwtw

Yeah, I'm sorry I didn't make it clear. I have a few addresses that have more than 1 line in the street address and, yes, I expected it could pick up the carriage return between "1234 1st St." & Apt. 2234

and write
1234 1st St.
Apt. 2234

Because if you look in the Address Book Script dictionary it does say that
"street (text) : Street part of the address, multiple lines separated by carriage returns."

Also, if I drop a address entry with a multiline street address into a Pages mail merge, it does pick up on the 2-line street address.


so I tried running the following:

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


and got:

Address Book got an error: Can’t make character 1 of street of address 1 of person id "30A40C61-3A4B-11D7-B726-000A9575842C:ABPerson" into type reference.

So, it appears I can't further analyze the text of the street entry using applescript.

So, I guess I just got to put up with this "hangnail" It's no big deal, 'cause 99% of my contacts have a single-line street address. So, _thanks for your help_, and we'll call this solved!

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

actually, that's a referencing error. an address in address book is an object, not a text string, and applescript will often deal with objects by making references. i.e., you can't take character 1 of a reference. the way around it is to collect the address first - 'set theAddress to street of first address of thisPerson' - and then get the characters of the theAddress (which will be a proper text string). also, you probably want to use 'word' rather than 'character', or else split the address line by spaces and work with those chunks.

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!

Nov 1, 2009 8:51 PM in response to jamesabomb

Hey, I've got it! This one works!:


tell application "Address Book"
add the selection to the group "envelope"
save addressbook
set theContacts to the selection
repeat with thisPerson in theContacts
set theName to name of thisPerson
set theStreetstring to street of first address of thisPerson
set theStreet to paragraph 1 of theStreetstring
try
set theStreet2 to paragraph 2 of theStreetstring
set theStreet to theStreet & return & theStreet2
end try
try
set theStreet3 to paragraph 3 of the theStreetstring
set theStreet to theStreet & return & theStreet3
end try
set theCity to city of first address of thisPerson
set theState to state of first address of thisPerson
set theZip to zip of first address of thisPerson
end repeat
end tell
set CSZ to theCity & ", " & theState & " " & theZip
activate application "TextEdit"
tell application "System Events"
keystroke theName
key code 36
keystroke theStreet
key code 36
keystroke CSZ
end tell

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

excellent. two things, just as FYIs. first, this code will be a bit more efficient than the sequential 'try' statements you're using now, as well as being less subject to breakage:
set theStreetArray to paragraphs of (street of first address of thisPerson)
set {tid, applescript's text item delimiters} to {applescript's text item delimiters, return}
set theStreet to theStreetArray as text
set applescript's text item delimiters to tid

second (and breaking a personal rule about repeating myself), building your text in system events using 'key code 36' for returns is really +*the worst possible way*+ one can do this. GUI scripting in system events is applescript's 'technique of last resort': it's fudgy, kludgy, slow, prone to breaking, and places unfortunate and unnecessary restrictions on what you can do. You seem to have your mind set on doing it this way, for some unfathomable reason, so I'm not going to say anything more about it. However, if (or should I say when) it eventually breaks, please refer to the post above where I talk about building the string in applescript and using one (1) keystroke command to write it to the document.

consider yourself appropriately lectured. 🙂

Nov 2, 2009 4:55 AM in response to twtwtw

Hey, thanks, but the array didn't work, got an error.

So here's the final version:


tell application "Address Book"
--adds the selection to the group "envelope" to drag and drop later into a Pages mail merge to print out the day's envelopes
add the selection to the group "envelope"
save addressbook
set theContacts to the selection
repeat with thisPerson in theContacts
set theName to name of thisPerson
set theStreetstring to street of first address of thisPerson
set theStreet to paragraph 1 of theStreetstring
try
set theStreet2 to paragraph 2 of theStreetstring
set theStreet to theStreet & return & theStreet2
end try
try
set theStreet3 to paragraph 3 of the theStreetstring
set theStreet to theStreet & return & theStreet3
end try
set theCity to city of first address of thisPerson
set theState to state of first address of thisPerson
set theZip to zip of first address of thisPerson
end repeat
end tell
set theaddress to theName & return & theStreet & return & theCity & ", " & theState & " " & theZip
activate application "TextEdit" --or other wordprocessor
tell application "System Events"
keystroke theaddress
end tell

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 ID.