This is a website by Willem van Zyl

I'm a project manager, software developer, Apple evangelist and geek from South Africa. I'm passionate about web and mobile application development, usability, productivity, physics, astronomy, science fiction and fantasy.

If you would like to contact me, message me on Twitter or send me an email.

How to extract and sort email addresses from text files with Automator on Mac OS X

10 Feb 2009

Automator (Mac OS X's drag & drop batch script-building tool) can be used to create applications that streamline repetitive tasks without any knowledge of programming.

Follow these steps to build an Automator workflow that will search all text files in a specified folder for email addresses, sort these email addresses alphabetically, and remove any duplicates:

Launch Automator from your "/Applications" folder and choose to create a "Custom" workflow.

Add an "Ask for Confirmation" action to the workflow and give it an appropriate name and description to prompt the user whether she'd like to continue.

Add an "Ask for Finder Items" action to the workflow to let the user select the folder her text files reside in.

Add a "Set Value of Variable" action and create a new variable to store the user's selection for later use.

Add a "Run Shell Script" action, set the Shell to "/bin/bash", and set Pass input to "as arguments". Add this line into the body of the shell script:

grep -Eiorh '([[:alnum:]_.]+@[[:alnum:]_]+?\.
  [[:alpha:].]{2,6})' "$@" | sort | uniq

The above command will use the Unix grep utility to perform a regular expression search for email addresses in the folder the user specified (which will be automatically passed into this action as "$@"). The results from this search will be piped into the sort utility to be sorted alphabetically, then into the uniq utility to remove duplicates.

Add a "New Text File" action to the workflow, specify a name for the new file, set Where to the variable storing the folder the user selected, and Encoding to "Unicode (UTF-8)". Check the "Replace existing files" checkbox.

Add an "Ask for Confirmation" action and change the name and description appropriately to notify the user that the process has completed.

To save the workflow, choose "File->Save" from the menubar and specify a filename, then choose "File->Save As..." and set the File Format to "Application" to compile a standalone application.

Geekology's example of this Find Email Addresses in Files workflow can be downloaded here, with a compiled version of the application available here.

Do you like this? Share it:

Copyright © Geekology 2011. All Rights Reserved.

Hosted by Code. Like. Clockwork.