macOS: Open files and folders with Emacs
Use Emacs to open files/folders when in Finder
The first day I joined Hootsuite over a year ago, I received my work laptop; and it was a Macbook - my first ever. Naturally, as an Emacs user I needed to integrate macOS with Emacs! I wanted to be able to open files and folders in Emacs with just a keyboard shortcut.
It took me some research and getting familiar with the new OS and what it offers. The result was, in macOS jargon, a "service" which I bound to command-\
.
To use it, create a "Quick Action" with "automator", add a "Run Applescript" node to your workflow, copy-paste the code below and save it. You can add a keyboard shortcut to it later on, from the keyboard preferences.
-- launch emacsclient in a new frame
--
-- if the active application is Finder
-- open the selected item(s) in a buffer(s)
-- or if nothing is selected open the current directory.
-- if the active application is NOT Finder
-- simply create a new frame
--
-- note: assumes emacs is already running as a daemon or you've run (server-start)
--
on run {input, parameters}
-- NOTE: make sure you change this path to fit your installation
set aCmd to "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -cn" as text
--
tell application "System Events"
set anApplication to name of first application process whose frontmost is true
end tell
--
if anApplication is "Finder" then
set aCmd to aCmd & " -- "
tell application "Finder"
set aSelection to selection as alias list
end tell
if aSelection is equal to {} then
tell application "Finder"
set aPath to (target of front Finder window)
end tell
set aCmd to aCmd & (quoted form of POSIX path of aPath)
else
repeat with anItem in aSelection
set aPath to POSIX path of contents of anItem as text
set aCmd to aCmd & (quoted form of POSIX path of aPath) & " "
end repeat
end if
end if
do shell script aCmd
--
activate application "Emacs"
--
return input
end run
Comments
Post a Comment