Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use Mac OS Lion and I would like to declare in my .bash_profile the file extensions and the default application to open them.

For example, I want to associate all .txt files to TextMate so that when I shall open the file from the shell it would automatically launch TextMate and opens my file.

share|improve this question
    
Issue textmate file.txt and I'm sure it works as you intended. That you can't just issue file.txt like that has to do with permissions for execution - it would be an ambiguous operation. If you want to run a script, you might just type ~/bin/myscript. But if you'd like to edit the same script, with your proposed system you would type the same thing. It would not make sense. A file ending such as .txt is really not fundamental to the file in question. It would bring complexity and more problems for a very small gain. I recommend just getting used to writing textmate file.txt. –  Daniel Andersson Aug 6 '12 at 9:17
    
Thanks a lot. Actually what I would like to be able to do is to type in shell something like: 'open filename' and get the appropriate application open the filename in question. –  cygnusxr1 Aug 6 '12 at 9:23
    
On Linux, this can be done with xdg-open (better info here). –  Daniel Andersson Aug 6 '12 at 9:52
    
@DanielAndersson The textmate command doesn't exist. I'm sure you meant mate? –  slhck Aug 6 '12 at 9:52
    
@slhck: Very likely, I was just guessing (I don't have a Mac). –  Daniel Andersson Aug 6 '12 at 9:53
add comment

2 Answers 2

You don't set this association in your .bash_profile. Your shell does not know which OS X application to open a file with – it's the Launch Services that take care of this.

The easiest way to globally change file type associations to certain applications is to install RCDefaultApp and just set everything through System Preferences.

In theory, you could also modify the ~/Library/Preferences/com.apple.LaunchServices.plist file for custom associations, but I wouldn't recommend going there.

I want to associate all .txt files to TextMate so that when I shall open the file from the shell it would automatically launch TextMate and opens my file

Go to a text file, right click it and select Open With…. Here, choose TextMate, and then click Change All….

Now, whenever you're in the shell, you can type:

open foo.txt

And this will open TextMate with your text file.1 The same applies to any other association you might set through either RCDefaultApp or the Get Info… dialog in Finder. The magic behind this is that open wil defer the call to the Launch Services, which then decide on the application to use.2

1: Actually, for TextMate, the following is enough: mate foo.txt
2: You can also manually choose an app, e.g. open -a Xcode foo.txt. Read man open for more.

share|improve this answer
add comment

I use duti for that. It basically just modifies ~/Library/Preferences/com.apple.LaunchServices.plist but doesn't require you to know the UTIs of filename extensions or restart to apply the changes. Here's a part of my configuration file.

# .txt, plain text files without an extension
com.apple.TextEdit public.plain-text all

# executable scripts
com.macromates.textmate public.unix-executable all

com.apple.Preview .jpg all
com.macromates.textmate .rb all
net.sourceforge.skim-app.skim .pdf all
org.videolan.vlc .mkv all
org.videolan.vlc .mp4 all

Filename extensions that are included in UTI declarations are converted to UTIs automatically, so .jpg also applies to .jpeg.

share|improve this answer
add comment

Your Answer

 
discard

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.