The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   read the number of attachments in mail using applescript? (http://hintsforums.macworld.com/showthread.php?t=27593)

MasterSwitch 09-05-2004 08:59 AM

Quote:

Originally Posted by rusto
If you don't have a G5, you are doing 32 bit processing. In fact, those of us WITH G5s are pretty much doing 32 bit right now as well. But not to worry, I believe Tiger is slated to work with pretty much the same machines that Jaguar does...but that is a topic for another thread.

hmmmm do you have a link for the other thread that you could post in this one?

rusto 09-05-2004 09:59 AM

Here's one

Here's the other

bramley 09-05-2004 01:47 PM

Quote:

Originally Posted by MasterSwitch
hi bramley

finally got round to putting your code in a script, first problem and its one i dont know a way around,
attachments
should i assume be a command, ie, in blue if you use default colors

Well, I'm flabbergasted! Just to check - in Mail dictionary, attachment (in Panther) is the first item under "Text Suite".

Don't forget I only said it was very probable, not highly probable. :)

jbc 09-05-2004 02:33 PM

Finally dumped out an mbox with lots of attachments, then sliced and diced in Terminal to see what was happening. Main problem seems to be an inconsistency about how different clients put together email headings; I doubt you will ever get an accurate count of how many files Mail will extract without examining each message separately.

The good news is you don't really need a full count, and our checks *are* actually working! Turns out the result of this expression

grep -i "name=\".*\"" "/path/to/mbox"

was all "name=" tags from the Content-Type headers and all "filename=" tags from the Content-Disposition headers, 445 total...and nothing else (I was afraid it would match attributes in html parts). If *any* of these are matched, it should indicate an attachment that Mail can save. So you should be able to do a test like

tcount=`grep -i -c "name=\".*\"" "/path/to/mbox"`
if test $tcount -ne 0
then
#run save script for mail box here
fi

or in Applescript...

set tcount to do shell script "grep -i -c \"name=\\\".*\\\"\" \"/path/to/mbox\""
if tcount is not "0" then
--run save script for mail box here
end if

Btw, the regular expression "name=\".*\"" is simply looking for "name=" followed by zero or more characters surrounded by double-quotes, which is why it also finds "filename="???"".

Best regards,
Brad

MasterSwitch 09-05-2004 08:27 PM

1 Attachment(s)
Quote:

Originally Posted by bramley
Well, I'm flabbergasted! Just to check - in Mail dictionary, attachment (in Panther) is the first item under "Text Suite".

Don't forget I only said it was very probable, not highly probable. :)

hi bramley

well when i looked before i didnt see anything on attachments
there is as you say an attachment section
ill play around with the commands that are listed tomorrow, though there is no obvious command to read the number of attachments
anyway, ive attached a screen shot of the dictionary

MasterSwitch 09-05-2004 08:44 PM

Quote:

Originally Posted by jbc
I doubt you will ever get an accurate count of how many files Mail will extract without examining each message separately.

The good news is you don't really need a full count, and our checks *are* actually working! Turns out the result of this expression
or in Applescript...

set tcount to do shell script "grep -i -c \"name=\\\".*\\\"\" \"/path/to/mbox\""
if tcount is not "0" then
--run save script for mail box here
end if

Best regards,
Brad

actually ive known i dont NEED an acurate read for some time, i wouldnt check for 0, i would check for 20 or something and hope that there was at least one downloadable attachment in amonst them.
that wasnt really the point, the point was we were trying to get an accurate read, and it was fun trying to do it
plus if it was accurate, the script would never fail, with an aprox count, there will be very rare occationsions when it will
sorry its just the perfectionist in me, im like a lion with a kill, i wont let it go till ive had my fill :D

my web sites are like that too and i get told off for it, they tell me if its working dont try to fix all the bugs, just move on to the next one, but i like to build sites with no bugs, then i dont have to find out what some idiot has done later to make the site fail

ho hum, anyway, if you have any other thoughts i would be happy to hear and test them.....
ive had jaguar now for about 2 years i think
at the start i was told you could not wake a computer from energy saver sleep with actionscript, 2 years later im reporting and bouncing spam while im asleep :D

o/s x is much more powerful than o/s 9
there probably is an answer, we just havent found it yet

sweet dreams
hitting the hay

jbc 09-05-2004 09:44 PM

Quote:

Originally Posted by MasterSwitch
actually ive known i dont NEED an acurate read for some time, i wouldnt check for 0, i would check for 20 or something and hope that there was at least one downloadable attachment in amonst them.
that wasnt really the point, the point was we were trying to get an accurate read, and it was fun trying to do it

Well, if you want to go for the gold, you'd probably be best off looking at something like a Perl script. I think the headers I'm suggesting are an accurate indicator of a savable attachment; the problem is that a particular message or MIME part may contain only one or both of these. So you need to check on a per message and per MIME part basis which headers are there to get an accurate count. A Perl script can be made to pick out the messages and parts; this isn't really possible with something like grep.

You might also be able to tackle this with procmail, I'm not sure (not much of a Perl or procmail expert).

Alternatively, you could set up a mail server (MTA) to deal with the attachments when they arrive with the email. My exim server writes the extension of each attachment into an additional header, so I can see how many and what type of attachments are there from looking at the log, before ever opening the email.

It's certainly possible, but may be a lot of work!

MasterSwitch 09-07-2004 09:23 AM

Quote:

Originally Posted by jbc
Well, if you want to go for the gold, you'd probably be best off looking at something like a Perl script.
You might also be able to tackle this with procmail, I'm not sure (not much of a Perl or procmail expert).
It's certainly possible, but may be a lot of work!

hi brad

ok, i guess we just have to leave it as aprox, at least until i get panther or tiger, or one of those commands for mail as dictionary attachments actually reads the number.
i dont mind work, but theres no point in doing a lot of work for something that kinda works, at least i can do some sort of test :)
as for perl, i used to have it working in o/s 9, but ive never managed to work out how to use it in o/s 10, or php for that matter :(

thx again


All times are GMT -5. The time now is 05:46 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.