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

11

3

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?

xdazz

Posted 2012-10-15T14:09:29.670

Reputation: 243

1

Similar: Is there a way to auto “put back” everything in Trash? — Doesn't seem to be possible easily.

– slhck – 2012-10-15T15:22:33.837

1Maybe you can hack up an Automator script that can do it. – billc.cn – 2012-10-15T20:09:56.453

Answers

3

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

valenok

Posted 2012-10-15T14:09:29.670

Reputation: 131

Wow, this was the only option that worked for me! Thanks – Andre Soares – 2016-01-12T10:50:02.193

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

Lri

Posted 2012-10-15T14:09:29.670

Reputation: 34 501

0

If it is the last thing you did in the Finder, then using "Undo" would be the preferred method.

There is no built-in way within the GUI to perform the action you are discussing.

This is all assuming that you have not emptied the Trash.

Jonathan

Posted 2012-10-15T14:09:29.670

Reputation: 78

0

'Put Back' multiple items in Trash https://gist.github.com/faresd/5661253

Faresd

Posted 2012-10-15T14:09:29.670

Reputation: 11

2This looks a little hacky. Could you explain what the script does, for a little more context? That'd be much appreciated. – slhck – 2013-05-28T08:33:24.637

2From what I can see, the script tries to close Finder until it is closed, then it reopens it to the trash and puts back the first item in there. After that, it closes all the windows, and repeats. This script uses System Events, which is a very high level of doing something with an AppleScript (it'd be better to do something without taking over the computer interface). If I were the answerer, I would look for a lower-level solution to contribute. – Carter Pape – 2013-05-28T13:43:56.170

-1

Select all the files you want to put back - Apple Key+A for Select All, then right-click over an item and select Put Back. This will put back multiple items at a time.

A C

Posted 2012-10-15T14:09:29.670

Reputation: 9

5When you selected all, the Put Back menu will disappear. – xdazz – 2012-10-15T15:41:56.170

I'm on OS X 10.7.5 and it shows for me. Try it - select all the files in the trash and right click, "Put Back" is there. – A C – 2012-10-15T16:08:34.647

4

It will work if (and only if) all the files are from the exact same folder. Try deleting files from different sources – it won't work. http://i.stack.imgur.com/q56wI.png

– slhck – 2012-10-15T16:10:26.213

True - you are right. I should have tried more variations. Thanks. – A C – 2012-10-15T16:13:12.627