Mac – emacsclient: create a frame if a frame does not exist

emacsemacsclient

I start emacs server using

emacs --daemon

then open files using

emacsclient filename.ext

but the first file has to be opened using

emacsclient -c filename.ext

in order to create a new frame which can be later used by subsequent files without using -c command line flag for emacsclient.

I want to automate this. "if there is no emacs frame, emacsclient should create a frame else it should use the current frame". How can it be done?

Best Answer

This is like dimitri's solution, but it handles the case when emacs was launched as emacs --daemon. emacs --daemon makes a hidden window that causes xprop to give a false positive when checking for an existing window.

#!/bin/bash

emacsclient -n -e "(if (> (length (frame-list)) 1) 't)" | grep -q t
if [ "$?" = "1" ]; then
    emacsclient -c -n -a "" "$@"
else
    emacsclient -n -a "" "$@"
fi