Is there a way to auto “put back” everything in Trash?
-
30-04-2021 - |
Question
My wife saw the folder 'all documents' in Finder and decided to move it all to the trash.
Now we have over 9,000 files in the trash and I haven't been able to find a way to put back everything with one click.
For some reason it will only let me 'put back' one file at a time.
Is there any way to automate this?
Solution
If "Put Back" is disabled for a multiple-item selection it means that (at least) one of the items selected doesn't have its original location information stored in the Trash directory's .DS_Store file.
Though it's less than ideal, try multi-selecting subsets of the files looking for groups that you can "Put Back" en masse before resorting to manual filing for the remaining files.
OTHER TIPS
Try running a script like this in AppleScript Editor:
repeat
tell application "Finder"
close windows
if items of trash is {} then return
open trash
activate
end tell
tell application "System Events"
key code 125 -- down arrow
key code 51 using command down -- command-delete
end tell
end repeat
If Finder shows a password dialog when you try to put back some item, try adding something like this to the end of the tell application "System Events"
block:
delay 1
if exists window 1 of process "SecurityAgent" then
tell window 1 of process "SecurityAgent"
set value of text field 2 of scroll area 1 of group 1 to "pa55word"
click button 2 of group 2
end tell
end if
delay 1
This AppleScript code works for me using the latest version of macOS Mojave.
This code will loop through every item in the trash, putting each item back to their original location.
If any of the original source folders of the files in the Trash no longer exist, the repeat until trashCount is 0
command will exit the loop. Any remaining files in the Trash will only be files that could not be put back because of this reason.
UPDATE
Since it is possible to select an item on your desktop during the repeat loop of the process of putting back files from the trash, the selected desktop item can get caught up in the process and be moved to the trash. To avoid this scenario, I added code which will lock the currently unlocked Desktop items and will also unlock them at the end of the script.
Because all Desktop items are now locked... During the process of putting back files from the trash, if for some reason you accidentally select a file or folder on your desktop and the code attempts to process that selected desktop item... It will generate a dialog window mentioning that item is locked and ask if you want to continue sending it to the trash. The System Events tell block towards the end of the script will handle any of those dialog boxes which may have been generated.
property desktopFolder : path to desktop
property unlockedFiles : missing value
tell application "Finder" to set trashCount to count of every item of trash
tell application "Finder"
set unlockedFilesRef to a reference to ¬
(items of desktopFolder whose locked is false)
set unlockedFiles to contents of unlockedFilesRef
try
set locked of unlockedFilesRef to true
end try
end tell
repeat until trashCount is 0
tell application "Finder" to set orphanCount to ¬
count of every item of trash
putFilesBack()
tell application "Finder" to set trashCount to ¬
count of every item of trash
if orphanCount is equal to trashCount then exit repeat
end repeat
delay 0.3
try
tell application "Finder" to close window "Trash"
end try
delay 0.3
tell application "System Events"
repeat until not (exists of button "Stop" of scroll area 1 ¬
of window 1 of application process "Finder")
if exists of button "Stop" of scroll area 1 ¬
of window 1 of application process "Finder" then
click button "Stop" of scroll area 1 of window 1 ¬
of application process "Finder"
end if
end repeat
end tell
tell application "Finder"
close every Finder window
delay 0.5
repeat with i in unlockedFiles
set locked of i to false
end repeat
end tell
on putFilesBack()
global trashFiles, trashCount, thisItem
tell application "Finder"
set trashFiles to every item of trash
set frontmost to true
repeat while not frontmost
delay 0.1
end repeat
my closeFinderWindows()
end tell
delay 0.1
tell application "System Events"
tell application process "Finder"
repeat with i from 1 to count of trashFiles
set thisItem to item i of trashFiles
delay 0.1
set frontmost to true
select thisItem
delay 0.1
try
key code 51 using {command down}
end try
delay 0.1
my closeFinderWindows()
delay 0.1
end repeat
end tell
end tell
tell application "Finder" to set trashCount to count of every item of trash
end putFilesBack
on closeFinderWindows()
tell application "Finder"
set finderWindowRef to (a reference to ¬
(every Finder window whose name is not "Trash"))
set finderWindowRef to contents of finderWindowRef
close (items of finderWindowRef)
end tell
end closeFinderWindows
An AppleScript that "Put[s] back all items in the Trash" worked for me:
Open "AppleScript Editor" and copy/paste the lines below, then run the script as many times as needed.
tell application "System Events"
tell process "Finder"
repeat 100 times
tell application "Finder" to open trash
tell application "Finder" to activate
key code 126
key down command
key code 51
key up command
delay 0.2 -- adjust delay as needed
end repeat
end tell
end tell
tell application "Finder" to close every window
you should be able to highlight every file or at least do it in incremental batches, copy them then re-paste them I believe. I just tried it and if you double click your trash can then right click the files you wish to restore there is a "put back" option which only works file by file, as said i would highlight then press C-c (command c)to copy then C-v(command v) to paste them back.
This worked for me:
- Create a new folder in Finder, I called mine "recovered files"
- Open Trash folder and select a group of files
- Copy files and paste into "recovered files" folder.
If you need to put back a large folder, or a large number of individual files, use this technique. If you need only 1 or 2 files, just use the "put back" feature.
Prior to Snow Leopard, OS X does not natively have the ability to restore files to their original locations that they were deleted from (as can be done natively in a Windows environment with the "Restore" option in the context menu for Recycle Bin). I accidentally did the same thing as your wife did at one point with around 10,000 plus files being deleted.
After exploring all my options I performed a system restore via Time Machine. It was by far the most expedient method to get said files to their proper locations.
Drag all files from Trash to All My Files tab in Finder. It will take a while if you have a lot of files. We tested on 10000+ files. Finder will restore all files to its original location.
Based on a near solution that @thierry already gave here from Giacomo Balli, we created this solution to help several of you, since the problem still remains. When you delete a file in MacOS, its original locations is stored in the .DsStore
hidden files in case you want to restore it. Recent versions of macOS have the ability to put stuff in your Trash back where it came from, lickety-split, and it’s way faster than dragging and dropping. So as you know, if you click on the trash icon in your Dock, you will see the items you have, well, trashed. Right-click or Control-click on any file (or hold down Command to click and select multiple items, then Control-click), and you will see the Put Back option. The only problem is that it let us do it for only one item at the time.
Solution
Start by cloning the repository to your local machine so you may begin to use our solution;
git clone git@github.com:opprDev/trash-back.git
cd trash-back
Run the AppleScript on that computer, via the osascript command;
osascript scripts/trash-back.scpt
Conclusions
Running AppleScript greatly increases what you can do remotely from the command line, and enables a lot of cool tricks that are hard to do otherwise. While you can run whole scripts in the command line, this way of running is only about running can also log into a remote computer (using ssh) and run the AppleScript on that computer, via the osascript
command. The osascript
command can also run any other Open Scripting Architecture language using the -l modifier.