![]() |
read the number of attachments in mail using applescript?
hi guys
is there a way to read the number of attachments in a mail box using applescript or the shell command? alternatively maybe the size of the selected messages? thx clive :rolleyes: |
This depends on what mail client you're using and how it stores email messages. And using Applescript will depend on that client's Applescript library, if any. If your client uses standard mbox files you should in theory be able to get a count of all attachments in a mailbox using something like
grep -c "^Content-Disposition:\s+attachment" "/path/to/mboxfile" If your messages are stored as separate messages, as for my email client, you'll probably need to pipe the output of a cli "find" command for your mail box folder into grep -c. |
Quote:
im using mail 1.2.5 sorry i didnt include this info before, i posted really late and was ready to crawl into bed :) unfortuantely it doesnt seem to work at least in the terminal window, and if it doesnt work in there im sure it wont work in my action script. i know its the right path because i just dragged and dropped the path from the finder into the terminal window. i tried targeting the .mbox file and the parent folder, it didnt like either of them. unfortunately ive never used the grep command so the code youve given is greek to me :( but thx for trying clive |
Ahh. I'll back up a bit.
Firstly, Mail.app's ".mbox" files are not standard mbox files; they're a special type of folder that contains other files that are usually hidden. If you click on a ".mbox" file with the control key down and select "Show Package Contents" from the contextual menu, you can see the contents of the folder. Fortunately one of the items a ".mbox" file contains *is* a standard mbox file, called just "mbox", that contains the messages for that particular mail box. So for your path you'll need to add "/mbox" to the end of the path to search the actuall mbox file. The command line itself...grep is a utility for searching for lines within files that match a certain pattern. Normally it returns the lines that match. Had it a bit wrong, but here's basically what the line contains. command: grep options: -c (count matches instead of returning matches) pattern: "^Content-Disposition:[ ]*attachment" (email header for an attachment part - note correction to handing of possible multiple spaces) files to search: "/Users/clive/Library/Mail/Mailboxes/Drafts.mbox/mbox" If you try to put this inside an Applescript 'do shell script "..."', you'll need to escape the double-quotes by including a backslash or two before them. The grep line works okay for me run through the contextual menu. |
Masterswitch,
The following code will give you the number of attachments for a message in Mail 1.3.9. It'll probably work in Mail 1.2.5. Code:
tell application "Mail" |
If I understand the question, MasterSwitch is trying to get the number of attachments in a mail box, not an individual message. Don't have access to OS X or Mail's dictionary at the moment, but I think that means you need a "repeat with i from 1 to number of messages in mailbox" loop or some such. Then you can just add the individual counts together as you go along.
|
[QUOTE=jbc]
The command line itself...grep is a utility for searching for lines within files that match a certain pattern. Normally it returns the lines that match. Had it a bit wrong, but here's basically what the line contains. command: grep options: -c (count matches instead of returning matches) pattern: "^Content-Disposition:[ ]*attachment" (email header for an attachment part - note correction to handing of possible multiple spaces) files to search: "/Users/clive/Library/Mail/Mailboxes/Drafts.mbox/mbox" QUOTE] hi jbc grep -c "^Content-Disposition:[ ]*attachment" "/Users/clive/Library/Mail/Mailboxes/Drafts.mbox/mbox" this almost works, it got 1 out of 3 mailboxes right this is not the first time unix commands dont seem to give the expected result i have an uptime command which someone gave me, it is suposed to cut lots of crap just leaving the up hours or days, it only works about half the time. before i got this command i worked on my own commands, i built a calender alowing for leap years etc and stored the login time in a text file. so sometimes my applescript hours matches the uptime command, sometimes i get gibberish with the uptime command. im not really surpised the command isnt working properly, unix is a very complex language to get right :( ill have another look at my attachments against the command count tomorrow, and the applescript above btw, it was a total count i wanted :) thx and nite all clive :eek: |
[QUOTE=MasterSwitch]
Quote:
|
clive-
Had a thought on why your counts might be off with the grep search: we may be keying in on the wrong thing with the grep search. The Content-Disposition header is where a file is declared as "attachment" or "inline". So what if a jpeg is "inline"? Or what about the case where a background gif is referenced in html by cid and has no Content-Disposition as all? I'm guessing you'd still consider both of these "attachments". As a first approximation, you might try a search like egrep -c -i "^(.*;)?[[:space:]]+name=\".*\"" "/Users/clive/Library/Mail/Mailboxes/Drafts.mbox/mbox" This will catch even the gifs that are only referenced by cid (it tries to match the name tag in the Content-Type header). If you're not concerned about these, you could change "name" to "filename": egrep -c -i "^(.*;)?[[:space:]]+filename=\".*\"" "/Users/clive/Library/Mail/Mailboxes/Drafts.mbox/mbox" This is approximate because grep does linewise matching, so you can't match over multiple lines. You could use something like perl to do a more specific search. I've upgraded my grep to the latest version of GNU grep, but hopefully these will work with the stock OSX grep as well. |
Quote:
ive not tried this yet but wanted to say i dont sit here and open each email and count each attachment and im awear that there are attachments that arnt obvious i have been using the attachment tab to see how many attachments should be in a mailbox and comparing that way. i dont know if its worth counting gifs, sometimes the attachments i want to save are gifs, but then of course they dont take up as much room as the jpgs and other larger attachments can take. what i want is a script that will read the downloadable attachments, for a very simple reason. i have a script that will do the download, but if there are only emails in that inbox that dont have any attachments, the script fails. its so my hard disk doesnt get full when i go away for a break. so when i tell my script to click the menu item "download attahcments" that menu item has to be highlighted, it wont if there are no attachments. btw, what is egrep? |
Quote:
Unmatched ". however to me it looks like there are the right number of open and closed inverted comma's, so not really sure what would be wrong clive :confused: |
Okay, I'm officially lost. It sounds like rather than checking mailboxes on your computer, you're trying to check for attachments on the server before even downloading them to your machine, so presumably this is an IMAP account? If it's set up for POP, the attachments should have already been downloaded. Or are you talking about extracting the attachments that are in the email on your computer and saving a copy to disk somewhere?
It might be easier to try to get your Applescript to fail gracefully rather than grepping files. Seems like it might be possible to check the status of the "download attachments" menu item using System Events (I can't find this menu item in my Mail btw, but I don't use IMAP); bramley might have a better idea about this. Also if you set up a loop to process each message using bramley's model above, you could check Mail's evaluation of whether there's attachments or not and process only messages with attachments. But I'm really unclear on what you're trying to do here. egrep is the same as "grep -E", which uses extended regular expressions and allows more flexibility in pattern matching. Check "man grep" if you're curious about this. Just saw your post about the unmatched ". There should be four regular " and two escaped " (\"), so not sure what terminal is complaining about, unless it's some difference in the grep versions we're using. One final thought: if you have to worry about your hd filling up because of the size of attachments, maybe it's time to invest in a new larger hard drive? Brad |
Quote:
ok ive now tested the above code, as i said above, im interested in the number of attachments mail considers it can download from a list of emails, so in one in box i downloaded a load of attachments, mostly txt attachments from a bounced email plus a few other odds n sods. the result from the download was -- actual 152 measured 182 i did another mailbox with mostly picture type files, the result from the download was -- actual 126 measured 52 actual is the number of files resulting from the download to disk measured is the result of running the script in the terminal so im afraid that script isnt really good enough :( clive |
Quote:
Quote:
Quote:
the save attachments is right in the middle of system event statement but i have no idea how to test if that menu option is active and it would be quite complex code to exit the script at that point. also it would involve application activity to do the test. the mailbox that would get full is a special mailbox, with files that have attachments and not much else of interest are filtered. although when im here i do scan the emails to see if there is anything of interest, when im not here, downloading the attachments will be more than enough. the best solution is to activate the script when a message gets sent to that inbox, then test for attachments on all the mail in that mailbox. if there are none, simply exit the script and test again on the next download, less processing power :) Quote:
Quote:
i have to say i dont like the manual, though i guess i will get used to it eventually, i do have mac osx in a nutshell which is much more intuative :p Quote:
i have to work with what ive got for now. main disk is just 6 gig, so that is only ever going to be the space for the o/s another internal 9gig that i can startup from apparently i can put in a 40gig in its place which will cost me £70 about $110 and i have an external 40gig i would like panther, another £70 then tiger will be out next year i would like to upgrade from flash mx to 2004 professional, thats about £300 and the list goes on and on btw, im unemployed....... and no i dont like admiting that on here :( clive |
Quote:
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 in jaguar there is no attachment command. applescript reads it as a variable in green. i did actually look through the mail dictionary and try to read the attachments before posting and couldnt see an attachment or variation there of, command, before i posted :( i guess they sorted that problem in panther back to square one :( clive |
Thanks for all the detail; I have a clearer picture now. Here's a few notes for when you wake up (I assume you're asleep by now!), not necessarily in order.
The "attachment" in bramley's code *is* in blue in Panther's Script Editor, so this is apparently something that's been added to the newer version of Mail. Which means you're out of luck there, unless it runs on Jaguar as well. I reverted to OS X's native grep, and the egrep script still works; no complaints from Terminal about mismatched " either. But they could have updated the grep that ships with Panther. There's a number of tools already on your disk that should allow you to search the email headers. And it sounds as if you don't really even need a count, just a check to see if there's a single attachment in the mailbox (since you've saved and deleted the emails for the previous ones)? You might try something simple first. Say something like grep -c -i "filename=\".*\"" "/path/to/mbox" See how this matches up with Mail's counts (assuming you don't run into quote problems again). If we can identify what is a "savable" attachment for Mail, you should be in business as far as telling whether to run your script or not. You could even look at the raw messages to try to see whether Mail is queing in on the "name=" tag of the Content-Type header or the "filename=" tag of the Content-Disposition header. I don't use Mail, but if you run into difficulties, I can import an mbox from my mail client and test it here. Re: NO MONEY...you're correct, no digs intended. I'm in more or less in the same boat myself; had to scrape to upgrade to Panther. |
Quote:
Quote:
Quote:
Quote:
Quote:
Code:
grep -c -i "filename=.*" "/Users/Clive/Library/Mail/Clive/INBOX.mbox/mbox"but now youve cut it down a bit i get a bit more what your trying to do your looking for "filename" or "name" in the email headers? specificly ones with any dot ".*" extension so you could search for jpeg and do something like ".jp*" Quote:
Quote:
its talking about 64bit processing, and i dont know but i think my box is probably only capable of 32bit processing :( talk soon clive :) |
Quote:
"filename" reads 66 attachments "name" reads 88 attachments but still not 104 the actual amount downloaded :( |
Quote:
there are a few lines of code that keep popping up 88 attachments some of these emails download "image.tiff" files, however in the raw source i cant find a reference to "image.tiff" im not going to open all 25 emails but 25 + 88 equals 113 take away the few without attachments that are downloadable and its much closer to the 107 that i can download from those selected mails. the problem with this theory is there are 34 "image.tiff" files, so more than one per email. these two lines of code return 88 attachments Code:
egrep -c -i "Content-disposition:*" "/Users/clive/Library/Mail/Mailboxes/Drafts.mbox/mbox"Code:
egrep -c -i "^(.*;)?[[:space:]]+name=*.*" "/Users/clive/Library/Mail/Mailboxes/Drafts.mbox/mbox" |
Quote:
|
Quote:
|
|
Quote:
Don't forget I only said it was very probable, not highly probable. :) |
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 |
1 Attachment(s)
Quote:
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 |
Quote:
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 |
Quote:
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! |
Quote:
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.