I’ll just start by explaining that I hate applescript. I wanted to create a script that would take a specific folder withing the script and save each message from that folder to a text file on my machine for post-processing.
This is what I came up with.
It breaks down like this, the following script sets a list to be the messages in our names mailbox folder. We can then iterate over that array, getting attributes of each message, such as the message content or the sent date of the message:
Taking the creation of the file root out of the loop, and also adding a looping variable out of the loop the content inside the loop now looks like:
Looking in a bit more detail, we convert the counter (an int) into a string. We set the filepath to a base filename, the string version of the counter, and a file extension. In order to ensure that the file exists we pass this path to a shell script, and “touch” the file. That’s right, in order not to get fucked over by applescripts error handling, I’ve just called out to the system shell as the most useful way of creating a filehandler to a file that might not extist. Jesus.
The second part of the loop now creates the file handler, writes some of the properties of the mail message into the file, closes the file handler, and iterates our loop counter.