reddit / emacs

How does this script to run emacsclient work?

TeeMcBee · 2022-11-09 16:15
I'm trying to understand the first part of the second line -- `which osascript > /dev/null 2>&1 &&` \-- in the following script, from this [Tips And Tricks](https://emacsformacosx.com/tips) page, to start emacsclient on MacOS: #!/bin/sh which osascript > /dev/null 2>&1 && osascript -e 'tell application "Emacs" to activate' emacsclient -c "$@" .My (very meagre) understanding is that the script does the following three things: 1. Check that osascript is present 2. Launch Emacs (which I guess it is assumed should have a call to `(server-start)` in its init.el or .emacs), *but only if* osascript is present, 3. Run emacsclient Is that right? If so, I have two questions: 1. **Exactly how does the "*****but only if*****" in my point #2 above work?** I assume that if the `which osascript` fails, then the call to `osascript -e` never happens. But how is that done? Something to do with the `&&` I presume? 2. **Why is the script author making only the call to** `oasscript -e` **conditional on a successful** `which osascript`? Why don't they also gate the `emacsclient` **line itself?** (In other words why don't just they kill the whole script if the `which` fails?) I might have understood letting the `emacsclient` line still run, even if the `which` had failed, but only if the author had included a `-a` option for `emacsclient`. Then, even if the failed `which` did blocked the call to `oascript -e` which in turn meant there would be no Emacs launched (and so no call to `start-server`), still `emacsclient` could have launched the alternate editor it had been given using `-a`. But as it stands, it looks like `emacsclient` will still valiantly attempt to launch even though it is almost certainly doomed to failure through lack of an emacs server. ???
weevyl · 2022-11-09 16:29
For question (1), you should look into [shell short circuiting](http://wresch.github.io/2014/04/24/bash-short-circuit-evaluation.html). Basically the '&&' tells the shell to execute the next command only if the first one did not fail. For question (2) you will have to ask the author. Maybe they forgot?
anaumann · 2022-11-09 16:32
Or they want to use the same script across different platforms :) On MacOS, where background processes from homebrew-installed applications are not always great, they just keep an open minimized window and use Applescript to bring it back up.. And on Linux, they run the emacs-server as a systemd unit and don't need to care about that. Guarding the osaclient will only run that when needed, but always run emacsclient.