[Pythonmac-SIG] appscript and mail.app, how to access attachments?

Ned Deily nad at acm.org
Tue Oct 5 11:05:02 CEST 2010


In article 
<AANLkTim-Sb=uw2OPE7Y7iGyq_hxokunp9_SUdosJW1TR at mail.gmail.com>,
 Christian Prinoth <christian at prinoth.name> wrote:
> I am trying the following:
> 
>  mail=app('mail')
>  msgs=mail.selection()
>  msg=msgs[0]
> 
> now I would like to use
>  msg.mail_attachments
> but this won't work. If I ask for help, under elements I get the following:
> 
> Elements:
>     AEType("attc") -- by name, index, relative, range, test, id
>     bcc_recipients -- by name, index, relative, range, test
>     cc_recipients -- by name, index, relative, range, test
>     headers -- by name, index, relative, range, test
>     recipients -- by name, index, relative, range, test
>     to_recipients -- by name, index, relative, range, test
> 
> How do I access the attachments?

I'm not a Appscript expert but, taking a clue from an earlier answer[1] 
by has, it appears that there is something problematic about the 10.6 
Mail.app scripting terminology.  You can see also see that by trying to 
the ASTranslate.app tool available from the appscript web site.

One way to work around it is to use the lower-level aem API to construct 
a reference using the raw AE codes.

>>> from appscript import *
>>> import aem
>>> mail = app("Mail")
>>> msg = mail.selection()[0]
>>> msg
app(u'/Applications/Mail.app').mailboxes[u'In/InMail'].messages.ID(206990
)
>>> aemref = msg.AS_aemreference.property("attc")
>>> attachment = mail.AS_newreference(aemref)
>>> attachment
app(u'/Applications/Mail.app').AS_newreference(app.elements('mbxp').bynam
e(u'In/InMail').elements('mssg').byid(206990).property('attc'))
>>> attachment()[0].properties()
{k.name: u'20100925_005-001.jpg', aem.AEType('attp'): u'image/jpeg', 
aem.AEType('atdn'): True, k.class_: aem.AEType('attc'), 
aem.AEType('atsz'): 387441, k.id: u'2'}

Hope that helps.

[1] http://permalink.gmane.org/gmane.comp.python.apple/14658

-- 
 Ned Deily,
 nad at acm.org



More information about the Pythonmac-SIG mailing list