10

Is there a best, most future-proof method of setting emacs on OS X to delete files to the trash?

I tried the instructions here:

(setq delete-by-moving-to-trash t)

(defun system-move-file-to-trash (file)
  "Use \"trash\" to move FILE to the system trash.
When using Homebrew, install it using \"brew install trash\"."
  (call-process (executable-find "trash")
    nil 0 nil
        file))

but they don't work:

Trashing...
(wrong-type-argument stringp nil)

Also, it feels wrong to have to install a separate program in order for Emacs to be able to trash files. Furthermore, I won't remember to install the "trash" program the next time I do a clean OS X install.

Update: I added this to my .emacs and removed the call to trash and it seems to work. I wonder why Emacs on OSX defaults to the FreeDesktop ~/.local/share/Trash location.

(setq trash-directory "~/.Trash")
6

4 Answers 4

9

The following is required:

(setq delete-by-moving-to-trash t)
(setq trash-directory "~/.Trash")

The function move-file-to-trash has three tests: (1) whether trash-directory is defined; (2) whether (fboundp 'system-move-file-to-trash); and, (3) (t . . . the catch-all that uses ~/.local/share... The second test always fails on OSX 10.6.8 when built with the standard option of --with-ns. I don't have access at the moment to other versions of OSX.

1
  • However OS X al tracks where the file came from if you use trade from the finder and that does not occur here - so note this is not a full implementation but often good enough
    – mmmmmm
    Oct 19, 2015 at 21:57
4

Define the function system-move-file-to-trash in this way should be enough:

(defun system-move-file-to-trash (file)
  (call-process "trash" nil nil nil file))

if you don't have trash installed, Emacs will tell you. then, don't set the variable trash-directory since trash does that for you.

About Emacs's trash behavior:

move-file-to-trash is an interactive compiled Lisp function in ‘files.el’.

(move-file-to-trash FILENAME)

Move the file (or directory) named FILENAME to the trash. When ‘delete-by-moving-to-trash’ is non-nil, this function is called by ‘delete-file’ and ‘delete-directory’ instead of deleting files outright.

If the function ‘system-move-file-to-trash’ is defined, call it with FILENAME as an argument. Otherwise, if ‘trash-directory’ is non-nil, move FILENAME to that directory. Otherwise, trash FILENAME using the freedesktop.org conventions, like the GNOME, KDE and XFCE desktop environments. Emacs only moves files to "home trash", ignoring per-volume trashcans.

I guess (setq trash-directory "~/.Trash") is not working: Finder will not be able to recover these files since it (at least) doesn't know where these files come from.

3

I've been using the osx-trash package. It works with or without the trash command line utility, and integrates properly with the macOS Trash directory.

2
  • Without the trash command line utility you can ear the trash sound. With the trash command line utility it's faster. Apr 26, 2020 at 9:53
  • As of April 2021 the package now calls trash with a-F argument which users Finder to do the trashing, so you get the sound and also when you look at the file in trash with Finder you get the Put Back option
    – mmmmmm
    Jun 10, 2022 at 20:00
0

According to my investigation (on MacOS 10.14 with Emacs 28.0.50).

For those, who DON'T really need "Put Back" feature

It's enough to configure:

(setq delete-by-moving-to-trash t)

(if (eq system-type 'darwin)
    (setq trash-directory "~/.Trash"))

delete-by-moving-to-trash is available/updated since Emacs 23.1.
trash-directory is available/updated since Emacs 23.2.
So this is expected to be a working solution for many (or majority) of MacOS Emacs users.

For those who DO want to use "Put Back" feature

The issue is not something specific to Emacs app only. On MacOS, "Put Back" feature is enabled only for those files that were deleted by Finder. (At least it used to be so and works this way on my system too.)

Possible workarounds:

  • Use the package osx-trash (with/out brew install trash).
  • brew install trash and call trash -F command from an init-file. Examples to base the solution on: osx-trash package code or EmacsWiki.
  • Do not install trash library, but call Finder with an Applescript. Examples to base the solution on: osx-trash package code again or use ns-do-applescript.
  • Use Emacs Mac port that more fully integrates Emacs into MacOS. There's a Homebrew adaptation of this package.
  • Implement in Emacs core. (That's a bit separate topic, so I'm just mentioning the possibility.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.