Solved

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

Posted on 2015-01-06
6
1,274 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Create a default user profile for Mac OS X 10.7/10.8 Create a user account on OS X that will be a template for every other user of that computer. I usually call it “profile” and make it an administrator account for the time being. 1. Install a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

815 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

11 Experts available now in Live!

Get 1:1 Help Now