At Save attachments with AppleScript for Mail I had developed a script for Alfred to save attachments. I often get session logs files for Mail Archiver. The scripts saves me quite a bit of time. Of course, I can do the same for Outlook.
Mail and Outlook have similar AppleScript capabilities. But it still took some time to work out the differences. Instead of developing the full script I'll only show the differences to the Mail script.
Instead of the tell for Mail the tell is for Outlook.
Mail:
tell application "Mail"
Outlook:
tell application "Microsoft Outlook"
Like Mail Outlook has a selection for messages. In Mail the sender is a string. In Outlook the sender is a data structure called record. I can get the email of the sender out in a second step.
Mail:
set Originator to sender of theMessage
Outlook:
set Originator to sender of theMessage
set Originator to address of Originator
If the attachments folder already exists I need to get a reference to it. Theoretically, the code is Finder only. For whatever reason I had to do the code for Outlook in 2 lines of code.
Mail:
set newFolder to get item 1 of (every folder in attachmentsfolder whose name contains FolderName)
Outlook:
set newFolder to (every folder in attachmentsfolder whose name contains FolderName)
set newFolder to item 1 of newFolder
The final difference is that for Mail it was okay to get the folder as rich text. Outlook wanted unicode:
Mail:
set savePath to (newFolder as rich text) & originalName
Outlook:
set savePath to (newFolder as Unicode text) & originalName
That's all. The result is now:
--set main folder of attachments
set attachmentsPath to "/Users/beatrixwillius/Desktop"
try
set attachmentsFolder to (attachmentsPath as POSIX file) as alias
on error errMsg number errNr
display dialog "Please select a folder for saving the attachments! Error: " & errMsg & " Nr.: " & errNr
return
end try
if not my isdirectory(attachmentsPath) then
display dialog "Please select a folder for saving the attachments!"
return
end if
--set how the sub folders for the attachments should be named
set UseSubject to "true"
set UseFrom to "true"
if not UseSubject = "true" and not UseFrom = "true" then
display dialog "Please set the name of the attachment folders either to the subject or the from of the email!"
return
end if
tell application "Microsoft Outlook"
--get the selected emails
set selectedMessages to selection
--loop over the messages to get and save the attachments
try
repeat with theMessage in selectedMessages
--do only something if the message has attachments
set theAttachments to theMessage's attachments
if (count of theAttachments) > 0 then
--make folder in Finder with the selected name option
set theSubject to subject of theMessage
set Originator to sender of theMessage
set Originator to address of Originator
tell application "Finder"
activate
if UseSubject = "true" and not UseFrom = "true" then
set FolderName to theSubject
else if UseFrom = "true" and not UseSubject = "true" then
set FolderName to Originator
else if UseFrom = "true" and UseSubject = "true" then
set FolderName to Originator & " " & theSubject
end if
try
set newFolder to (make new folder at attachmentsFolder with properties {name:FolderName})
on error errMsg number errNr
if errNr = -48 then
--folder already exists
set newFolder to (every folder in attachmentsFolder whose name contains FolderName)
set newFolder to item 1 of newFolder
else
display dialog "Error: " & errMsg & " Nr.: " & errNr
return
end if
end try
open newFolder
end tell
--save the attachments
repeat with theAttachment in theAttachments
set originalName to name of theAttachment
set savePath to (newFolder as Unicode text) & originalName
save theAttachment in file savePath
end repeat
end if
end repeat
on error errMsg number errNr
display dialog "Error: " & errMsg & " Nr.: " & errNr
end try
end tell
--check if the path is a directory and is actually there
on isdirectory(someItem) -- someItem is a file reference
set filePosixPath to quoted form of (POSIX path of someItem)
set fileType to (do shell script "file -b " & filePosixPath)
if fileType ends with "directory" then return true
return false
end isdirectory