How to restore all the files from trash in Mac OS X?

I can put back the items one by one, but there are too many files, how could I restore all the files in the trash?


Solution 1:

MacOS keeps file meta information about deleted files in ~/.Trash/.DS_Store, which also contains records of the original locations. I've written a perl script that scans ~/.Trash/.DS_Store file and prints commands to move all files back to their original location. The output can be fed directly to shell.

Perl script: https://gist.github.com/cpq/3d58e144a3fc2e47c54a

To run, download script, start terminal and type perl restore_mac_trash.pl

Solution 2:

Here is another AppleScript like the one posted by user227282:

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

You can run the script by pasting it to AppleScript Editor and pressing command-R. I didn't need any delays.

If Finder shows a password dialog when it tries 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