Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to open all txt files in my home directory with emacs, but not all the others with text edit, is that possible? How could it be done?

share|improve this question
XEmacs or Aquamacs, or yet another emacs program? – Daniel Beck Feb 25 '12 at 11:55
I use gnu emacs. – Tor Thommesen Feb 25 '12 at 17:50
up vote 2 down vote accepted

Open Automator and create a new application. Add a single Run Shell Script action that receives input as arguments, and has the following script code:

if [[ $# -eq 0 ]] ; then
    # just exit if there are no files to open
    exit 0

# check the location of the first file to determine what to do
if [[ "${1/$HOME/}" != "$1" ]] ; then
    # $HOME (e.g. /Users/danielbeck) was found in the file path
    open -a TextMate "$@"
    open -a TextEdit "$@"

Save as e.g. "Conditional Open Text Files"

Open the Get Info dialog of a .txt file and select it to open with this newly created program. Then, click Change All….

This will cause all text files to open with this script application, which looks at the file path and decides based on that whether to open the files in TextMate or TextEdit. Use the application name of your particular emacs instead to make this work with e.g. Aquamacs.

If you want to get a nicer icon than the default "unknown document" icon, follow these instructions.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.