Digital Media Mac Blogs > Mac

Everyday Automation - Processing Downloaded Files


If you repeat a computing task more than three times, consider automating it! "Everyday Automation" will provide the occasional idea on how to delegate repeating tasks to AppleScript and Automator. And in case you don't find the presented scripts and workflows to be useful as they are, they might at least inspire you to create your own.

If you live a highly Internetworked life, there will likely be certain types of files that, after downloading them from the 'Net, you perform the same regular tasks on: say, archiving bank statements to a folder, printing electronic invoices, etc. You can highly automate processing such files by using Folder Actions attached to your downloads folder.

Folder Actions are Automator workflows or AppleScripts that are run whenever the contents of the folder that they are attached to, changes. E.g., when you attach an Automator workflow to your Mac's downloads folder, that workflow is run whenever you download a file to your computer, and every time this happens, the list of files in the folder is handed to the workflow, in which you can use any of the countless actions included in Automator's vast library to process the downloads.

The Foundation

When attaching an Automator workflow to your Mac's downloads folder (not just the default folder called "Downloads" in your user folder, but any folder you have configured for downloads in your web browser, etc.), there is a little caveat: the workflow will be triggered as soon as that folder changes, which may be the case well before the download of a file is complete. So, to ensure that downloads are completed before they are processed, such workflows need to use an action called "Wait for File to Copy" as the very first action.

This action is not included in Automator's stock library, but can be downloaded from the downloads page on the Automator.us website, where it is listed as download #31 (or just use this direct download link).

Once you've downloaded this .ZIP file, double-click it to unpack it, and then double-click the "Wait for Files Installer" application and follow the instructions to add the action to Automator.

Now, launch Automator, create a new Custom-type workflow, and add the "Wait for Files to Copy" action. In the action's "Name Extensions" field, type those file name extensions -- i.e., file types -- that you would like to process in the workflow. Note that this action will only pass on files that are of the type you specify here; leave the field empty and no files will be processed. Additionally, you can modify the Timeout Value, which is the time this action will wait for any file downloads to complete, with the default being 300 seconds, or five minutes.

Unless this workflow should process any and all files of the type(s) specified in the "Name Extensions" field of the "Wait for Files to Copy" action, add the "Filter Finder Items" action to limit the list of files to those whose names contain certain search strings.

EA_Downloads_GenericWorkflow.png

This two-action workflow will be your foundation for any workflows processing files downloaded to the downloads folder. Let's have a look at an example: rename, archive, and open online banking statements.

A Post-Download Processing Example

The statements for my online banking account follow a rather clunky file naming scheme: "Account_1234567890-Statement_2008_003_pdf.pdf", in which "2008" reflects the year and "003" is an index. And yes, my bank's IT guys do find it necessary to re-state the file's .PDF type in the file name. What I would like the files to be named instead, is a much cleaner "MyBank 2008-003.pdf".

Also, after renaming the files, I print them out to compare receipts with the transfers on the account by old-fashionedly checking them off with a pen (and to have printed copies available in case the IRS comes knocking), and finally move them to a specific folder for archiving.

Using the foundation outlined above, the workflow filters the files by checking that their names contain the literal string "Account_1234567890-Statement_" (which also prevents them from being re-processed after having been renamed, by the way) and then performs a total of three "Rename Finder Items" actions.

Note that when you add "Rename Finder Items" actions, Automator offers to also add a "Copy Finder Items" action as a safeguard. If you're following along, just choose "Don't Add", as renaming does not pose a major threat to the files. To keep Automator from offering the "Copy" action when you add potentially dangerous actions, just hold down the Shift key while dragging-and-dropping the actio into the workflow.

Going back to the example, these are the three replacement operations:

  • Replace "Account_1234567890-Statement_" with "MyBank "

  • Replace "_pdf" with an empty string to remove it

  • Replace the remaining underscore between the year and the index with a dash

After renaming the file, it is moved to the archive folder via "Move Finder Items" and then opened in Preview for printing by "Open Finder Items".

Turning the Workflow into a Folder Action

EA_Downloads_SaveAsFolderAction.png

Once all required actions have been added to the workflow, it needs to be saved as a Folder Action via the "File" menu's "Save As Plug-In…" command. Give the workflow a name, choose "Folder Actions" from the "Plug-in for:" pop-up menu, select the appropriate folder to attach the workflow to, and make sure that the "Enable Folder Actions" option is checked.

And that's it: now, every time I download another bank account statement, Automator will kick in and rename it as desired, archive it, and open it in Preview. What used to take several time-consuming steps with both the mouse and the keyboard is now reduced to hitting Cmd-P for printing the statement from within Preview, and then Cmd-W (or Cmd-Q) for closing the file afterwards.

Since you can attach multiple workflows to a folder, you can also process multiple types of files with separate, easy-to-maintain workflows.

Categories





AddThis Social Bookmark Button
Comments (1)
Read More Entries by Jochen Wolters.

1 Comments

Leave a comment


Recommended for You

Topics of Interest

Archives


 
 


Or, visit our complete archive.