No internet connection
  1. Home
  2. How to

How to move a file to the trash in Finder

By Andrew Sherman @Andrew_Sherman
    2021-04-09 15:07:34.006Z

    How can I move a named file from a known location in Finder to the trash?

    Solved in post #2, click to view
    • 17 replies
    1. Here you go!

      // If it's a file
      sf.file.move({
          sourcePath: `~/Desktop/Test.txt`,
          destinationPath: '~/.Trash/Test.txt'
      });
      
      
      // If it's a folder
      sf.file.directoryMove({
          sourcePath: `~/Desktop/Test Folder`,
          destinationPath: '~/.Trash/Test Folder'
      });
      
      Reply1 LikeSolution
      1. AAndrew Sherman @Andrew_Sherman
          2021-04-09 16:29:45.827Z

          Brilliant, thank you. I was getting stuck with the location for trash.

          1. AAndrew Sherman @Andrew_Sherman
              2021-04-10 06:09:52.741Z

              Raphael, is there a way to get this to overwrite if the file already exists in the destination location?

              1. Mmm.. this is a tricky one. Usually, I would check if the file already exists, and if so, delete it. But in this case, these commands don't work because it's in the Trash.

                Maybe @Kitch can give us another angle?

                1. AAndrew Sherman @Andrew_Sherman
                    2021-04-10 16:04:33.252Z

                    Ok no worries; what I'll do is empty the trash first. Thanks!

                    1. Kitch Membery @Kitch2021-04-10 18:14:26.524Z

                      Hi @raphaelsepulveda & @Andrew_Sherman

                      I’ll take a look over the weekend. :-)

                      Rock on.

                      1. TTJ Allen @TJ_Allen
                          2022-04-22 12:35:25.246Z

                          Hey Kitch,

                          Just wondered if you ever got a chance to look at this one? It would be a super useful little script for me also.

                          Hope all is golden!

                          Cheers

                          1. Kitch Membery @Kitch2022-04-22 18:48:30.286Z

                            Hi @TJ_Allen & @Andrew_Sherman,

                            Firstly... Andrew, sorry for not getting back to you on this one.

                            To follow up, all the methods that I have found require some brute force for erasing files from your system, which is something I'm reluctant to share here on the forum.

                            Also, AFAIK the trash folder works differently to the way other folders do. It's my understanding that If files that are in the trash get deleted with brute force this can cause hidden files to change, potentially causing unforeseeable issues down the line.

                            Can you describe in more detail why you'd like this functionality, maybe if I have a better understanding I can suggest a different solution.

                            Rock on!

                            1. In reply toTJ_Allen:
                              AAndrew Sherman @Andrew_Sherman
                                2022-04-22 19:06:55.315Z

                                I'm using something like this to copy files to a predetermined folder and empty the trash first in case the file already exists. I use this for client reviews, watch folders for upload to frame.io, vimeo etc (Via Finder integration). It's a template so you'd need to set up properties for the path(pathname) and log (text string).

                                I have plans to develop it and make it more robust so that it can easily switch between copying and moving, as well as handling both files and folders.

                                
                                sf.ui.finder.appActivate();
                                
                                const { destinationFolder, destinationLog } = event.props;
                                
                                sf.engine.runInBackground(() => {
                                
                                    log(destinationLog);
                                
                                    function copyToFolder() {
                                
                                        sf.ui.finder.selectedPaths.forEach(filePath => {
                                            // Get each file name
                                            var fileName = filePath.split('/').slice(-1)[0];
                                            var destinationFile = destinationFolder + '/' + fileName;
                                
                                            if (sf.file.exists({ path: destinationFile }).exists) {
                                
                                                // Delete file
                                
                                                sf.wait({
                                                    intervalMs: 900,
                                                });
                                
                                                sf.ui.finder.menuClick({
                                                    menuPath: ["Finder", "Empty Trash"],
                                                });
                                
                                                sf.file.move({
                                                    sourcePath: destinationFile,
                                                    destinationPath: '~/.Trash/' + fileName
                                                });
                                
                                                // Copy file
                                
                                                sf.file.copy({
                                                    sourcePath: filePath,
                                                    destinationPath: `${destinationFile}`
                                                });
                                
                                            } else {
                                
                                                // Copy file
                                
                                                sf.file.copy({
                                                    sourcePath: filePath,
                                                    destinationPath: `${destinationFile}`
                                                });
                                
                                            }
                                
                                        });
                                    }
                                
                                    copyToFolder();
                                
                                });
                                
                      2. In reply toAndrew_Sherman:

                        @raphaelsepulveda any ideas on why i might be getting an error message when trying to move this folder from an external drive to the trash?

                        sf.file.directoryMove({
                            sourcePath: "/Volumes/WORK DRIVE/DDP/Player",
                            destinationPath: "~/.Trash/Player",
                        });
                        
                        1. @JOSEPH_BRANCIFORTE, I'm getting the same error and haven't encountered it before.

                          Maybe @chrscheuer can shine a light on this one.

                          1. You might have to do this differently, for example via sf.system.exec. I'm sure this is the OS reporting some kind of restriction in file moves with the API we're using.

                            1. Ah wait, no. I think all trashes are on each drive. So the destination path shouldn't be the trash on your system drive - it should be the trash on the external drive itself. The whole point of the trash is that it's a cheap move (no cross device moving).

                              1. It's probably called ".Trash" and is at the root of the volume, ie. something like this:

                                sf.file.directoryMove({
                                    sourcePath: "/Volumes/WORK DRIVE/DDP/Player",
                                    destinationPath: "/Volumes/WORK DRIVE/.Trash/Player",
                                });
                                
                                1. i think you're definitely correct that the issue is that each external volume has its own trash (never knew that!). however, the path format you suggested is still not working for me.

                                    1. this method does appear to work for a folder called 'test'.

                                      sf.file.directoryMove({
                                          sourcePath: "/Volumes/WORK DRIVE/test",
                                          destinationPath: "/Volumes/WORK DRIVE/.Trashes/501/test",
                                      });
                                      

                                      for some reason i'm still getting an error message that a folder "player" exists already in the Trash location, even though i've emptied by trash via Finder command, and don't see and files in the /Volumes/WORK DRIVE/.Trashes folder.

                                      strange... perhaps a reboot will do the trick... anyway, seems like this is pretty much solved... thanks @chrscheuer!