Solved

AppleScript to rename and save the attachments of emails in Oulook for Mac 2011

Posted on 2015-01-06
6
1,234 Views
Last Modified: 2015-01-10
I have an email subfolder (ie a subfolder under Inbox) in Oulook for Mac 2011 which contains hundreds of emails each with an attachment.  

- the emails are all of identical structure (e.g. this email contains image 1 with attachment 'image1', this email contains image 2 with attachment 'image2', etc.)
- all emails have a single attachment
- some of the attachments have an identical filename but contain different information


I want to use an AppleScript to:

set a filenamePrefix
set a 'userdefinedCounter

open Microsoft Outlook
go to a specific email folder 'myFolder' (the subfolder is under On My Computer:Inbox:myFolder)
go to the bottom of the list of messages in that folder
open the bottom_most message

save the attachment in bottom_most message to myDesktopFolder while inserting the string 'filenamePrefix&userdefinedCounter' at the front of the attachment

move the bottom_most message from email folder myFolder to email folder myFolder2

increment userdefinedCounter

repeat this process for all emails in myFolder continuing to work from the bottom_most message upward

Note:  I don't know if it will be necessary to keep retargetting myFolder during the execution of this script in case a new email arrives causing Outlook to jump to the inbox mid-script (not sure if it does that)
0
Comment
Question by:qeng
  • 4
  • 2
6 Comments
 

Author Comment

by:qeng
ID: 40536693
I did spend hours looking for scriplets to pull together to accomplish this but was unsuccessful in finding anything that worked.

Before posting my question, I had gotten as far as Step 4:

Step1.  set a filenamePrefix (successful)
Step 2.  set a 'userdefinedCounter (successful)

Step 3.  open Microsoft Outlook (successful)
Step 4.  go to a specific email folder 'myFolder' (the subfolder is under On My Computer:Inbox:myFolder) (successful but will need to check in context of full script)
Step 5.  go to the bottom of the list of messages in that folder (don't know how to do)
Step 6.  open the bottom_most message (don't know how to do)

Step 7.  save the attachment in bottom_most message to myDesktopFolder while inserting the string 'filenamePrefix&userdefinedCounter' at the front of the attachment (don't know how to do)

Step 8.  move the bottom_most message from email folder myFolder to email folder myFolder2 (i know how to do)

increment userdefinedCounter (i know how to do)

repeat this process for all emails in myFolder continuing to work from the bottom_most message upward (I can likely get a loop working)
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40536774
Here's a working script. It processes all the messages in the folder, saves the attachments and moves the messages to the second folder. What it doesn't do is start at the bottom of your list of messages. Why do you need to do that? Is it because you need a specific sort order for the attachments?

set saveToFolder to (choose folder with prompt "Choose the destination folder") as string
set prefix to the text returned of (display dialog "Enter the text to prefix the saved attachment names with" default answer "" buttons {"Cancel", "OK"} default button 2)
set ctr to 0

tell application "Microsoft Outlook"
	set topFolder to mail folder "inbox" of on my computer
	set srcFolder to mail folder "myfolder" of topFolder
	set destFolder to folder "myFolder2" of topFolder
	
	set selectedMessages to messages of srcFolder
	repeat with msg in selectedMessages
		set ctr to ctr + 1
		set attFiles to attachments of msg
		repeat with f in attFiles
			set attName to (get the name of f)
			log attName
			set saveAsName to saveToFolder & prefix & ctr & attName
			log saveAsName
			save f in saveAsName
		end repeat
		move msg to destFolder
	end repeat
end tell
display dialog "" & ctr & " messages were processed" buttons {"OK"} default button 1
return ctr

Open in new window


It would be possible to include the ID of the message and/or a string representation of the received date in the name of the saved attachments if that would help you categorise and sort them.

Regarding the possibility of more messages arriving while the script is running... the collection array is built at this line       
"set selectedMessages to messages of srcFolder"
>>       get every message of mail folder id 100
            --> {incoming message id 1, incoming message id 3}

So items that arrive after this line is executed will NOT be included in the process, the script would have to be run again to process the newly arrived messages.
0
 

Author Comment

by:qeng
ID: 40538497
Simon,

Brilliant.  It works.  I'm almost where I need to be.

The reason I wanted to start at the bottom of the list is because of the order the records are downloaded.  

For example, a user may elect to download records which are on successive pages 3 through 9.  Records of page 3 will arrive attached to the first email, records of page 4 will arrive next, etc..  On completion, the topmost message in the email folder will be for page 9 and the bottomost record  will be for page 3. The problem is that sometimes they arrive one after the other in emails (ie the time stamp is identical) with identical attachment names (e.g. "your records for today as of now") though the attachment content is different.

However, I can work around that problem (since I am concatenating all of the files anyhow; see below).  This was more of an interim measure so I'd know which page numbers had already been downloaded.

The last part I'm trying to achieve with this script is for it to concatenate the saved attachments residing in saveToFolder into one file (think of the attachments as txt files).

I can do this outside of the script using Terminal commands such as:

(after loading 'Terminal' from Applications)

CD \desktop
CD \myFolder
cat *.txt > filename.txt

It would be useful for 'filename' to be equal to 'prefix' set in line 2 of the script you kindly supplied.

How can we modify your script to execute the above Terminal commands (or equiv) once it has completed ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Assisted Solution

by:Simon
Simon earned 500 total points
ID: 40538759
Hmm, which is top and bottom depends entirely on user preference for sorting their inbox.

To do the last bit you wanted, just add these two lines to the bottom of the script (before the RETURN line):

-- Use the quoted form of the posix path to cope with spaces in the object names
set pp to the quoted form of (the POSIX path of saveToFolder)
-- The && ensures that the cat command is only executed if the cd command succeeds.
do shell script "cd " & pp & "&& cat *.txt > filename.txt"

Open in new window

0
 

Author Comment

by:qeng
ID: 40542042
Terrific.  Thx.  I adapted this to my needs.

Excellent help.  Thank you Simon.
0
 

Author Closing Comment

by:qeng
ID: 40542044
Terrific help.  Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mac monitoring software? 6 83
Non root SFTP users cannot log in - Software caused connection abort 15 75
Need to learn promise API 2 43
Reinstall Mac OSx 13 40
Apple's Mac OS X has become an official member of the malware club. The Flashback Trojan has affected over half million Macs, worldwide. It is behavior that ultimately gets malware onto a person’s computer. Obsolete or out-of-date software helps…
Set up iPhone and iPad email signatures to always send in high-quality HTML with this step-by step guide.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now