[564] | 1 | from appscript import *
|
---|
[566] | 2 | from datetime import datetime
|
---|
| 3 | from osax import *
|
---|
| 4 | from plistlib import readPlist, writePlist
|
---|
| 5 | import os
|
---|
[564] | 6 | import re
|
---|
[566] | 7 | import time
|
---|
[564] | 8 |
|
---|
[566] | 9 | PREFERENCES_PATH = \
|
---|
| 10 | os.path.expanduser('~/Library/Preferences/net.sabi.UpdateDates.plist')
|
---|
| 11 |
|
---|
[564] | 12 | DATE_FORMATS = (('%m/%d/%y', r'\d{1,2}/\d{1,2}/\d{1,2}' ), # T-Mobile
|
---|
| 13 | ('%m.%d.%y', r'\d{1,2}\.\d{1,2}\.\d{1,2}' ), # iFixit
|
---|
| 14 | ('%b %d, %Y', r'[A-Z][a-z][a-z] \d{1,2}, \d{4}'), # AmerenIP
|
---|
| 15 | ('%B %d, %Y', r'[A-Z][a-z]+ ?\d{1,2}, ?\d{4}' ), # Amazon
|
---|
| 16 | ('of %Y%m%d', r'of \d{8}' ), # Amazon
|
---|
| 17 | ('%m/%d/%Y', r'\d{1,2}/\d{1,2}/\d{4}' ), # Busey
|
---|
| 18 | ('%b %d %Y', r'[A-Z]{3} \d{1,2} \d{4}' ), # State Farm
|
---|
| 19 | ('%d %b %Y', r'\d{1,2} [A-Z][A-Za-z]{2} \d{4}'), # Apple
|
---|
| 20 | ('%Y-%m-%d', r'\d{4}-\d{2}-\d{2}' ), # MacSpeech
|
---|
| 21 | ('%Y-%m', r'\d{4}-\d{2}' ), # filename
|
---|
| 22 | ('%m1%d/%y', r'\d{1,2}1\d{1,2}/\d{1,2}' ), # T-Mo bad OCR
|
---|
| 23 | ('%m/%d1%y', r'\d{1,2}/\d{1,2}1\d{1,2}' ), # T-Mo bad OCR
|
---|
| 24 | ('%m/%d/%y', r'\d{1,2}/ \d{1,2}/ \d{1,2}' ), # T-Mo bad OCR
|
---|
| 25 | ('%m/%d/%Y',
|
---|
| 26 | r'(?:\d ?){1,2}/ (?:\d ?){1,2}/ (?:\d ?){4}' ), # Busey bad OCR
|
---|
| 27 | )
|
---|
| 28 |
|
---|
| 29 | RE_DATE = re.compile('|'.join(r'(\b%s\b)' % regex
|
---|
| 30 | for format, regex in DATE_FORMATS))
|
---|
| 31 |
|
---|
| 32 | def extract_date(contents, match=None):
|
---|
| 33 | no_match = []
|
---|
| 34 | for m in RE_DATE.finditer(contents):
|
---|
| 35 | matched_format = m.lastindex
|
---|
| 36 | format = DATE_FORMATS[matched_format - 1][0]
|
---|
| 37 | # note: spaces in strptime format match zero or more spaces, this is OK
|
---|
| 38 | matched = m.group(matched_format).replace(' ', '')
|
---|
| 39 | try:
|
---|
[566] | 40 | parsed = datetime.strptime(matched, format)
|
---|
[564] | 41 | except ValueError, e: # not a date
|
---|
| 42 | no_match.append((matched, format, e))
|
---|
| 43 | continue
|
---|
| 44 | if not match or (match.year, match.month) == (parsed.year, parsed.month):
|
---|
| 45 | return parsed.date(), no_match
|
---|
| 46 | no_match.append(m.group(matched_format))
|
---|
| 47 | return None, no_match
|
---|
| 48 |
|
---|
[566] | 49 | def extract_source(title, hint):
|
---|
| 50 | if hint:
|
---|
| 51 | return title[:RE_DATE.search(title).start(0)].rstrip()
|
---|
| 52 | else:
|
---|
| 53 | return title
|
---|
| 54 |
|
---|
[564] | 55 | EagleFiler = app(id='com.c-command.EagleFiler')
|
---|
| 56 | Paper = EagleFiler.documents['Paper.eflibrary']
|
---|
| 57 |
|
---|
[566] | 58 | sources = os.path.exists(PREFERENCES_PATH) and \
|
---|
| 59 | readPlist(PREFERENCES_PATH).get('Sources', []) or []
|
---|
[564] | 60 |
|
---|
[566] | 61 | def update_all():
|
---|
| 62 | for record in Paper.library_records[its.kind=='PDF']():
|
---|
| 63 | title = record.title()
|
---|
| 64 | hint, no_match = extract_date(title)
|
---|
| 65 | source = extract_source(title, hint)
|
---|
| 66 |
|
---|
| 67 | contents = record.contents()
|
---|
| 68 | if re.search(re.escape(source), contents, re.IGNORECASE):
|
---|
| 69 | if source in sources:
|
---|
| 70 | sources.remove(source)
|
---|
| 71 | sources.append(source)
|
---|
| 72 |
|
---|
| 73 | extracted, no_match = extract_date(contents, hint)
|
---|
| 74 |
|
---|
| 75 | if not extracted:
|
---|
| 76 | print title, hint
|
---|
| 77 | for nm in no_match:
|
---|
| 78 | print ' no match', nm
|
---|
| 79 | if not hint:
|
---|
| 80 | continue
|
---|
| 81 |
|
---|
| 82 | record.creation_date.set(extracted or hint)
|
---|
| 83 |
|
---|
| 84 | sources.reverse() # most recently referenced ones at top
|
---|
| 85 |
|
---|
| 86 | def scan_one():
|
---|
| 87 | Acrobat = app(id='com.adobe.Acrobat.Pro')
|
---|
| 88 | SystemEvents = app(id='com.apple.systemevents')
|
---|
| 89 | acro_process = SystemEvents.application_processes[u'Acrobat']
|
---|
| 90 |
|
---|
| 91 | filename = datetime.now().strftime('Scanned Document %y%m%d %H%m%S')
|
---|
| 92 |
|
---|
| 93 | SA = ScriptingAddition()
|
---|
| 94 | SA.activate()
|
---|
| 95 | try:
|
---|
| 96 | while True:
|
---|
| 97 | result = SA.display_dialog('How many pages do you wish to scan?',
|
---|
| 98 | buttons=['Cancel', 'Scan'],
|
---|
| 99 | cancel_button=1, default_button=2,
|
---|
| 100 | default_answer='1')
|
---|
| 101 | try:
|
---|
| 102 | pages = int(result[k.text_returned])
|
---|
| 103 | except ValueError:
|
---|
| 104 | continue
|
---|
| 105 | if pages > 0:
|
---|
| 106 | break
|
---|
| 107 | except CommandError:
|
---|
| 108 | return False
|
---|
| 109 |
|
---|
| 110 | Acrobat.activate()
|
---|
| 111 |
|
---|
| 112 | acro_process.menu_bars[1].menu_bar_items['Document'].menus[1].\
|
---|
| 113 | menu_items['Scan to PDF...'].click()
|
---|
| 114 | acro_process.windows['Acrobat Scan'].buttons['Scan'].click()
|
---|
| 115 |
|
---|
| 116 | # pause (Carbon -> Cocoa? use keystrokes instead?)
|
---|
| 117 | acro_process.windows['Save Scanned File As'].text_fields[1].value.\
|
---|
| 118 | set(filename)
|
---|
| 119 | acro_process.windows['Save Scanned File As'].buttons['Save'].click()
|
---|
| 120 |
|
---|
| 121 | acro_scan_window = acro_process.windows['Acrobat Scan']
|
---|
| 122 |
|
---|
| 123 | while True:
|
---|
| 124 | acro_process.windows['DSmobile 600'].buttons['Scan'].click()
|
---|
| 125 | while not acro_scan_window.exists():
|
---|
| 126 | time.sleep(0.1)
|
---|
| 127 |
|
---|
| 128 | pages -= 1
|
---|
| 129 |
|
---|
| 130 | if pages == 0:
|
---|
| 131 | acro_scan_window.groups[1].radio_buttons[2].click()
|
---|
| 132 | acro_scan_window.buttons['OK'].click()
|
---|
| 133 | break
|
---|
| 134 |
|
---|
| 135 | acro_scan_window.groups[1].radio_buttons[1].click()
|
---|
| 136 | acro_scan_window.buttons['OK'].click()
|
---|
| 137 |
|
---|
| 138 | scanned_document = Acrobat.documents['%s.pdf' % filename]
|
---|
| 139 | scanned_file = scanned_document.file_alias()
|
---|
| 140 | scanned_document.close()
|
---|
| 141 |
|
---|
| 142 | record = Paper.import_(files=[scanned_file])[0]
|
---|
[564] | 143 | contents = record.contents()
|
---|
[566] | 144 | m = re.search('(%s)' % '|'.join(map(re.escape, sources)), contents,
|
---|
| 145 | re.IGNORECASE)
|
---|
| 146 | if m:
|
---|
| 147 | # use the saved source's case
|
---|
| 148 | title = sources[map(str.lower, sources).index(m.group(1).lower())]
|
---|
| 149 | else:
|
---|
| 150 | title = '???'
|
---|
[564] | 151 |
|
---|
[566] | 152 | extracted, no_match = extract_date(contents)
|
---|
| 153 | if extracted:
|
---|
| 154 | title += extracted.strftime(' %Y-%m')
|
---|
| 155 | record.creation_date.set(extracted)
|
---|
[564] | 156 |
|
---|
[566] | 157 | record.title.set(title)
|
---|
| 158 |
|
---|
| 159 | return True
|
---|
| 160 |
|
---|
| 161 | # update_all()
|
---|
| 162 |
|
---|
| 163 | # XXX incremental source recording from EagleFiler (use tag to record)
|
---|
| 164 |
|
---|
| 165 | while scan_one():
|
---|
| 166 | pass
|
---|
| 167 |
|
---|
| 168 | writePlist({'Sources': sources}, PREFERENCES_PATH)
|
---|