Solved

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

Posted on 2015-01-06
6
1,172 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
Comment Utility
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:
SimonAdept earned 500 total points
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Assisted Solution

by:SimonAdept
SimonAdept earned 500 total points
Comment Utility
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
Comment Utility
Terrific.  Thx.  I adapted this to my needs.

Excellent help.  Thank you Simon.
0
 

Author Closing Comment

by:qeng
Comment Utility
Terrific help.  Thank you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

763 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

10 Experts available now in Live!

Get 1:1 Help Now