2

Configuration

MacOSX 10.11.5
emacs version 24.5.1
brew 0.9.9

I want to have the following behavior in my emacs setup:

  1. At login, automatically start an emacs server process.
  2. Running the /Application/Emacs.app executable opens a frame to a running emacs server. If the server is not running, then run it and try again (i.e. emacsclient -a ''&).
  3. When executed from the command-line, have this behavior but use the current terminal instance to host the emacs session (i.e. emacsclient -nw -a ''&).

There are several pages that cover this information, albeit incompletely:


What I've Tried

  • I was able to accomplish (1) by installing the homebrew launch-agent. See brew info emacs for more detail.
  • Once (1) is running, it was easy enough to get (3) working.
  • The problem with the above links was that the my version of homebrew (0.9.9) doesn't have emacsclient in a subdirectory of the app bundle. That's not too big of a deal, I was able to locate the correct binary at /usr/local/Cellar/emacs/24.5/bin/emacsclient and modify the scripts appropriately.
  • I created an AppleScript app to replace the standard link to the Emacs.app in /Applications. The AppleScript has the following code

    tell application "Terminal"
        try
            do shell script "/usr/local/bin/emacsclient -c -n -a ''&"
            tell application "Emacs" to activate
        on error
            do shell script "/usr/local/bin/emacsclient -c -n -a ''&"
        end try
    end tell
    

There are two problems here:

  1. The try block doesn't do anything; it doesn't error out (i.e. throw an exception).
  2. Even though the shell script command works on the command-line, running it from AppleScript doesn't respawn the server process.

My main problem is the sometimes I accidentally kill the server process. Trying to activate the Emacs.app GUI frame doesn't work until I manually go to the command line and run emacsclient -a ''&. At that point I am able to use Emacs from the GUI.

Using the vanilla emacs provided by homebrew, how do I setup a system where activating Emacs.app restarts the server if it's been terminated? I suspect it has something to do with the way AppleScript sets up its environment to call emacsclient.

1 Answer 1

3

Use the following script, compiled as an application (via ScriptEditor):

tell application "Terminal"
    try
        -- Tests if Emacs running already; throws error otherwise
        do shell script "pgrep Emacs"
        -- Emacs is already running, is it an actual process?
        do shell script "/usr/local/bin/emacsclient -c -n &"
    on error
        do shell script "launchctl start homebrew.mxcl.emacs"
        -- Give Emacs some time to load
        delay 5
        do shell script "/usr/local/bin/emacsclient -c -n &"
    end try
end tell
tell application "Emacs" to activate

Assumptions & Caveats

  • If an Emacs process is running, it's in daemon mode.
  • The homebrew provided Launch Agent is being used.
  • This script creates a new window EVERY time it is called. This behavior is a bit different than a typical MacOSX application, where activating the application after it is already running just returns the user to the application. This script can be modified to have that behavior (i.e. after the pgrep check, check to see if Emacs is registered as a SystemProcess and do something appropriate).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.