Macos – Emacs opens files in a new frame when opened with “open -a”

emacsmacmacos

I want to be able to invoke Emacs from the command-line, and either start graphical Emacs if it isn't running or visit a file in an already running Emacs. I tried to do this with emacsclient which I know is the "right" way to do this, but I had a lot of problems with it on OS X, it would randomly crash, or the emacs --daemon process would hang during shut down or rebooting the machine, and general flakiness. While I'd like to get that to work, at the moment open -a actually works much better, except for this one problem I'll describe below:

Using open -a like this:

$ open -a Emacs file.txt

will start Emacs if it's not running, and visit the file. But if I do this when the current buffer isn't *scratch* the file is opened in a new frame (ie a new system window).

Here's an example session:

$ open -a Emacs file.txt

This starts Emacs and opens file.txt, so there's a single frame with this buffer in it. If I switch to the *scratch* buffer, and do this:

$ open -a Emacs file1.txt

It opens this file in the same frame. Now there's a single frame with this file open, and if I do this:

$ open -a Emacs file2.txt 

It opens a new frame, resulting in two frames open at once.

I've tried fiddling with command-line args to Emacs using the --args switch to open, but this doesn't seem to work for subsequent calls, for instance:

$ open -a Emacs --args --eval='(print "foo")'
$ open -a Emacs --args --eval='(print "bar")'

This only prints "foo" in the message buffer… the second time Emacs is just brought to the foreground but no message is printed.

I'm not sure how open communicates with applications that are already running, does anyone know how I could find out? Or is there some way to get a much more detailed log of what's going on than the Messages buffer? Nothing interesting is printed to that buffer during the session above, so I don't know how I could hack some Emacs Lisp to do what I want…

Thanks!

Best Answer

Answer is here: emacs variable to "open with" in original frame

It's a change to the default settings in newer versions of emacs. Add:

(setq ns-pop-up-frames nil)

to .emacs file.

Very happy to have found this.

Related Question