How can I move a named file from a known location in Finder to the trash?
- Raphael Sepulveda @raphaelsepulveda2021-04-09 16:15:38.242Z
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' });
- AAndrew Sherman @Andrew_Sherman
Brilliant, thank you. I was getting stuck with the location for trash.
- In reply toraphaelsepulveda⬆:AAndrew Sherman @Andrew_Sherman
Raphael, is there a way to get this to overwrite if the file already exists in the destination location?
Raphael Sepulveda @raphaelsepulveda2021-04-10 16:00:42.026Z
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?
- AAndrew Sherman @Andrew_Sherman
Ok no worries; what I'll do is empty the trash first. Thanks!
- TTJ Allen @TJ_Allen
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
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!
- In reply toTJ_Allen⬆:AAndrew Sherman @Andrew_Sherman
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(); });
- In reply toAndrew_Sherman⬆:JOSEPH BRANCIFORTE @JOSEPH_BRANCIFORTE
@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", });
Raphael Sepulveda @raphaelsepulveda2022-09-26 17:02:29.820Z
@JOSEPH_BRANCIFORTE, I'm getting the same error and haven't encountered it before.
Maybe @chrscheuer can shine a light on this one.
Christian Scheuer @chrscheuer2022-09-26 17:04:34.674Z
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.Christian Scheuer @chrscheuer2022-09-26 17:06:25.431Z
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).
Christian Scheuer @chrscheuer2022-09-26 17:09:04.712Z
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", });
JOSEPH BRANCIFORTE @JOSEPH_BRANCIFORTE
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.
Christian Scheuer @chrscheuer2022-09-26 17:16:29.839Z
JOSEPH BRANCIFORTE @JOSEPH_BRANCIFORTE
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!