Emacs App Mac Os X
Since the upgrade to macOS Catalina, I’ve had two serious annoyances with Emacs. Here’s a little insight into how I fixed them.
Issue 1: I Couldn’t Access Certain Folders
After the upgrade, Emacs was unable to access special folders, like the Documents folder. This is a ramification of the User Data Protection enhancements that were made in Catalina.
Mac OS X 10.1.4, the fourth update to Mac OS X 10.1 'Puma', was released on April 17, 2002.It mainly has a build number of 5Q125, although its build number as a Mac OS preinstalled on new Macs can vary: for iMac G4s, it's either 5R48 or 5T91; DVI PowerBook G4s have build 5R60; and eMacs get 5R106. Numerous changes in the update deal with network, security and more disc recording device support. NOTE: This project is superseded by Emacs.app, which is based on a later version of the GNU Emacs core. Version 9.0-pre3 or later of that project includes a binary, and functionality is better than Emacs on Aqua. However, if you are running OS X 10.1 (Puma) or 10.2 (Jaguar), you cannot use it. For use within Emacs. Org-mac-mailing – linking to messages in OS X Mail.app; org-mac-iCal – import OS X iCal.app events into Emacs diary; org-mac-link – Hyperlink to items in mac applications; For use outside Emacs. Org-mac-protocol – create links and remember notes from a variety of OS X.
Here’s the gist of why this exists: In a previous version, macOS started displaying user prompts (similar to iOS) to grant application permission for accessing things like contacts, photos, location, etc. These prompts were displayed when using special APIs designated for those purposes.
Alas, there was an easy way to bypass these prompts. Rather than using APIs to, say, access your contacts, you could just read the contact database via the filesystem. Catalina now enforces these permissions at the filesystem APIs, as well. This means that, because it doesn’t even ask for permission, Emacs is simply forbidden from accessing certain files.
Apple did provide an escape hatch: Full Disk Access. You, the user, can provide this privilege to an application, and it is allowed to access all files that your user would otherwise be able to access. It’s pretty easy: Go to the Security section of System Preferences, find Full Disk Access in the list, and then add Emacs:
Canon selphy cp800 software download for mac. But…this solution didn’t work. After doing this, I still couldn’t access my documents folder. Solving this is directly related to the second issue that’s been nagging me since the upgrade to Catalina.
Issue 2: Spotlight Can’t Bring Emacs to the Front
I commonly switch between applications on macOS by pressing cmd-space
to bring up Spotlight, then the first few letters of the application name, and finally return. It’s often quicker than finding the icon via cmd-tab
. Unfortunately, this method stopped working, too.
As it turns out, the binary embedded in Emacs.app is…actually not a binary. Instead, it’s a Ruby script. From the script:
Although this never caused problems on prior versions of macOS, here, it is responsible for both of these issues:
- Full disk access does not apply to Emacs because the permissions were not granted to
/usr/bin/ruby
. - Emacs can’t be launched again because it is already running, but the running binary doesn’t actually match the Ruby script. Spotlight gives up.
The Solution
On Catalina, the Ruby script will always choose to launch the bundled binary Emacs-x86_64-10_14
. So, if you simply move it in place of the Emacs launcher script, everything starts working as normal. At the Terminal, just run these commands:
And, with that, Emacs should correctly receive its Full Disk Access permission, and Spotlight will correctly bring it to front (n.b., it’s still required to add Emacs to the Full Disk Access section of the security system preferences).
Note that, because we have been performing surgery on the Emacs app, its code signature is no longer valid. The last command above removes the code signature from the Emacs binary. On my machines, I had no issues. If you can’t launch Emacs after running these commands, try following the steps in this document from Apple.
Conclusion
Emacs Mac Port
One final caveat: This works for me using homebrew-cask-installed Emacs version 26.3. In future versions of Emacs, it’s possible that the correct binary might be something other than Emacs-x86_64-10_14
.
If you’ve been plagued by these problems, I hope this post helped.
This is a quick note on the setup of Emacs on Mac OS X.
Install Emacs on OS X
Emacs App Mac Os X
The simplest way to install Emacs on Mac is using Homebrew
. Here is the commands:
If you want to try a patched Emacs with transparent titlebar, install as follows
(You may add --without-spacemacs-icon
, if you keep only one version of Emacs.)
Run Emacs as daemon
If you have run the Emacs, you can start an Emacs server by the emacs-lisp function (server-start)
. Then you could anywhere open files in or create a frame of this Emacs by emacsclient -nc
. However, this is rarely used. An alternative way is to run Emacs as daemon
and open emacsclient
in the same way.
In my Mac, I run multiple Emacs servers (each Emacs process as daemon is called one server) to allow using multiple color themes (e.g. I use a light theme for text editing like Markdown, LaTeX, and use dark themes for coding). The way to do it is giving each daemon process a name so as to connect to it later via emacsclient
:
and later you could connect to them, e.g. the main
server, by
To stop an Emacs server (the daemon process), you could call (kill-emacs)
in anyEmacs frame connecting to this daemon. Alternatively, use system command kill
withprocess ID listed and grepped by ps aux grep -i 'emacs --daemon'
.
To quickly start Emacs as daemon, connect to and kill them, I wrote a bash scriptfunction emacs-server-func.sh
on my githubrepository, which provides commands es, ec, ecc
:
es
: list all Emacs daemon processeses start
: startmain
andcoding
servers by defaultes start m
ores start main
: startmain
serveres start c
ores start coding
: startcoding
serveres start YOURNAME
: start a sever with your given name
es stop
: stop all Emacs daemon processeses stop m
ores stop main
: stopmain
server, similar forcoding
es stop YOURNAME
: start the sever with the given name
ec FILENAME
: connecting to themain
server and open the fileFILENAME
in a new frameecc FILENAME
: connecting to thecoding
server and open the fileFILENAME
in a new frame.
Alternatively, we could use Alfred.app
to perform the same tasks. We need its PowerPack
in order to build Workflows
. Create an Input
using Keyword
, with the keyword es start
(with Argument Optional
, enabling with space
), shown as
Then, connect to an Action
of Run Script
, in which choose bash
and fill the following codes
Similarly create workflows for es stop
, ec
, etc. The scripts are the same as thecodes provided in emacs-server-func.sh
. We just copy and paste them into Alfred
and create shortcuts to run them. The whole workflow looks as
and you could find the workflow, named Emacs.alfredworkflow
, on my github repository.
Since Mac OS X by default only allows one Emacs instance (not via daemon), we may use the follow code to open multiple ones, which is created as a workflow in Alfred triggered by em new
:
Create an app via Automator to open files in Emacs clients by double-clicks
Automator
is a built-in OS X app for creating custom automated user workflows forjust about any installed app you might have or even OS functionality. Suppose wecould like to open files by default in the Emacs client named as main
. You may openfiles in the Emacs.app, however, with too much user-defined configurations, it is tooslow to be acceptable. Using the daemon mode and opening in clients are a better choice.
The following shows how to create an app that allows opening files in Emacs clients.
Launch
Automator
and create a new document. SelectApplication
as its type.Search the Actions palette on the left for the
Run AppleScript
action and add itto your Automator document.In the
Run AppleScript
building block, copy and paste the following codes:and Save it with name, e.g.
Open in Emacs.app
in the file formatApplication
.Now you go to the files with specific extensions, right-click it, choose
Get Info
and choose our appOpen in Emacs.app
as the application to open it. If you prefer to using Emacs to open all files of this extension, clickChange All..
.
Load different themes for Emacs servers/daemons
In practice, I prefer light themes for text editing (like Markdown, LaTeX, org mode) and dark themes for programming. To achieve, we run multiple Emacs daemon processes, e.g. main
and coding
. Now we will describe how to load different themes in your .emacs
or init.el
configurations.
First of all, we need to prepare a few variables and loading functions:
These functions are essential since the theme has to be loaded when the frame is firstly created, instead of when the elisp files are loaded during emacs --daemon
.
Then we define different themes for each severs (the daemon processes), for examples:
Mac Terminal Emacs
where these constant variables are defined as follows in advance for convenience:
Emacs Os
The last step is to load or reload themes depending on specific Emacs running modes: