7

I am looking for a way to inject some more extensions into the OS X launch services database systematically so that when clicked, they execute in the terminal app just like .command files do by default.

Things like .ksh, .sh, and .csh would be nice to start, but I assume once I figure out how to add one, it's easy to do the rest.

An answer specific to Lion or Snow Leopard is fine, so a general-case answer for all OS versions is not necessary. It would be great if this were scriptable and wrote directly to the Launch Services database so I could automate this rather than going to finder on each mac and doing a lot of clicking.

3

2 Answers 2

3

If a file is opened with Terminal, and has the execute bit set, then Terminal will execute it.

Get Info on a .sh file and set it to be opened with Terminal, and click the Change All button.

1
  • 1
    When I made the .sh file executable, another app claimed it on my mac. Going to finder, get info, right click, ignore the warning that Terminal might not be able to open .sh does work. Technically, this is certainly doable - but I'll leave the answer open for something that directly writes into the Launch Services database programmatically instead of through the UI.
    – bmike
    Commented Aug 5, 2011 at 16:50
3
+50

To prefer Terminal to open .sh files — using the defaults command, without using Finder

In most cases, all that's required should be possible from the command line …

Preparation

Two commands:

defaults read com.apple.LaunchServices | grep -B 1 -A 3 public.shell-script

defaults read com.apple.LaunchServices | grep -B 1 -A 4 "LSHandlerContentTag = sh;"

If either command reveals an existing preference

Use Xcode or any other suitable property list editor to remove the relevant dictionary from the LSHandlers key within the following file:

~/Library/Preferences/com.apple.LaunchServices.plist

Hints:


Adding to LaunchServices preferences, for Terminal to view and edit .sh files

Two one-line commands.

Depending on your requirements, the first command alone may suffice:

defaults write com.apple.LaunchServices LSHandlers -array-add '{ LSHandlerContentType = "public.shell-script"; LSHandlerRoleViewer = "com.apple.Terminal"; LSHandlerRoleEditor = "com.apple.Terminal"; }'

defaults write com.apple.LaunchServices LSHandlers -array-add '{ LSHandlerContentTag = "sh"; LSHandlerContentTagClass = "public.filename-extension"; LSHandlerRoleViewer = "com.apple.Terminal"; LSHandlerRoleEditor = "com.apple.Terminal"; }'

For the new preferences to be respected

Modifications to the user domain persistence layer of Launch Services must be acknowledged. For this, least disruptive to the end user will be:

  • a restart of the operating system.

From the command line, if immediate restart is acceptable:

sudo shutdown -r now


Background

Understanding Terminal.app support for .sh (public.shell-script)

Consider the result of the following command:

defaults read /Applications/Utilities/Terminal.app/Contents/Info

Terminal aside for a moment: it's sometimes inappropriate (or simply worthless) to force opening of a document type, by an app that is designed without support for that type.

For Terminal, I imagine no harm in handling the public.shell-script type.

4
  • Discuss Commented Apr 29, 2012 at 12:36
  • @Lri in two or more tests, Terminal worked as expected for with a simple !sh one-liner — after the run of lsregister completed. But (as you note under the other question) using lsregister in that way is not without annoyance. I'll post something to Ask Different Chat for your attention. Commented Apr 30, 2012 at 13:37
  • 1
    I was really hoping @Lri had words of wisdom. I asked for detail and Graham - you are prolific on detail. I'm not able to put your answer to practical use (yet perhaps) but I very much appreciate the extra eyeballs and suggestions so far on this.
    – bmike
    Commented May 4, 2012 at 19:16
  • Thanks! — expect the grey area of this answer to be tidied in due course. Commented May 4, 2012 at 20:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .