Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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
Question marked as Best reply

Oct 28, 2009 9:49 AM in response to jamesabomb

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.

Oct 29, 2009 11:10 AM in response to twtwtw

hey twtwtw, having a few bugs here, thanks for your help

1st, "formatted address" yields this result for the city, state & zip:
Kansas Citys KSs 66160 (using "s" instead of ",")
so I tried splitting "formatted address" up:
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
keystroke theStreet
key code 36
keystroke CSZ
and got city-state-zip correct:
Kansas City, KS 66160

But the remaining bug is for multi-line street addresses, it's not implementing the carriage return, even though the Address Book script library says "Street part of the address, multiple lines separated by carriage returns."

so I'm getting:
University of Kansas Medical CenterDepartment of Neurology3901 Rainbow Blvd.
when I of course want:
University of Kansas Medical Center
Department of Neurology
3901 Rainbow Blvd.

Any ideas?

Oct 30, 2009 10:39 PM in response to jamesabomb

jamesabomb wrote:

set CSZ to theCity & ", " & theState & " " & theZip
activate application "TextEdit"
tell application "System Events"
keystroke theName
keystroke theStreet
key code 36
keystroke CSZ
and got city-state-zip correct:
Kansas City, KS 66160

But the remaining bug is for multi-line street addresses, it's not implementing the carriage return, even though the Address Book script library says "Street part of the address, multiple lines separated by carriage returns."

so I'm getting:
University of Kansas Medical CenterDepartment of Neurology3901 Rainbow Blvd.
when I of course want:
University of Kansas Medical Center
Department of Neurology
3901 Rainbow Blvd.

Any ideas?


yeeee... first, don't use system events for simple text processing - you'll give yourself a mental hernia. just say this:
set theAddressString to theName & return & theStreet & return & CSZ
tell application "TextEdit"
set text of document 1 to theAddressString
end tell

The 'return' bits will give you the proper effect. with respect to the other problem, it may be a confusion about line ending characters. Mac style is to use a carriage return, unix style is to use a linefeed, Windows style uses a carriage return followd by a linefeed. if you've imported your address book entries from a different machine they may have odd line ending codes built in which you'd have to test for. hard to test for that from this side of the internet, though.

Oct 31, 2009 6:18 AM in response to twtwtw

I see, but I'm putting the address into an already made document, so I think I have to do it this way. I formatted the address book on the same mac I'm using for the applescript and it still doesn't make the line breaks on multi-line street addresses. Now, I'm typing on a different mac and tried the script on it, same bug, script doesn't do the carriage returns for multiline street addresses.

Oct 31, 2009 8:26 AM in response to jamesabomb

jamesabomb wrote:
I see, but I'm putting the address into an already made document, so I think I have to do it this way. I formatted the address book on the same mac I'm using for the applescript and it still doesn't make the line breaks on multi-line street addresses. Now, I'm typing on a different mac and tried the script on it, same bug, script doesn't do the carriage returns for multiline street addresses.


this response didn't quite track for me. what do you mean 'putting the address into a ready made document'? what kind of document, and how are you 'putting it in'? which 'it' do you have to do 'this way', and which 'this way' are you talking about, and why do you think that doing 'it' 'this way' is necessary?

sorry, but the help I can give you is only as good as the information you give me. clarity is important.

on my machine (macbook pro running 10.5.8) I have no problems - applescript captures multi-line addresses from address book perfectly, with the correct line breaks and everything. the fact that it doesn't for you can only mean (1) your addresses are formatted incorrectly to begin with, (2) your applescript is accidentally trashing the formatting while you process it, or (3) there's a bug in 10.6 that I don't know about. you've excluded 1, 3 is unlikely, so that leaves 2. post your script again, this time inside code tags (the word code inside curly braces - one at the beginning of the script and one at the end. that's how you make those gray boxes), and explain what you're doing in a bit more detail, please.

Oct 31, 2009 11:50 AM in response to twtwtw

OK, sorry. So I dictate medical reports in Macspeech Dictate. I do a bunch at a time. I have to dictate a report to Dr. Jones, so I want to insert using address book:

Joe Jones, M.D.
1234 1st St.
Kansas City, KS 66111

then I dictate the letter to Dr. Jones, then I dictate the next letter to Dr. Smith, etc.
at the end of the day's dictation I have a single document that has ~20 medical reports. I have a macro that I've used for 8 yrs in MS Word to split the letters up, save them in individual files, print them properly formatted on letterhead. I recently have switched to Pages & have developed an applescript to do the same thing. I could dictate, save, print letters one at a time, then I could use the script the way you originally wrote it, but my workflow is more efficient to do it all at once. I guess I'm stuck in my ways, but, again, I think it works better to finish the dictation for the whole day and let my macro save, format, and print all the reports at once. So that's why I want to put the addresses in an already made document.

So here's the code I'm using:
(I added in 2 lines of code to save the selected addresses in a group "envelope" so I can drag & drop them into a Pages mail-merge document to print out the envelopes for the day's dictation)


tell application "Address Book"
--the next 2 lines are to store selected addresses in group envelope that I can drag and drop into a pages merge document to print out all the envelopes for the day's dictation
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 theStreet to street of first address of thisPerson
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 "Macspeech Dictate Medical"
tell application "System Events"
keystroke theName
key code 36
keystroke theStreet
key code 36
keystroke CSZ
end tell


I ran this code on my macbook pro running 10.5.8 and my imac running 10.5.8. I made a brand new entry in the address book using a multiline street address and it doesn't put the line breaks. Otherwise, this macro works great. Weird, huh!

Thanks for your help & patience.

Oct 31, 2009 12:16 PM in response to jamesabomb

Oh, & I also tried running it in Text Edit, same error
Here's what I get with a brand new entry in the address book:

new guy
12234 streetmailstop joe1djk
blah, ks 66220

I get this when I try running it using formatted address property instead of splitting the address up into sections (street, city, state, zip):

new guy
12234 streetmailstop joe1djkblaha ksa 66220

again, I'm aiming for :

new guy
12234 street
mailstop joe1djk
blah, ks 66220

thanks

Oct 31, 2009 3:02 PM in response to jamesabomb

ok, I think I get it. try this code variant:
tell application "Address Book"
--the next 2 lines are to store selected addresses in group envelope that I can
--drag and drop into a pages merge document to print out all the envelopes for the day's
add the selection to the group "envelope"
save addressbook
set theContacts to the selection
repeat with thisPerson in theContacts
set FormattedAddress to ""
set FormattedAddress to FormattedAddress & name of thisPerson & return
set FormattedAddress to FormattedAddress & street of (first address of thisPerson) & return
set FormattedAddress to FormattedAddress & city of first address of thisPerson & ", "
set FormattedAddress to FormattedAddress & state of first address of thisPerson & ", "
set FormattedAddress to FormattedAddress & zip of first address of thisPerson
-- my writetomacspeech(FormattedAddress)
end repeat
end tell
writetomacspeech(FormattedAddress)
on writetomacspeech(theAddress)
activate application "Macspeech Dictate Medical"
tell application "System Events"
keystroke theAddress
end tell
end writetomacspeech

There are two changes I made here. first, I build the entire formatted string in applescript (rather than trying to do it through system events in the unscriptable app). That way you can just do one keystroke event; Applescript should handle the returns better this way. if you still have trouble, try changing the word 'return' in lines 9 and 10 to 'linefeed', see if that makes a difference.

second, I put the bit where you write it to macspeech into a subroutine. I did that because it seemed more correct to do the writing bit from within the repeat loop rather than at the end. that way if you have multiple people you're processing it will write the address out for each (rather than merely writing out the address for the last, which is way the script plays out now). the active write tomacspeech call just duplicates what you had before; the commented out call three lines above is where you probably want it to be.

Oct 31, 2009 5:55 PM in response to jamesabomb

you have to look at the code yourself, or you're never going to get anywhere with this. In this case I accidentally put a comma on the city line; just remove it.

frankly, I'm at the limit of what I can do to help you here. I don't see the problem on my end (as I said, applescript captures the line breaks perfectly on my system), so this must be something to do with Macspeech Dictate Medical, which is not an application I have or can test. the only thing I can suggest (and this is a pure shot in the dark) is to split and rebuild the multi-line streets manually. that would look like this - replace this line
set FormattedAddress to FormattedAddress & street of (first address of thisPerson) & return
with these lines
set x to to paragraphs of ( street of (first address of thisPerson) )
set {tid, applescript's text item delimiters} to {applescript's text item delimiters, return}
set x to x as text
set applescript's text item delimiters to tid
set FormattedAddress to FormattedAddress & x & return

any way it goes, though, you're going to have to figure out the problem on your end, because I literally can't see it. you know it has to do with line endings, you know that means carriage returns vs linefeeds - you may have to look at the text character by character in applescript to see what's happening.

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.