Automate [parts of] my Life (the Apple way)

Ok, maybe that title was a bit clickbaity... but I still learned an interesting bit of Apple Script the other day, and wanted to discuss it and share it.

I had an odd problem. I had about 600 emails I wanted to scrape content from and get into a CSV table to run further analysis on (I am a behavioral researcher at heart, it's hard to turn that inclination off). Sure, I could go through and manually input the contents into a spreadsheet, then do the work, but that seemed miserable. As a computer scientist, I've learned anything that needs to be done more than 10 times should be done programmatically. Even if the time it takes to build out that program takes just as long as it would have taken to do the task manually. Should you ever have to do it again, you now have limitless time-savings on hand!

My problem immediately sent me down the rabbit hole of Googling for answers to my particular problem. But wouldn't you know, Apple Script is actually rather niche! Sadly no one else had the same problem I had, so I needed to adapt.

Thanks to this post and this post and this post and this gist I managed to piece together some viable code. You can find that code as a Gist here. The code uses all of the emails you currently have selected within the Mail app, so you can select all messages (as I did) or filter those messages and then select the subset before running this script. It asks you to select a directory to save the files to and then uses an iterative numbering schema to increment a value and save the content of the emails as 1.txt, 2.txt, 3.txt, etc...

Interestingly enough, this script does NOT operate in the same way as your email is sorted (so the order you see in the Mail app will not be captured in the order of the text files numbering). This worked fine for me as I later processed the text files with a Python script that extracted meaningful information like the date from the email body and later sorted the output accordingly.

Anyways this was a short and fun lesson learned in the art of Apple Script. I hope it serves others along the way to their own email data adventures. I may update this post later on with the Python analysis code I used to parse and clean up the data before visualizing it, but that is TBD.

Cheers!