Skip to content

Instantly share code, notes, and snippets.

@guptesp
Last active May 19, 2022 18:37
Show Gist options
  • Save guptesp/a130a6a5efb42b4a2950 to your computer and use it in GitHub Desktop.
Save guptesp/a130a6a5efb42b4a2950 to your computer and use it in GitHub Desktop.
Applescript to save attachments from Outlook to disk
-- Save this file to ~/Library/Application Support/Microsoft/Office/Outlook Script Menu Items folder on your ma
tell application "Microsoft Outlook"
set totalSize to 0
set msgs to current messages
set myDocumentFolder to POSIX path of (path to documents folder as string)
set myAttachmentsFolder to myDocumentFolder & "Attachments"
tell application "Finder"
if not (exists myAttachmentsFolder) then
#make new folder at myDocumentFolder with properties {name:"Attachments"}
do shell script "mkdir -p " & quoted form of myAttachmentsFolder
end if
end tell
repeat with msg in msgs
if msgs is {} then
display dialog "Please select a message first then run the script" with icon 1
return
end if
set all_attachments to attachments of msg
set subj to subject of msg
set msgSubject to subj as string
set newSubject to my cleanSubject(msgSubject)
set saveToFolder to myAttachmentsFolder & "/" & newSubject
tell application "Finder"
if not (exists saveToFolder) then
do shell script "mkdir -p " & quoted form of saveToFolder
end if
end tell
repeat with this_attachment in all_attachments
try
set fileSize to file size of this_attachment
set totalSize to totalSize + fileSize
set {year:y, month:m, day:d, hours:h, minutes:min, seconds:sec} to current date
set timeStamp to ("" & y & "-" & my pad(m as integer) & "-" & my pad(d) & "-" & my pad(h) & "-" & my pad(min) & "-" & my pad(sec))
set fileDownloaded to saveToFolder & "/" & timeStamp & "_" & name of this_attachment
save this_attachment in fileDownloaded
set fileDownloadedURL to "file://localhost" & fileDownloaded
set fileDownloadedList to "Attachment: <a href=\"" & fileDownloadedURL & "\">" & fileDownloaded & "</a><br>"
set content of msg to fileDownloadedList & content of msg
on error
exit repeat
end try
end repeat
set attachmentCount to count of all_attachments
repeat (attachmentCount) times
delete item 1 of all_attachments
end repeat
end repeat
display dialog "Removed " & attachmentCount & " attachments occupying " & (round ((totalSize / 1024) * 100) / 100) & "KB"
end tell
on cleanSubject(this_text)
set c to my replace_chars(this_text, ":", "_")
set c1 to my replace_chars(c, "'", "_")
set c2 to my replace_chars(c1, "\"", "_")
set c3 to my replace_chars(c2, "!", "_")
set c4 to my replace_chars(c3, "@", "_")
set c5 to my replace_chars(c4, "#", "_")
set c6 to my replace_chars(c5, "$", "_")
set c7 to my replace_chars(c6, "%", "_")
set c8 to my replace_chars(c7, "^", "_")
set c9 to my replace_chars(c8, "&", "_")
set c10 to my replace_chars(c9, "*", "_")
set c11 to my replace_chars(c10, "(", "_")
set c12 to my replace_chars(c11, ")", "_")
set c13 to my replace_chars(c12, "-", "_")
set c14 to my replace_chars(c13, "/", "_")
return c14
end cleanSubject
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
on pad(n)
return text -2 thru -1 of ("00" & n)
end pad
@tmblrxaos
Copy link

Hey @guptesp

Is it possible for you to make another version of this script that just saves the attachments out but doesn't remove them from the emails themselves?

Thanks-in-advance

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