-
-
Save crosson/4470417 to your computer and use it in GitHub Desktop.
This was a script I wrote to send delayed emails from macmail. The script is run in a cron every 15 minutes The script checks for items in the Mail draft folder that have a [SEND@XYZ] tag. For example, [SEND@16:00] The script checks if a drafted emails SEND@ tag matches or is near the current time. If so a new email is drafted, See step 4. The n…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/macruby | |
framework 'ScriptingBridge' | |
ACCOUNT = "AccountName" | |
DAY = Time.now.day | |
MONTH = Time.now.month | |
YEAR = Time.now.year | |
CURRENT_TIME = ((Time.now - 150)..(Time.now + 300)) | |
def draft_tag(draft) | |
draft.subject.match(/\[SEND@\d+:\d+\]/)[0] | |
end | |
def send_now?(draft) | |
tag = draft_tag(draft) | |
hour, minute = tag.scan(/\d+/) | |
time = Time.local(YEAR, MONTH, DAY, hour, minute) | |
CURRENT_TIME.cover?(time) && draft.recipients.length > 0 | |
end | |
def build_new_draft(draft, mail = @mail) | |
props = {} | |
props['subject'] = draft.subject.gsub(draft_tag(draft), '') | |
props['sender'] = draft.sender | |
props['content'] = draft.content.get | |
outgoing_msg = mail.classForScriptingClass('outgoing message').alloc.initWithProperties(props) | |
mail.outgoingMessages.addObject(outgoing_msg) | |
recievers = "" | |
draft.recipients.each do |recip| | |
recievers << "%s," % recip.address | |
end | |
recipient = mail.classForScriptingClass('to recipient').alloc.initWithProperties({'address'=>recievers}) | |
outgoing_msg.toRecipients.addObject(recipient) | |
outgoing_msg | |
end | |
@mail = SBApplication.applicationWithBundleIdentifier("com.apple.mail") | |
if @mail.isRunning | |
my_account = @mail.accounts.select { |account| account.name == ACCOUNT }.first | |
drafts_folder = my_account.mailboxes.select { |mailbox| mailbox.name == "Drafts" }.first | |
delayed_messages = drafts_folder.messages.select { |x| x.subject.match(/\[SEND@\d\d:\d\d\]/) } | |
delayed_messages.each do |draft| | |
if send_now?(draft) | |
outgoing_msg = build_new_draft(draft) | |
if outgoing_msg.send | |
draft.delete | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://plus.google.com/hangouts/_/be482ce407d2dafb65672b5485b5b69632eaac9d?authuser=0&hl=en is a link where you can get together (it is persistent).