Move selected Finder items to new folder

Over the years, there have been several ways—either with third-party utilities or AppleScripts—to select a bunch of files in the Finder and then collect them all in a new folder. But these solutions have come and gone with the fortunes of developers and the changes in OS X. And there’s always been a wrinkle: No matter what kind of tool you’ve used, it’s always been hard to apply these tools or scripts to files in certain restricted folders, such as the Trash. A couple of OS X Hints readers came up with an AppleScript that both does the job of collecting files and solves the restricted-folder problem.

It’s easiest to implement their script as a Finder service, which you can run from the Finder’s contextual menu. Open Automator, selecting the Service template. At the top of the right-hand editing window, set the service to receive selected files or folders as input in the Finder. Find the Run AppleScript action (in the Utilities section at the far left) and drag or double-click it to add it to the workflow. Now copy the code below and use it to replace (* Your script goes here *) in the action’s editing pane:

try
    tell application "Finder" to set the this_folder to (folder of the front window) as alias
on error
    set the this_folder to path to desktop folder as alias

end try

tell application "Finder"
    set selected_items to selection
    set thefoldername to text returned of (display dialog "Folder name:" default answer "new folder")
    set theFolder to (make new folder at this_folder with properties {name:thefoldername})
    repeat with x in selected_items
        move x to theFolder
    end repeat

end tell

Save the service. To test it, go to the Trash, select a few deleted files, right-click on them, and select your service from contextual menu. The selected files should be moved to a new folder on your desktop; you can move it wherever you like from there.

Subscribe to the MacWeek Newsletter

Comments