-
-
Save akagr/18ec650bc5f7e0ef663af9a30ce02973 to your computer and use it in GitHub Desktop.
-- ███████╗███╗░░░███╗░█████╗░░█████╗░░██████╗ | |
-- ██╔════╝████╗░████║██╔══██╗██╔══██╗██╔════╝ | |
-- █████╗░░██╔████╔██║███████║██║░░╚═╝╚█████╗░ | |
-- ██╔══╝░░██║╚██╔╝██║██╔══██║██║░░██╗░╚═══██╗ | |
-- ███████╗██║░╚═╝░██║██║░░██║╚█████╔╝██████╔╝ | |
-- ╚══════╝╚═╝░░░░░╚═╝╚═╝░░╚═╝░╚════╝░╚═════╝░ | |
-- | |
-- Open this script with 'Script Editor' on MacOS, then save it | |
-- inside /Applications as an 'Application', not 'Script'. | |
-- | |
-- Prerequisites: | |
-- | |
-- 1. There should be Emacs in your /Applications | |
-- If you downloaded emacs-plus from homebrew: | |
-- ln -s /usr/local/Cellar/emacs-plus@27/27.2/Emacs.app /Applications/Emacs.app | |
-- | |
-- 2. Make sure you have a /usr/local/bin/emacsclient | |
-- If you don't, update the script below to the correct path | |
-- | |
-- Akash Agrawal <[email protected]> | |
tell application "Finder" | |
try | |
log "checking for existing emacs client frames" | |
set frameVisible to do shell script "/usr/local/bin/emacsclient -n -e '(> (length (frame-list)) 1)'" | |
if frameVisible is not "t" then | |
log "server exists, attaching a new client frame" | |
do shell script "/usr/local/bin/emacsclient -c -n" | |
else | |
log frameVisible | |
log "server and window exists, bringing it to foreground" | |
end if | |
on error | |
log "starting server" | |
do shell script "/usr/local/bin/emacs --daemon" | |
do shell script "/usr/local/bin/emacsclient -c -n" | |
end try | |
tell application "Emacs" to activate | |
end tell |
# Complimentary shell helper to add an 'em' command if you're opening files from terminal. | |
# It will try to connect to a running emacs daemon. If not found, it starts one. | |
# | |
# Source this file from your ~/.zlogin, ~/.zshrc, ~/.bashrc etc, | |
# or add this code directly to those files. | |
# | |
# Example Usage: | |
# > em ~/.zshrc | |
# | |
# Akash Agrawal <[email protected]> | |
em () { | |
# Check if a frame(window) already exists | |
emacsclient -n -e "(if (> (length (frame-list)) 1) 't)" | grep -q t | |
# If it doesn't create a new window (start server if not running) | |
# Else, use the same window to open the file. | |
if [ "$?" = "1" ]; then | |
emacsclient -c -n -a "" "$@" | |
else | |
emacsclient -n -a "" "$@" | |
fi | |
} |
@rnkn Thanks for your suggestion. Tried it out but I feel it's solving a different problem.
What I'm trying to do is open emacs daemon from spotlight if it's not already running, or connect to it and open a frame. Creating a launch agent will start the daemon when the system starts, yes, but if for whatever reason I have to restart it, I'll have to do it from the CLI (right?).
That said, I think this can be a good addition to start a daemon so most timee, I don't have to do that initial slow load!
Ah I see! Yes I was confused by "launch agent" (this is what launchd calls its background services). This would start at login and now I've edited the plist to include the KeepAlive boolean, which will automatically restart emacs when it's killed with either command-Q or M-x kill-emacs.
You can then use Automator.app to create an application with a Run Shell Script action that runs /usr/local/bin/emacsclient -c
and save that as Emacs.app, which will be available via Spotlight and Launchpad.
Does M-x server-start
not accomplish the same thing as this script? I may be confused. Newbie here.
@AtomicNess123 Kinda. To run M-x start-server
, you have to have already started emacs. This script allows you to open spotlight, enter something like 'emacs' or 'em' or whatever you name it, and open emacs in daemon mode with a visible frame.
If you close emacs (without killing server), opening emacs from spotlight again will just connect to the running instance, so it's gonna be fast.
Thanks, I think I get the idea :)
Hey! Found this via Sacha Chua's blog.
This is actually a bit more complicated than it needs to be. Just put this in
~/Library/LaunchAgents
:Then alias
emacsclient -nw
andemacsclient -c
to something easier.