Auto-filing from Outlook for Mac

Why would I go back to Outlook?

For number of reasons, I am unfortunately going back to using Outlook on my work machine. I was able to go with Mail and, for the past year, BusyCal. It was great except for Exchange. Turns out that Exchange is pretty vital to the whole experience of Email and Calendar in my work-life. Exchange has problems all on its own. There are issues at work between even the different versions of office on PC's. Macs bring more issues. BusyCal isn't the issue. If I could still use BusyCal, I'd probably not make the switch. I lost access to BusyCal as it seems to have fallen off the corproate whitelist for access to Exchange. So much of my issue is with Calendar. I've had multiple issues with calendar items simply not syncing with Exchange. The final straw was moving a meeting on Calendar and having the series disappear! With any of my more important meetings and meetings with large distribution lists, I had been opening Outlook to manage these. So to answer my own questions, I have a lack of confidence in Calendar syncing properly and to manage an Exchange based connection.

Switching Back

I've been keeping the Outlook option open all along so it was simple to switch over except with 2 issues. First, I didn't have my auto-filing scripts set up for Outlook. Second, my archives from Mail won't import into outlook (after exporting in .mbox format). I won't deal with this at the moment.

I missed my auto filing scripts immediately and have adapted the script. I miss BusyCal daily and am reduced to hoping the next Office for Mac improves.

Script Update

Applescript support for Office for Mac is very good so this wasn't a problem beyond my lack of experience at scripting Outlook. Ultimately, this was solvable and the results are below. The script replaces my Mail script and does what I've described previously.

Dual Stream Filing Approach

I took the opportunity to set up a second script that auto tags and puts the document on the desktop (with Hazel). The reason for this is that I save too many files which does often complicate the process of me finding files quickly. So my approach is to have a "curated" pile of files that go through the desktop and then the balance of files in a separate pile that go through the inbox. I'm much more likely to be looking for the files that I've been working with on the desktop. With Tembo, I can target a search easily for either set of files.

The key to this is Hazel. The scripts simply save the files to two different nested folders based on the senders name. From here, Hazel tags them using dynamic tags and either puts them on the desktop if they are from one set of nested folders or my inbox for the others. Not the most elegant solution but since its really just a bunch of empty folders most of the time, its not an issue.

The Script

also can be found my github repository.

-- set up the attachment folder path
tell application "Finder"
    set folderName to "Attachments"
    set homePath to (path to home folder as text) as text
    set attachmentsFolder to (homePath & folderName) as text
    -- display dialog attachmentsFolder
end tell


tell application "Microsoft Outlook"
    
    set theMessages to selection
    repeat with eachMessage in theMessages
        
        -- set the sub folder for the attachments to the first part of senders email before a period
        -- All future attachments from this sender will the be put here.
        
        set theSender to sender of eachMessage
        set theAddress to address of theSender
        -- display dialog theAddress
        
        set AppleScript's text item delimiters to "."
        set senderName to text item 1 in theAddress
        set subFolder to senderName
        -- display dialog senderName
        
        
        
        -- use the unix /bin/test command to test if the timeStamp folder  exists. if not then create it and any intermediate directories as required
        if (do shell script "/bin/test -e " & quoted form of ((POSIX path of attachmentsFolder) & "/" & subFolder) & " ; echo $?") is "1" then
            -- 1 is false
            -- display dialog attachmentsFolder & "/" & subFolder
            do shell script "/bin/mkdir -p " & quoted form of ((POSIX path of attachmentsFolder) & "/" & subFolder)
            
        end if
        
        try
            -- Save the attachment
            repeat with theAttachment in eachMessage's attachment
                
                set originalName to name of theAttachment
                set savePath to attachmentsFolder & ":" & subFolder & ":" & originalName
                try
                    save theAttachment in file (savePath)
                end try
            end repeat
            --on error msg
            --display dialog msg
        end try
        
        
        
        -- set theArchiveMailboxName to "Processed"
        -- if (mail folder theArchiveMailboxName exists) = false then
        -- make new mail folder with properties {name:theArchiveMailboxName}
        --   end if
        -- repeat with aMessage in theMessages
        --  move aMessage to mail folder theArchiveMailboxName
        -- end repeat
        
        
    end repeat
    
end tell

Automatically Convert Word Documents to PDF

UPDATE - SCRIPT HAS A BUG - DO NOT USE TO AUTOPOST TO PUBLIC SITES THE SCRIPT CAN CONVERT ALREADY OPEN WORD DOCUMENT INSTEAD OF INTENDED DOCUMENT. DESCRIBED HERE ON NOODELSOFT FORUM.

Updated thanks to Mac Power User listener Corwin Carr - MPU Episode 186 with script at MPU Evernote document

The Issue

I have issues with a lot of the Microsoft Word documents I receive at work when I open them on my iPad. The documents are often complex and the format is garbled. As always, I need to do these things securely because of confidentiality of work documents. So web services are out. Also, iPad apps that convert don't usually solve my formatting issues. What does work is saving in PDF natively from Word.

The Design of the Solution

Here's my current solution. I set up a folder on my work Box account. That folder is watched by Hazel. On seeing the Word document, Hazel runs a script to open the document in Word on my Mac and saves it as a PDF back to the Box folder.

This solution assumes that you have a copy of office for Mac, a "always on" Mac to be available to you via a synced folder on a cloud service, and the corresponding cloud app on your iPad. This could be Box or Dropbox or even your Tranporter. Since I either have my Mac with me and I can read the file natively or I'm using my iPad and my Mac is back at my desk and available, this works for me. It should work for many of you with your always on Mac Mini!

How it Works

I use the open in feature on my iPad in the mail app to upload the document to the watched folder via my Box app. After the sync occurs to my Mac, the script fires and the PDF is created. Box syncs the document to the cloud and I have my easy to read and annotate (in Notability or Notes Plus) PDF on my iPad.

An Aside

Back when I first started my Mac productivity "Hobby", I had looked for a solution on the net and found this post with a solution by spazek. I was new to Hazel and Applescript so I was stumped to get it to work. I posted something on the Hazel forums here. Alas no help for the noob. I resorted to running this as a folder action using Hazel to shuttle the files back and forth. It worked and I never got around to updating the system. Recently, someone visited the post and was looking for help to get it to work. So I dug back into the script to simplify it and use Hazel Applescript conventions.

The Details

So here's the script and a screen shot of how I have Hazel set up.

Screen Shot 2014-03-01 at 12.24.32 PM.png
-- this script adapted from post at https://discussions.apple.com/thread/3050596?start=0&tstart=0 solution by spazek
-- details on the use of this script for Hazel found a http://scrubbs.me
-- Updated with delay to solve issue as described by Corwin Carr at Mac Power Users Show 186 notes http://5by5.tv/mpu/186
-- the First two commands avoid an error where Word would be unable to get the file path. The script would ultimately run appropriately, but only after throwing the error.

tell application "Microsoft Word" to activate
delay 1

tell application "Microsoft Word" to set theOldDefaultPath to get default file path file path type documents path -- looks like we change the default path to where the document is and then set it back when we're done
try
tell application "Finder"
set theFilePath to container of theFile as text

set ext to name extension of theFile

set theName to name of theFile
copy length of theName to l
copy length of ext to exl

set n to l - exl - 1
copy characters 1 through n of theName as string to theFilename

set theFilename to theFilename & ".pdf"

tell application "Microsoft Word"
set default file path file path type documents path path theFilePath
open theFile
set theActiveDoc to the active document
save as theActiveDoc file format format PDF file name theFilename
close theActiveDoc
end tell

end tell

end try
try

tell application "Microsoft Word" to set default file path file path type documents path path theOldDefaultPath

end try


Filing System - Automatically Saving Mail Attachment to Folder Named for Sender

Recently I was excited to make a few breakthroughs on my filing (reference system for you GTD folks) system. By breakthroughs, I mean finding a couple good nuggets via web searching variations of “automatically saving attachments in mail.”

This post on The Cocoa Quest is right what I was looking for. This issue I had was figuring out Applescript enough to make it work for me when I don’t want a rule to fire off the script. In hindsight, its pretty simple but it took me monkeying around with it quite a bit.

I modified the script from Mark Hunte to take out some of the functionality and suitable to fire off via a keyboard command with Quicksilver. You can use Alfred which is en vogue these days or Launchbar. I simply select the messages I want to ultimately tag with the sender’s name, execute the script and then let Hazel do its magic.

Modifications are to parse out and email address that for the vast majority of the time is lastname.first@companyname.com. The script will take the first part of the email if there is no “.” in it. Here it is:

-- Adapted from a copyrighted script by Mark Hunte 2013 
-- http://www.markosx.com/thecocoaquest/automatically-save-attachments-in-mail-app/
-- Changed script to parse out the first part of the email address as the folder name, eliminated time stamp folder
-- Changed to run as triggered script vs email rule
-- explanation of what and why at scrubbs.me


-- set up the attachment folder path

tell application "Finder"
set folderName to "Attachments"
set homePath to (path to home folder as text) as text
set attachmentsFolder to (homePath & folderName) as text
end tell


tell application "Mail"

set theMessages to selection
repeat with eachMessage in theMessages

-- set the sub folder for the attachments to the first part of senders email before a period
-- All future attachments from this sender will the be put here.
-- parse email name by @ and . to get to first part of email name

set subName to (sender of eachMessage)
set AppleScript's text item delimiters to "<"
set fName to text item 2 in subName
set AppleScript's text item delimiters to "@"
set fName to text item 1 in fName
set AppleScript's text item delimiters to "."
set subFolder to text item 1 in fName


-- use the unix /bin/test command to test if the timeStamp folder exists. if not then create it and any intermediate directories as required
if (do shell script "/bin/test -e " & quoted form of ((POSIX path of attachmentsFolder) & "/" & subFolder) & " ; echo $?") is "1" then
-- 1 is false
do shell script "/bin/mkdir -p " & quoted form of ((POSIX path of attachmentsFolder) & "/" & subFolder)

end if
try
-- Save the attachment
repeat with theAttachment in eachMessage's mail attachments

set originalName to name of theAttachment
set savePath to attachmentsFolder & ":" & subFolder & ":" & originalName
try
save theAttachment in file (savePath)
end try
end repeat
end try
end repeat

end tell

Update to Automatically Encrypting PDF with PDFPen via AppleScript and Hazel

I wasn't really happy with the security piece in the original simple script for encrypting PDFs and there isn't any scripting for keychain access anymore in Mountain Lion.  So I googled it and came up with a link that had a sample / demo script I could adapt at MacScripter.net.   To work, you need to store your selected password for your PDF's in the KeyChain (name in KeyChain should be inserted for PDFName and should be an application password.  It uses a built in 'security' command in OSX as explained by partron22 at MacScripter.net.  It will ask you for access to the KeyChain when the script executes and you can decide whether you want to grant access one-time or always.

The script (text at bottom) has has been set up to run in Hazel.  Hope this helps folks.

Script in Hazel

Script in Hazel

set this_file to theFile -- use this if you are using hazel 

set label to "PDFName" --this is the Name: in Keychain change for whatever you use
set Qlabel to quoted form of label
set t to do shell script "security 2>&1 find-generic-password -gl " & Qlabel
set text item delimiters to "\"" -- Get Password
set tlst to every text item of t
set pw to item 2 of tlst

tell application "PDFpen"
open this_file
save document 1 encrypt using AES256 password pw
quit
end tell

Automatically Encrypting PDF's via PDFPen in Filing WorkFlow

see updated script using Apple's KeyChain....better security

I'm still using Yojimbo but I'm contemplating switching to simple nested folders for filing my documents.  One piece I want to maintain is some security on the documents.  So I did a short script to automatically open and encrypt a PDF document in PDF Pen.  

Since the password is embedded in your script, I don't think its the most secure but should the documents end up somewhere else, they'll be secure.   The script is at the bottom. 

Hazel workflow with encrypting PDFPen

Script from the testing phase if you don't use hazel.

Script from the testing phase if you don't use hazel.

script is below for embedding in Hazel
-----------------------------------

set this_file to theFile
set pass_word to "password"

tell application "PDFpen"
open this_file
save document 1 encrypt using AES256 password pass_word

end tell