-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathce.py
More file actions
executable file
·67 lines (53 loc) · 1.72 KB
/
ce.py
File metadata and controls
executable file
·67 lines (53 loc) · 1.72 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python -B
# vi: set syntax=python ts=4 sw=4 sts=4 et ff=unix ai si :
#
# ce: Mac Contacts Exporter
#
# (c) Steven Scholnick <scholnicks@gmail.com>
# The ce source code is published under a MIT license.
import glob
import os
import sqlite3
import sys
SQL = """select
R.ZLASTNAME, R.ZFIRSTNAME, P.ZSTREET, P.ZCITY, P.ZSTATE, P.ZZIPCODE
from
ZABCDRECORD R, ZABCDPOSTALADDRESS P
where
R.Z_PK = P.ZOWNER and
R.ZLASTNAME is not null and P.ZCITY is not null and P.ZZIPCODE is not null
order by
1, 2
"""
def main():
"""main method"""
with open(options.outputPath, "w") as fp:
with sqlite3.connect(getAddressDatabaseFile()) as conn:
for row in conn.cursor().execute(SQL):
printRow(fp, row)
if options.verbose:
print("{0} has been created".format(options.outputPath))
sys.exit(0)
def printRow(fileHandle, row):
"""prints a single row of data"""
print(
"""{1} {0}
{2}
{3}, {4}, {5}
""".format(
*row
),
file=fileHandle,
)
def getAddressDatabaseFile():
"""Returns the path to the addressbook SQLite database file"""
files = glob.glob(os.environ["HOME"] + "/Library/Application Support/AddressBook/Address*")
return files[0] if files else None
if __name__ == "__main__":
from optparse import OptionParser
parser = OptionParser(usage="%prog [--output PATH] [--verbose]")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="Toggles verbose")
parser.add_option("-o", "--output", dest="outputPath", type="string", help="Output filepath")
parser.set_defaults(outputPath="Addresses.txt")
options, args = parser.parse_args()
main()