Launch Emacsclient with GUI (from Dock) on Mac OS X
Asked Answered
I

2

10

How can I launch Emacsclient with GUI from the Dock (or maybe also from the terminal) on Mac OS X?

The EmacsWiki describes how to create an "Emacs from Dock" app using Automator. It worked for me but I don't want to launch Emacs but Emacsclient. So, I tried replacing /Applications/Emacs.app/Contents/MacOS/Emacs with both /Applications/Emacs.app/Contents/MacOS/bin/emacsclient and /Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c but both didn't work.

Imprescriptible answered 13/9, 2016 at 8:1 Comment(2)
you might want to ask this question in emacs stackexchange if people do not reply here. Just saying.Comeuppance
if you're using the emacs-mac port, note that its emacsclient is a bit buggy: github.com/railwaycat/homebrew-emacsmacport/issues/52Goahead
G
9

From the terminal

You can find the appropriate path to emacsclient using type in your shell (assuming emacsclient -c works from said shell):

$ type emacsclient
emacsclient is /usr/local/bin/emacsclient

Then we can add the appropriate emacsclient flags (see $ man emacsclient for details) to open the GUI:

/usr/local/bin/emacsclient -n -c -a ""


From macOS GUI

To launch emacsclient from eg the Dock or Spotlight, it's easy to use Automator. Automator is built in to macOS.

Choose to make an "Application", then choose "Run Shell Script", and add a modified version of the above call to emacsclient:

/usr/local/bin/emacsclient -n -c -a "" -- "$@"

Then change "Pass input": use "as arguments" instead of "to stdin".

The added "$@" is where any optional arguments passed to this shell script will be placed. Here, this allows you to pass a filename to open with emacsclient. Automator automates passing this filename in when, eg, you click to open a file with the application we've just made. This also allows you to set the application to be the default application for certain file types.


From anywhere, flexibly

Another way to run the above shell command is with skhd (link). skhd is far more involved to learn, but ultimately makes it much easier to set up a large number of shell commands with rapid access.

For example, you could make "Ctrl-o" from anywhere in macOS enter a mode you name open_app, from which you could press "e" to open emacsclient, "d" to open emacs --debug-init, "t" to run emacs --adv-timers, "f" to open Firefox, "F" to open a second Firefox profile, etc.

Goahead answered 6/10, 2016 at 23:36 Comment(1)
The khd link is dead, now.Culp
D
1

One idea would be to create an applescript that does whatever the original poster desires, and wrap it up in an application using something like platypus or automator. See https://superuser.com/questions/685111/basic-setup-of-emacs-server-under-osx for additional ideas such as using the --daemon command-line argument instead of placing (server-start) within the user-configuration file.

Here is an example applescript:

#  (server-start) must be inside `init.el` or `.emacs` file.
#
#  This script can also be used in the terimal:  osascript path-to-script arguments
#  Terminal Example:
#  osascript /absolute/path/to/applescript/file "-e '(progn (dired \"/Applications\") (message \"Hello-World\!\"))'"

on run argv
    set arg to item 1 of argv
    set emacs to application "Emacs"
    set appIsRunning to emacs is running
    if appIsRunning then
        say "Emacs is already running."
        do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient " & arg
    else
    tell application "/Applications/Emacs.app/Contents/MacOS/Emacs" to activate
        say "Please wait five seconds for Emacs to load."
        delay 5
        do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient " & arg
    end if
end run
Diorama answered 14/9, 2016 at 1:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.