[MacRuby-devel] Basic delayed email method

Cliff Rosson cliff.rosson at gmail.com
Wed Oct 24 16:21:33 PDT 2012


So here is the lazyman's solution I came up with.

#!/usr/local/bin/macruby
> framework 'ScriptingBridge'
> ACCOUNT = "AccountName"
> DAY = Time.now.day
> MONTH = Time.now.month
> YEAR = Time.now.year
> time_range = ((Time.now - 150)..(Time.now + 300))
> @mail = SBApplication.applicationWithBundleIdentifier("com.apple.mail")
> 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|
>   tag = draft.subject.match(/\[SEND@\d\d:\d\d\]/)[0]
>   hour, minute = tag.scan(/\d\d/)
>   time = Time.local(YEAR, MONTH, DAY, hour, minute)
>   if time_range.cover?(time) && draft.recipients.length > 0
>     props = {}
>
>     props['subject'] = draft.subject.gsub(tag, '')
>     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)
>     if outgoing_msg.send
>       draft.delete
>     end
>   end
> end


And this runs in a cron every 15 min. Been using it all day successfully.
Only does plain text because I could not figure out how to migrate the rich
text formatting over.

On Tue, Oct 23, 2012 at 9:42 PM, Cliff Rosson <cliff.rosson at gmail.com>wrote:

> With Steve's help I think I have a working solution. Basically I'll run a
> macruby script as a cron job which checks my drafts folder every 15 minutes.
>
> time_range = ((Time.now - 150)..(Time.now + 300)
> #I am not sure how much lag I can expect from the job. I would assume it
> will do its work in only a few seconds but here I have given myself a few
> minutes grace.
>
> I'll create an draft email with the a tag in the subject line. "[SEND at 16:15]"
> for example. When the appropriate time comes my script will build an
> outgoing message for each draft with a time tag that matches the present
> time_range. The tag will be removed from the subject and the message will
> be sent. It will then delete the now expired draft message.
>
> What do you guys think about this? This seems like the easiest way to get
> delayed/automated emails configured in Apple Mail without much
> sophistication. There are some limitations that could be fixed, like taking
> date into account.
>
>
> On Tue, Oct 23, 2012 at 11:56 AM, Steve Clarke <steve at sclarkes.me.uk>wrote:
>
>> Re A)  I think it should be easy to transfer the content from draft to
>> outgoing message.  Don't both use MailRichText?
>>
>> B) I think you'll be stuck with that problem.  As far as I can see you
>> can't mutate an existing message into an outgoing message.
>>
>> Steve
>>
>> On 23 Oct 2012, at 19:05, Cliff Rosson <cliff.rosson at gmail.com> wrote:
>>
>> Yea putsing methods(true,true).sort is pretty useful. Below is what I
>> have compiled so far.
>>
>> framework "ScriptingBridge"
>>
>>
>>>  @mail = SBApplication.applicationWithBundleIdentifier("com.apple.mail")
>>> @my_account = @mail.accounts.select { |account| account.name ==
>>> "MyAccount" }.first
>>> @drafts_folder = @my_account.mailboxes.select { |mailbox| mailbox.name== "Drafts" }.first
>>> @draft = @drafts_folder.messages.first
>>>
>>
>>
>> def send_message(message)
>>>   props = {}
>>>
>>>   props['subject'] = message.subject
>>>   props['sender'] = message.sender
>>>   #props['content'] = message.content
>>>
>>>
>>>   outgoing_msg = @mail.classForScriptingClass('outgoing
>>> message').alloc.initWithProperties(props)
>>> @mail.outgoingMessages.addObject(outgoing_msg)
>>>   recipient =  @mail.classForScriptingClass('to
>>> recipient').alloc.initWithProperties({'address'=>message.recipients.first.address})
>>>   outgoing_msg.toRecipients.addObject(recipient)
>>>
>>>   outgoing_msg.send
>>>
>>> end
>>> send_message(@draft)
>>
>>
>> *Note I used instance variables so I could play with these objects in
>> macirb. If you were wondering...
>>
>> The code above works. I get the email. Two issues however.
>>
>> A)
>> I can't figure out how to pull the text contents of my @draft.contents
>> message. Presumably because it is a MailRichText class and not a simple
>> string object. If only somehow I could attach this contents to the outgoing
>> message to preserve the body. What would be even better is to simply create
>> my outgoing message with something like InitWithDraft so as to use a
>> drafted email already created. I suspect this will require further research
>> from me.
>>
>> @draft.content
>> => <MailRichText @0x1370760: content of MailMessage 0 of MailMailbox 19
>> of MailAccount 0 of application "Mail" (1845)>
>>
>> B)
>> The other issue is since I am not using my existing drafted email
>> creating this outgoing message actually makes a NEW draft in my Draft
>> folder. It looks like I would have to go and manually delete the draft that
>> I am attempting to copy in the method above. I believe there must be an
>> easier way to do this. This comes back to what I was talking about above by
>> creating the outgoing message from a draft. I guess I'll just have to do
>> some research to figure that out.
>>
>> Thanks Steve for your help!
>>
>>
>>
>>
>> On Tue, Oct 23, 2012 at 10:12 AM, Steve Clarke <steve at sclarkes.me.uk>wrote:
>>
>>> You need to use the ScriptingBridge documentation in Xcode - which means
>>> that you need to spend some time understanding Objective C.  I don't know
>>> it well but enough to translate examples into Macruby.
>>>
>>> I found Matt Aimonetti's book very helpful when I was starting off. It
>>> doesn't say a lot about ScriptingBridge per se, but in the end SB is just
>>> another framework you can use from Macruby.
>>>
>>> It's worth looking back at the Macruby archives because Matt wrote some
>>> very helpful stuff about using SB.  One tip I recall is that you can see
>>> what methods are available on an object in Macruby by using
>>> obj.methods(true,true).  This gives tons of stuff but critically it shows
>>> both the Macruby methods AND the Objective C methods.  With SB it's the
>>> latter group that are of real interest.
>>>
>>> Steve
>>>
>>> On 23 Oct 2012, at 17:52, Cliff Rosson <cliff.rosson at gmail.com> wrote:
>>>
>>> Perfect! How did you figure all of this out. Are there some documents
>>> that explain when it is appropriate to use things like
>>> "classForScriptingClass" etc...?
>>>
>>> On Tue, Oct 23, 2012 at 9:49 AM, Steve Clarke <steve at sclarkes.me.uk>wrote:
>>>
>>>
>>>
>>> --
>>> http://about.me/cliffrosson
>>> vizualize.me/cliffrosson
>>> _______________________________________________
>>> MacRuby-devel mailing list
>>> MacRuby-devel at lists.macosforge.org
>>> http://lists.macosforge.org/mailman/listinfo/macruby-devel
>>>
>>>
>>>
>>> _______________________________________________
>>> MacRuby-devel mailing list
>>> MacRuby-devel at lists.macosforge.org
>>> http://lists.macosforge.org/mailman/listinfo/macruby-devel
>>>
>>>
>>
>>
>> --
>> http://about.me/cliffrosson
>> vizualize.me/cliffrosson
>>  _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo/macruby-devel
>>
>>
>>
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo/macruby-devel
>>
>>
>
>
> --
> http://about.me/cliffrosson
> vizualize.me/cliffrosson
>



-- 
http://about.me/cliffrosson
vizualize.me/cliffrosson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20121024/8dd5f245/attachment-0001.html>


More information about the MacRuby-devel mailing list