Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
modified apple script, https://gist.github.com/cweirup/3058303 7/31/13 updated the script so that running the script on multiple emails does not cause the attachment list to grow.
-- 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)
-- https://gist.github.com/cweirup/3058303
-- 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
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 YOU CAN SELECT MULTIPLE EMAILS
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
repeat with theMessage in selectedMessages
-- this line was moved here to allow for multiple messages to be processed
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
-- 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
@dagiorgi

This comment has been minimized.

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!

@emidd

This comment has been minimized.

Copy link

emidd commented Aug 26, 2015

Hello, This is my first time using GitHub. I was wondering if someone could help me to put each type of file (docx, xlsw, vsd, etc.) into is own subdirectory within the directory destination selected in the dialog window when prompted? Any help would be greatly appreciated.

Thanks,
Eric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.