Skip to content

Instantly share code, notes, and snippets.

@akagr
Last active June 14, 2024 03:46
Show Gist options
  • Save akagr/18ec650bc5f7e0ef663af9a30ce02973 to your computer and use it in GitHub Desktop.
Save akagr/18ec650bc5f7e0ef663af9a30ce02973 to your computer and use it in GitHub Desktop.
MacOS launch helper to start emacs daemon or attach to one
-- ███████╗███╗░░░███╗░█████╗░░█████╗░░██████╗
-- ██╔════╝████╗░████║██╔══██╗██╔══██╗██╔════╝
-- █████╗░░██╔████╔██║███████║██║░░╚═╝╚█████╗░
-- ██╔══╝░░██║╚██╔╝██║██╔══██║██║░░██╗░╚═══██╗
-- ███████╗██║░╚═╝░██║██║░░██║╚█████╔╝██████╔╝
-- ╚══════╝╚═╝░░░░░╚═╝╚═╝░░╚═╝░╚════╝░╚═════╝░
--
-- 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
}
@AtomicNess123
Copy link

Thanks, I think I get the idea :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment