Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Outlook 2011 Attachment Remover AppleScript
-- AppleScript to remove attachment from selected messages in Outlook 2011
-- Incorporates code from these sources:
-- http://dice.neko-san.net/2012/03/strip-attachments-from-outlook-2011-using-applescript/
-- http://stackoverflow.com/questions/9617029/how-to-get-the-a-file-url-in-osx-with-applescript-or-a-shell-script (for path2url function)
on path2url(thepath)
-- Needed to properly URL encode the path
return do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of thepath
end path2url
tell application "Microsoft Outlook"
-- Get currently selected message
-- FOR NOW JUST SELECT ONE
tell application "Finder"
try
tell me to set folder_path to (choose folder with prompt "Select a folder to save the attachments") as text
on error number -128
-- User canceled the action
display dialog "No folder selected. Please rerun the script and select a folder." with icon 2
return number
-- Exit the script
end try
end tell
set selectedMessages to current messages
set attachmentSizes to 0
set fileDownloadedList to "Attachments Removed:<br>------------------------<br>"
if selectedMessages is {} then
display dialog "Please select a message first then run the script" with icon 1
return
end if
-- Check for attachments
repeat with theMessage in selectedMessages
-- Get the attachments
set attachmentlist to every attachment in theMessage
repeat with anAttachment in attachmentlist
set a to anAttachment
try
set attachmentSize to file size of a
set fileDownloaded to folder_path & name of a
-- Save the attachment
save a in folder_path & name of a
-- Log the file sizes and file list
set attachmentSizes to attachmentSizes + attachmentSize
set fileDownloadedURL to "file://localhost" & my path2url(POSIX path of fileDownloaded)
set fileDownloadedList to fileDownloadedList & "Attachment: <a href=\"" & fileDownloadedURL & "\">" & fileDownloaded & "</a><br>"
on error
exit repeat
end try
end repeat
set content of theMessage to fileDownloadedList & "<br>" & content of theMessage
-- Need to delete the attachments here, otherwise we throw index errors
-- Becase Outlook resets the list when deleting, need to just keep deleting the first
-- item each time until all are removed
set attachmentCount to count of attachmentlist
repeat (attachmentCount) times
delete item 1 of attachmentlist
end repeat
end repeat
end tell
@marmot401
Copy link

marmot401 commented Aug 1, 2013

Nice Script, I made a minor mod...

https://gist.github.com/marmot401/6127913

7-31-13 updated the script so that running the script on multiple emails does not cause the attachment list to grow. Moved "repeat with theMessage in selectedMessages" to a better place.

@dagiorgi
Copy link

dagiorgi commented Oct 17, 2013

Great script! How do you set the attachment size limit to 100KB?
I'm trying to get only the attachments that are over a certain size to be removed.
thanks!

@dpiro
Copy link

dpiro commented Jul 16, 2014

Works great -- thank you!

@cweirup
Copy link
Author

cweirup commented Jan 4, 2017

If you want to only save and remove attachments above a certain size, you can use the "file size" property on the attachment object, which is an integer. Something like:

if file size of a is greater than 99999 then
     -- Process the file
end if

Hope that helps!

@frenchjp
Copy link

frenchjp commented Jan 12, 2017

Has anyone updated this script to work with Microsoft Outlook 2016?

@cweirup
Copy link
Author

cweirup commented Mar 14, 2018

I have a new version of this script for Outlook 2016. Specifically, it handles the issues with saving files in the Sandbox. It does require the use of Hazel to accomplish this.

Outlook 2016 Attachment Remover

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment