3

How would I go about setting all .rb files or all .py files to open in emacs if I double-click them; say, from the desktop?

I'm on OS X 10.6.2.

I have the script:

on open of finderObjects
    repeat with currFile in finderObjects
        set unixPath to POSIX path of currFile

        set base to do shell script "dirname " & unixPath
        set fname to do shell script "basename " & unixPath
        do shell script "cd '" & base & "';" & " emacs  " & fname

    end repeat
end open

But if I drag a file (text.py - just prints a line) on it, I get: emacs: standard input is not a tty

And I'm unsure of how to solve this.

Thanks!

EDIT: Solved with link: http://www.macosxhints.com/article.php?story=20031027142625782

set filecount to 0

on open filelist
    repeat with i in filelist
        set filecount to 1
        tell application "Terminal"
            set filename to do shell script ¬
                "perl -e \"print quotemeta ('" & POSIX path of i & "');\""
            do script "emacs " & filename & "; exit"
        end tell
    end repeat
end open

if filecount < 1 then
    tell application "Terminal"
        do script "emacs; exit"
    end tell
end if
Isaac
  • 15,783
  • 9
  • 53
  • 76
  • I think this would belong on superuser.com – Noufal Ibrahim Feb 02 '10 at 18:24
  • The reason I didn't post it there is I'm assuming that it will involved a bash script – that seems SO-y to me. But move it if it doesn't belong! – Isaac Feb 02 '10 at 18:25
  • In an ideal world it would go there. Sadly, I've yet to work in an environment where developers weren't expected to setup emacs themselves (or more likely, use some inferior editor....and *like it*). – T.E.D. Feb 02 '10 at 18:48

2 Answers2

3

For each type, select a file in the Finder, choose Get Info from the File menu. In the Info window that opens, under the Open with section, choose the emacs app you are using (and it must be an app version), and then press Change All.. .

If you are using the traditional curses emacs from the command line, you could build a small AppleScript app in the ScriptEditor or Automator action that would receive the files from the finder and open emacs with them. Then use Get Info to associate the app with .rb and .py files.

EDIT: See this recent answer for one way to create an AppleScript launcher app: just modify the shell command to call emacs instead.

FURTHER EDIT: Your script is almost there but you need to run the script under Terminal.app. Try modifying it like so:

        launch application "Terminal"
        tell application "Terminal"
            do script "cd '" & base & "';" & " emacs  " & fname
        end tell
Community
  • 1
  • 1
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
1

I'm not an OS X expert, but if emacs there is like it is on Unix and Windows it should have an emacsclient utility for feeding new files into an already running emacs session.

This previous SO question has some details on how to do this on a multitty setup. Perhaps some of that is applicable?

Community
  • 1
  • 1
T.E.D.
  • 44,016
  • 10
  • 73
  • 134