18

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?

1
  • 1
    If she dragged the folder into the trash, then just drag it back out?
    – henryaaron
    Jan 25 '12 at 5:58
11

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.

4
  • 1
    I can't seem to "Put Back" files en masse. I can select either file and the option to put back is there but when I select both "Put Back" is not available. Thus I don't think you can "Put Back" en masse. Maybe with an applescript though... Feb 3 '13 at 16:12
  • 6
    This answer is incorrect. I'm in a state where any individual file can be "Put Back" but if I select more than one file I cannot, which is inconsistent with your answer.
    – Tom Swirly
    Nov 16 '14 at 0:46
  • Answer is indeed incorrect. I have fle A and file B in my trash. Selectin them individually gives me the put back option when right cliking, Selecting them both and then right clicking, the Put Back option is gone.
    – Wrench
    Nov 28 '18 at 23:48
  • Multi-select Put Back is working for me right now, on MacOS v10.15.4.
    – Chalky
    Jun 14 '20 at 23:31
5

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
1
  • For some reason this ran really slowly for me while the one by @ThierryKoblentz was about 50x faster. I didn’t explore why.
    – Rory
    Aug 27 '18 at 17:39
4

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
5
  • The OP is ~ 7.5 years old, so while not applicable to the OP's issue anymore, nonetheless it's nice to have an updated automated script that handles the releases of macOS that open a new window showing the put back file. You should probably, as a usage note, inform the user that in order to use this, one must not touch the computer until its finished because of the nature of UI Scripting. Doesn't necessarily have to be a message presented by the running of the script but as a note in the answer. Jun 18 '19 at 21:28
  • BTW I would have +1, but the bug in the repeat loop is unacceptable because it could be coded to handle the situation that causes it! Jun 18 '19 at 21:28
  • @user3439894 I Updated the code... Where is my +1? LOL
    – wch1zpink
    Jun 18 '19 at 22:37
  • 1
    See how easy that was! :) +1 Jun 18 '19 at 23:13
  • This worked like a charm when my wife accidentally deleted a load of files on her Macbook (Sierra, 10.12.6). Thank you for updating this despite the age of the OP!
    – KeithWM
    May 10 '20 at 12:28
3

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
2
  • 1
    This worked for me on macOS Sierra. FYI: key code 126 is up and key code 51 is backspace. You can modify the 3 lines key down ... key up ... to one: key code 51 using key command down. (This is adapted from another answer. Files restored to my desktop wanted my system password, which messed with this script. I cancelled those and manually restored those few.
    – Eric Hu
    Dec 20 '16 at 11:25
  • For me on MacOS Mojave, this solution was a nightmare. Every item that was put back from the trash, opened a new Finder window to that item's source folder. Then the code started selecting items in the current Finder window (some of which were folders with attached folder actions) which then got triggered.
    – wch1zpink
    Jun 18 '19 at 19:10
1

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.

1

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.

1
  • 1
    Wrong - this again does not actually "put back" the files and does not solve the issue, it merely takes them all and throws them into one big pile.
    – Tom Swirly
    Nov 16 '14 at 0:47
1

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.

1
  • 2
    Well, as you can easily deduce from other answers here, it is possible to put back files/folders from Trash: open Trash, right-click a file and select "Put Back".
    – nohillside
    Oct 15 '12 at 18:52
1

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.

1
1

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

  1. 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

  2. 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .