Need to automate a weekly task using AppleScript
March 28, 2009 8:20 PM   Subscribe

I need to automate the process of printing a report to PDF with the date in the file name, saving it in an archive folder, printing it, and emailing it as an attachment. I am on a Mac and using Pages, Automator, and AppleScript. Help me find the missing pieces!

I have to write a report each Friday for work and send it as an attachment to a specific email address where it gets processed by an internal server. Usually my process is as follows:

1. Write report in Pages.
2. Print report as PDF to a specific folder using OS X's built-in capabilities.
3. Name the PDF file based on the date.
4. Print the PDF file to a hard copy.
5. Attach the PDF to an email using Mail and send it.

I realize that's not a ton of work, but this Friday I felt the urge to automate the process. At first I wanted to do it to save myself some time, and then once I had spent an hour trying to figure it out it became more of a challenge, and then after spending another hour on it I didn't care about solving the problem myself and just wanted a solution by any means necessary.

Most of the individual pieces of this automated task are not difficult to accomplish, but I have found it difficult to get each step to talk to the next. I have also had trouble due to the fact that I want to make the file name of the saved PDF contain the date it was saved. I'm sure this would be easy for anyone who has used AppleScript extensively, but I'm just a beginner at it so it's baffling me.

Could someone help me figure out the proper script structure to make all this work? If it matters, I am using the latest version of OS X and Pages. The files need to be saved with the name "weekly_report_MM_DD_YYYY.pdf" (it doesn't have to be exact, just something along those lines) and the email that it is attached to doesn't need anything in it except for a recipient. If I've forgotten any necessary details, please let me know and I'll provide them.

posted by joshrholloway to Computers & Internet (4 answers total) 2 users marked this as a favorite
Please stand by... Think I'll have something for you shortly.
posted by zachlipton at 9:09 PM on March 28, 2009

Best answer: Since this turns out to be pretty similar to an automation task I've been putting off for some time, this post was a perfect excuse to wind up building more or less the whole thing. It's all pretty straightforward, so you should be able to crib from it to make any changes or automate similar tasks next time.

You can download a copy from my website. Once you decompress it (just double click on it), it goes in /Users/[yourusername]/Library/PDF Services. To use it, open your document in Pages (or really, any document in any word processor), choose File->Print, and in the little PDF menu at the bottom left, choose "Prepare Weekly Report." It will then do everything for you.

Before you do that, you'll want to open up the Automator action (just double click on it) and change a few things. In the first block (Copy Folder Items), you can change the location where you want the finished PDF files saved. The default right now is the Documents folder. You'll also want to adjust the mail message information down at the bottom, and perhaps select a printer if you have more than one connected.

The trick to making this work here is a special (and well hidden) type of Automator action called a Print Workflow. These are designed for exactly these sorts of tasks, and the input to the action is a PDF file of the document in question. This saves you the trouble of having to script generating the PDF itself.

Hope this helps. Let me know if you have any questions.
posted by zachlipton at 9:33 PM on March 28, 2009 [2 favorites]

Nice solution from zachlipton, but I just wanted to point out that if you name the file with the date in the YYYY-MM-DD format instead, not only will you have a nice chronological list when viewing your directory listing / folder by name so everything is much easier to find (instead of having to sort by date), but you'll also be compliant with the ISO 8601 International Standard :)
posted by dirm at 11:15 PM on March 28, 2009

Response by poster: Thanks, zach. It looks perfect. I'll give it a try on Monday and let you know how it goes.
posted by joshrholloway at 6:40 AM on March 29, 2009

« Older typing SMS from laptop   |   Is there a sanitary way to reduce our daily... Newer »
This thread is closed to new comments.