Last active
January 30, 2019 18:01
-
-
Save cweirup/43cef8ee17a39b5d494ed0ba8ae52d66 to your computer and use it in GitHub Desktop.
The big change from my previous version is that due to Sandboxing, Outlook scripts can no longer write to any directory you choose. So this script now saves to an allowed directory, then puts the directory you want to file to go into the "comments" file attribute. From there, use the Hazel script to read the "comments" attribute and move the fil…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- The big change from my previous version is that due to Sandboxing, | |
-- Outlook scripts can no longer write to any directory you choose. | |
-- So this script now saves to an allowed directory, then puts the | |
-- directory you want to file to go into the "comments" file attribute. | |
-- From there, use the Hazel script to read the "comments" attribute | |
-- and move the file where you want it to go. | |
use scripting additions | |
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 | |
property folder_path : missing value | |
-- Due to Sandboxing, need to save the files to a directory that Outlook is allowed to write to | |
-- To move the file to where we want, we'll store the destination folder in the file "comment" attribute, | |
-- then let Hazel read that and move the file to the true destination | |
-- Make sure you replace "<YOURUSERNAME>" with the username of your Mac | |
set AttachmentFileDirectory to ("/Users/<YOURUSERNAME>/Library/Group Containers/UBF8T346G9.Office/Outlook/Attachments/") as text | |
set AttachmentFileDirectoryAFS to ("Macintosh HD:Users:<YOURUSERNAME>:Library:Group Containers:UBF8T346G9.Office:Outlook:Attachments:") as text | |
set unixAttachmentFileDirectory to the quoted form of POSIX path of AttachmentFileDirectory | |
tell application "Microsoft Outlook" | |
-- Get currently selected message - This script only handles one message at a time. | |
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 | |
-- Select path to save the attachments | |
-- Due to Sandboxing, this path will be stored in each file's "comment" attribute so Hazel can move later | |
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 | |
-- 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 anAttachment | |
set fileDownloaded to AttachmentFileDirectory & name of anAttachment | |
set destFilePath to folder_path & name of anAttachment | |
set theCType to content type of anAttachment as string | |
-- Save the attachment, but ignore images (png/jpeg/bmp) as they may be embedded in signatures | |
if theCType is not equal to "image/jpeg" and theCType is not equal to "image/png" and theCType is not equal to "image/bmp" then | |
-- Save the file | |
save anAttachment in POSIX file fileDownloaded | |
-- Get the file we just saved so we can set the "comment" attribute | |
set theOrigFile to (AttachmentFileDirectoryAFS & name of anAttachment) as string | |
-- Set the "comment" of the downloaded file to the path it needs to go to | |
tell application "Finder" | |
set dlFile to alias theOrigFile | |
set the comment of dlFile to folder_path | |
end tell | |
-- Log the file sizes and file list | |
set attachmentSizes to attachmentSizes + attachmentSize | |
-- Create a link to the file for its final destination | |
set fileDownloadedURL to "file://localhost" & my path2url(POSIX path of destFilePath) | |
set fileDownloadedList to fileDownloadedList & "Attachment: <a href=\"" & fileDownloadedURL & "\">" & destFilePath & "</a><br>" | |
end if | |
on error errStr number errorNumber | |
display dialog "Ran into an error during save: " & errStr & " - " & errorNumber | |
error errStr number errorNumber | |
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 | |
set attachmentCount to count of attachmentlist | |
log (attachmentCount) | |
-- Navigate the list of files and delete the ones that were saved | |
-- This processes the attachment list from the end | |
set theCounter to attachmentCount | |
repeat until theCounter = 0 | |
if (content type of item theCounter of attachmentlist as string) is not equal to "image/jpeg" and (content type of item theCounter of attachmentlist as string) is not equal to "image/png" and (content type of item theCounter of attachmentlist as string) is not equal to "image/bmp" then | |
delete item theCounter of attachmentlist | |
end if | |
set theCounter to (theCounter - 1) | |
end repeat | |
end repeat | |
end tell | |
-- LINKS USED IN DEVELOPING THIS SCRIPT | |
-- https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_error_codes.html | |
-- https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReferenceFilesandFolders.html | |
-- http://www.satimage.fr/software/en/smile/external_codes/file_paths.html | |
-- https://apple.stackexchange.com/questions/140877/applescript-file-vs-alias/141094 | |
-- https://www.noodlesoft.com/manual/hazel/attributes-actions/using-applescript-or-javascript/ | |
-- https://stackoverflow.com/questions/28054529/moving-a-file-with-applescript?noredirect=1&lq=1 | |
-- https://www.noodlesoft.com/manual/hazel/attributes-actions/action-reference/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- In Hazel, attach a new rule to the Outlook folder specified in the other script. | |
-- Set one condition that "Comment is not blank". | |
-- Then set the action to Run this AppleScript (I have it embedded but could run as a separate file). | |
tell application "Finder" | |
set theLocation to (get the comment of theFile) | |
set the comment of theFile to "" | |
move file theFile to folder theLocation | |
end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome - good stuff - thanks for doing this.
FYI that I had to manually create the Attachments directory in "/Users//Library/Group Containers/UBF8T346G9.Office/Outlook/Attachments/" before this would work.
Otherwise, was awesome. Thanks again.