applescript for address book
MacBook Pro, Mac OS X (10.5.8)
Apple Event: May 7th at 7 am PT
MacBook Pro, Mac OS X (10.5.8)
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
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
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
1: 1
2: 2
3:
4: 1
5: s
6: t
7:
8: a
9: p
10: t
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
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
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
set theStreetArray to paragraphs of (get street of first address of thisPerson)
that will work, and it won't fail if you have street addresses that are more than three lines. but that's a minor thing, really. glad you have something you can use.applescript for address book