Previous 1 2 Next 24 Replies Latest reply: Nov 2, 2009 7:11 AM by twtwtw
jamesabomb Level 1 Level 1 (0 points)
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)
  • twtwtw Level 5 Level 5 (4,900 points)
    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.
  • jamesabomb Level 1 Level 1 (0 points)
    Thanks, that was great, and quick too!
  • jamesabomb Level 1 Level 1 (0 points)
    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?
  • jamesabomb Level 1 Level 1 (0 points)
    see post above
  • twtwtw Level 5 Level 5 (4,900 points)
    don't use the formatted address approach if it's causing you headaches. If you look at the address book applescript dictionary, you'll see that you can extract the pieces of the address individually and combine them as you like. play with it a bit (that's what applescript is for) and you'll get the hang of it.
  • jamesabomb Level 1 Level 1 (0 points)
    it works OK as I modified it in the script I posted, but the only bug is that it doesn't recognize multi-line street addresses. Any ideas on that?
  • twtwtw Level 5 Level 5 (4,900 points)
    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.
  • jamesabomb Level 1 Level 1 (0 points)
    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.
  • twtwtw Level 5 Level 5 (4,900 points)
    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.
  • jamesabomb Level 1 Level 1 (0 points)
    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.
  • jamesabomb Level 1 Level 1 (0 points)
    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
  • twtwtw Level 5 Level 5 (4,900 points)
    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 writetomacspeech call just duplicates what you had before; the commented out call three lines above is where you probably want it to be.
  • jamesabomb Level 1 Level 1 (0 points)
    Sorry, still not formatting the 2-line street address. I changed "new guy's" street and made sure there were hard returns in it.

    still getting:

    new guy
    1234 1st St.Apt. 2234
    blah, ks, 66220
  • jamesabomb Level 1 Level 1 (0 points)
    at least it's putting commas in instead of "a's" but, of course, there shouldn't be a comma or an "a" between state & zip. Must be some system error on my end?
Previous 1 2 Next