Emacsclient --alternate-editor= "Could not start the Emacs daemon"; standard input is not a tty

I have a macro intended to create a new Emacs frame.

/usr/local/bin/emacsclient -n -c --alternate-editor=

It works in Bash in Terminal. If an Emacs server isn't already running, the --alternate-editor= argument says to start the server and try again.

In Keyboard Maestro, it works if an Emacs server is already running. Otherwise, Keyboard Maestro shows the following message.

If I display the results in a window, I can see that it looks like Emacs fails to start in daemon mode because standard input is not a tty.

/usr/local/bin/emacsclient: can't find socket; have you started the server?
To start the server in Emacs, type "M-x server-start".
emacs: standard input is not a tty
Error: Could not start the Emacs daemon

It's surprising to me that emacsclient tries to start the daemon in a way that requires a tty. I might investigate further in emacsclient.

How might I work around this in Keyboard Maestro in the meantime?

I wonder if the issue is not that the server requires a tty, but that without the server, it requires a tty.

In which case the issue is back to simply why wont the daemon start. Especially since it works when the daemon is running.

And the answer to almost every question along the lines of "This script works in the Terminal but not in Keyboard Maestro" comes back to differences in your environment, almost always specifically differences in the environment variables set up by your bash dot files (.bashrc or whatever).

Run env in the Terminal, and run env as a command in Execute a Shell Script, displaying the results in a window (or copy to the clipboard) and then compare them. Specifically, compare them looking at any environment variables that will affect the daemon starting, including PATH, but potentially including other ones, even PWD could be a factor.

Do you run emacs --daemon specifically somewhere? If so, try running the same command in an Execute Shell Script action and see what results you get, and/or what environment variables play a factor.

You're right.

I have another copy of Emacs at /usr/bin. That version is 22.1.1, presumably included with macOS. That version fails to run emacs --daemon without a tty.

This revised script works. It looks like Emacs 26.2 does not require a tty to start emacs --daemon.

emacsclient -c -n --alternate-editor=

You may want to set your default PATH to include that, as described in the wiki.