Changeset 566 for trunk


Ignore:
Timestamp:
07/19/09 22:32:55 (15 years ago)
Author:
Nicholas Riley
Message:

update_dates.py: Continuous scanning workflow.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Update Dates/update_dates.py

    r564 r566  
    11from appscript import *
     2from datetime import datetime
     3from osax import *
     4from plistlib import readPlist, writePlist
     5import os
    26import re
     7import time
     8
     9PREFERENCES_PATH = \
     10    os.path.expanduser('~/Library/Preferences/net.sabi.UpdateDates.plist')
    311
    412DATE_FORMATS = (('%m/%d/%y',  r'\d{1,2}/\d{1,2}/\d{1,2}'       ), # T-Mobile
     
    3038        matched = m.group(matched_format).replace(' ', '')
    3139        try:
    32             parsed = datetime.datetime.strptime(matched, format)
     40            parsed = datetime.strptime(matched, format)
    3341        except ValueError, e: # not a date
    3442            no_match.append((matched, format, e))
     
    3947    return None, no_match
    4048
     49def extract_source(title, hint):
     50    if hint:
     51        return title[:RE_DATE.search(title).start(0)].rstrip()
     52    else:
     53        return title
     54
    4155EagleFiler = app(id='com.c-command.EagleFiler')
    4256Paper = EagleFiler.documents['Paper.eflibrary']
    4357
    44 for record in Paper.library_records[its.kind=='PDF']():
    45     title = record.title()
    46     hint, no_match = extract_date(title)
     58sources = os.path.exists(PREFERENCES_PATH) and \
     59          readPlist(PREFERENCES_PATH).get('Sources', []) or []
    4760
     61def 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
     86def 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]
    48143    contents = record.contents()
    49     extracted, no_match = extract_date(contents, hint)
     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 = '???'
    50151
    51     if not extracted:
    52         print title, hint
    53         for nm in no_match:
    54             print '  no match', nm
    55         if not hint:
    56             continue
     152    extracted, no_match = extract_date(contents)
     153    if extracted:
     154        title += extracted.strftime(' %Y-%m')
     155        record.creation_date.set(extracted)
    57156
    58     record.creation_date.set(extracted or hint)
     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
     165while scan_one():
     166    pass
     167
     168writePlist({'Sources': sources}, PREFERENCES_PATH)
Note: See TracChangeset for help on using the changeset viewer.