Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
    
I'll probably have to dig into the dev docs to suss out what and how UTI are handled to do this properly. developer.apple.com/library/ios/#DOCUMENTATION/FileManagement/… –  bmike Aug 5 '11 at 16:52
    
Searching for RCDefaultApp led me to this answer apple.stackexchange.com/questions/9866/… but somewhere else recently I saw RCDefaultApp mentioned not far from Magic Launch … got it: probably not as useful as the first link, apple.stackexchange.com/q/13557/8546 –  Graham Perrin Aug 5 '11 at 17:01
    
In a linked question, a comment draws attention to duti. Should that be an answer here? Or for this question, will you prefer to work only with what's integral to OS X? –  Graham Perrin May 17 '12 at 7:12
add comment

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
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 Aug 5 '11 at 16:50
add comment

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.

share|improve this answer
    
Discuss –  Graham Perrin Apr 29 '12 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. –  Graham Perrin Apr 30 '12 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 May 4 '12 at 19:16
    
Thanks! — expect the grey area of this answer to be tidied in due course. –  Graham Perrin May 4 '12 at 20:02
add comment

The Unix way of doing this is to open Terminal.app and setting the scripts to executable with chmod +x scriptname.sh.

Or you can make a link from /bin/bash to /Applications and define /Applications/bash to be the default application for .sh files in the Get Info dialog. But this seems to be kind of wierd IMHO.

share|improve this answer
    
The executable files are claimed by another app. I'm guessing I'll need to massage the database directly and monitor it if other apps as for this UTI - developer.apple.com/library/ios/#DOCUMENTATION/FileManagement/… –  bmike Aug 5 '11 at 16:53
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.