Copy Addresses as Text

This is a script from July 09. Apple’s Address Book doesn’t have a way to select people and copy them as a nicely formatted address that you can paste into mail. It’s an amazingly simple script but one I use pretty regularly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python
 
## Puts the selected addresses from Address Book onto the clipboard 
## as nicely formatted text
 
from appscript import *
from AppKit import *   # Cocoa for cut and paste
import sys
 
def get_address_info():
    AB = app(u'/Applications/Address Book.app')
    addresses = AB.selection.get()
 
    lines = []
    for a in addresses:
        if a == None:
            continue
 
        lines.append( a.name.get() )
 
        if len(a.phones()) >0:
            lines.append( a.phones()[0].value())
 
        if len( a.addresses() ) > 0:
            lines.append( (a.addresses())[0].formatted_address() )
 
    pretty_print = "\n".join(lines)
 
    # Use Cocoa for cut and paste
    pboard = NSPasteboard.generalPasteboard()
    pboard.declareTypes_owner_([NSStringPboardType], None)
    pboard.setString_forType_(pretty_print, NSStringPboardType)
 
 
if __name__ == '__main__':
    get_address_info()
 
    # change to 0 for success, 1 for (partial) failure
    sys.exit(0)
  1. No comments yet.
(will not be published)