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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #!/usr/bin/env python
# encoding: utf-8
"""
mailapp.py
Created by Fabian Schächter on 2010-03-31.
Copyright (c) 2010 . All rights reserved.
"""
__author__ = 'Fabian Schächter <fabian@schaechter.info>'
__version__ = '0.1'
import optparse
import appscript
import mactypes
import os
def talk_to_mailapp(recipients, subject, files):
"""talks to mail.app through appscript"""
mail = appscript.app('Mail')
k = appscript.k
message = mail.make(
new=k.outgoing_message,
with_properties={
k.visible: True,
k.subject: unicode(subject),
}
)
for recipient in recipients:
message.to_recipients.end.make(
new=k.to_recipient,
with_properties={
k.address: recipient,
}
)
if files is not None and len(files) > 0:
for f in files:
message.content.make(
at=message.content.paragraphs.first.before,
new=k.paragraph,
with_data='nn',
)
file_path = os.path.realpath(f)
app_file = mactypes.Alias(file_path)
message.content.make(
at=message.content.paragraphs.first.after,
new=k.attachment,
with_properties={
k.file_name: app_file,
}
)
def main():
"""main"""
parser = optparse.OptionParser()
parser.add_option(
'-t',
'--to',
dest='recipient',
action='append',
help='recipient'
)
parser.add_option(
'-s',
'--subject',
dest='subject',
help='subject',
)
parser.add_option(
'-f',
'--file',
dest='files',
action='append',
help='files to be added as an attachment'
)
(options, args) = parser.parse_args()
if options.recipient == None:
parser.error('recipient is required')
if options.subject == None:
parser.error('subject is required')
talk_to_mailapp(options.recipient, options.subject, options.files)
if __name__ == '__main__':
main() |