Is there a way to auto "put back" everything in Trash?

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

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.

Solution 2:

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

Solution 3:

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