def send_mail(recipients, subject, body, attach_file_path): emails = [] address_book = contacts.address_book_to_list() for recip in recipients: if '@' in recip: emails.append(recip) else: result = contacts.find_contact(recip, address_book, "email") if result: emails.append(result['email'][0]) recipient_lines = [] for email in emails: recipient_lines.append("make new to recipient with properties {address:%s}"%(asquote(email))) file_command = u"make new attachment with properties {file name: (POSIX file %s as alias)} at after last paragraph of content"%(asquote(attach_file_path)) if attach_file_path else "" script = u""" tell application "Mail" activate set mgMessage to make new outgoing message with properties {subject:%s, content:%s, visible:true} tell mgMessage %s %s end tell end tell """%(asquote(subject), asquote(body), "\n".join(recipient_lines), file_command) print script asrun(script)
def send_mail(recipients, subject, body, attach_file_path): emails = [] address_book = contacts.address_book_to_list() for recip in recipients: if '@' in recip: emails.append(recip) else: result = contacts.find_contact(recip, address_book, "email") if result: emails.append(result['email'][0]) recipient_lines = [] for email in emails: recipient_lines.append( "make new to recipient with properties {address:%s}" % (asquote(email))) file_command = u"make new attachment with properties {file name: (POSIX file %s as alias)} at after last paragraph of content" % ( asquote(attach_file_path)) if attach_file_path else "" script = u""" tell application "Mail" activate set mgMessage to make new outgoing message with properties {subject:%s, content:%s, visible:true} tell mgMessage %s %s end tell end tell """ % (asquote(subject), asquote(body), "\n".join(recipient_lines), file_command) print script asrun(script)
def send_message(recipient, body, attach_selected_files): buddy = None if normalize_phone(recipient): buddy = normalize_phone(recipient) else: address_book = contacts.address_book_to_list() result = contacts.find_contact(recipient, address_book, "phone") if result: buddy = result["phone"][0] if not buddy: asrun( 'display notification %s with title "Limelight"' % (asquote("Couldn't find iMessage contact for %s." % recipient)) ) return set_selected_files = ( """ tell application "Finder" set selectedFiles to selection end tell """ if attach_selected_files else "set selectedFiles to {}" ) script = """ %s using terms from application "Messages" tell application "Messages" activate set targetService to 1st service whose service type = iMessage set targetBuddy to buddy %s of targetService send %s to targetBuddy repeat with theFile in selectedFiles send (theFile as alias) to targetBuddy end repeat end tell end using terms from """ % ( set_selected_files, asquote(buddy), asquote(body), ) print script asrun(script)
def send_message(recipient, body, attach_selected_files): buddy = None if normalize_phone(recipient): buddy = normalize_phone(recipient) else: address_book = contacts.address_book_to_list() result = contacts.find_contact(recipient, address_book, "phone") if result: buddy = result['phone'][0] if not buddy: asrun("display notification %s with title \"Flashlight\""%(asquote("Couldn't find iMessage contact for %s."%recipient))) return set_selected_files = """ tell application "Finder" set selectedFiles to selection end tell """ if attach_selected_files else "set selectedFiles to {}" script = """ %s using terms from application "Messages" tell application "Messages" activate set targetService to 1st service whose service type = iMessage set targetBuddy to buddy %s of targetService send %s to targetBuddy repeat with theFile in selectedFiles send (theFile as alias) to targetBuddy end repeat end tell end using terms from """%(set_selected_files, asquote(buddy), asquote(body)) print script asrun(script)