How do you set default application for `open` command in terminal based on file extension type?
Asked Answered
C

2

6

Some time ago I set up Sublime text 2 as my default editor for GIT in the command line terminal. I would now like to change it to LightTable, but am having some trouble. I have set both the VISUAL and EDITOR environment variables to LightTable and that does the trick for most extensions. But even though I set the .gitconfig file editor variable to LightTable as well, using several variations of this command:

git config --global core.editor "'/Applications/LightTable.app' -w"

and this one:

export GIT_EDITOR=LightTable

it is continuing to open .md files with sublime text 2. Other file extensions are also opening with TextEdit or other applications. Is there an easy way to rein in the chaos and set my default opening applications based on extension? I have read the man page for open and it mentions LaunchServices, but I can't find that on my hard-drive. I'm using bash with Unix(Darwin) on a mac running OSX. Thanks in advance!

Conveyancer answered 1/7, 2014 at 5:32 Comment(5)
This question is really off-topic for Stackoverflow (apple.stackexchange.com would be better) but the normal way to set LaunchServices associations is by selecting a file in the Finder, using the Get Info command, the Open With setting, and the Change All button.Somerville
Sorry Ned. I am actually becoming less and less clear about what is an appropriate question for a given forum. Is there like an atlas or something that lays it all out? Also, is there a way to move the question to that forum instead of having to rewrite it out again?Conveyancer
And thank you for this rocking answer. It pretty much solved the issue. I am still unclear as to how I could have done this from the terminal or specifically what variable where was causing it to open with Sublime Text 2 in the first place.Conveyancer
Application bundles can specify what file types they are prepared to handle; they do so in their Info.plist. In the common case where more than one app is prepared to handle the same file type, it's a bit of a crap shoot which one wins; most likely, the app that was most recently installed. But it's kind of magic and magic that Apple has tweaked a number of times over the life of OS X. I believe you can change file type associations from the terminal but it's painful.Somerville
WRT what's on topic here, check the help page: stackoverflow.com/help/on-topicSomerville
T
1

The problem is that /Applications/LightTable.app isn't an executable, it's an application bundle. You can double-click on application bundles from the Finder, but you can't run them from the terminal. See for yourself

$ /Applications/LightTable.app ./myfile.txt
zsh: permission denied: /Applications/LightTable.app

You will have to specify the full executable path, something like LightTable.app/Contents/MacOS/LightTable. I don't know exactly what it is.

Tammitammie answered 1/7, 2014 at 5:36 Comment(3)
Sure you can launch app bundles from the command line. That's exactly what the open command does. See man 1 open.Somerville
This explains how LaunchServices, and by extension the open command, is able to handle bundles. But I am still not sure that 'being knowledgable about' and 'launching' are the same thing. I have just searched the LightTable.app directory and can't find a simple executable path that ends with /LightTable.Conveyancer
In an OS X app bundle, the executable is specified in the app's Info.plist. The executable is normally found in <appname>.app/Contents/MacOS/. But that's not really germane to your question.Somerville
D
1

I build a wrapper duti wrapper, called dutis , providing an interactive way to set default applications in command line.

You can set default applications using dutis within a few steps:

  1. Waiting for environment checking

enter image description here

  1. Selecting suffix

enter image description here

  1. Selecting application UTI

enter image description here

Dawndawna answered 2/7, 2023 at 12:47 Comment(1)
I think it is customary to note that you are the author.Stolon

© 2022 - 2024 — McMap. All rights reserved.