Move Selected Files into a New Folder using AppleScript and Automator


Mac users, do you find yourself doing this more often than you like? You select a batch of files or folders, then realize you want to put these items into a folder to facilitate moving them about. You have to back out of your selection, create the folder, then move the files into the newly created folder. It occurred to me recently when I was doing just this that there had to be a better way. Then I remembered Services, the built-in control click method of performing actions in the Finder, or within Applications. A Google search revealed that, in fact, someone had already written this AppleScript. The following post documents how to make such a Service from an AppleScript.

Build and test the Applescript

media_1313466134974.png

The basic steps are:

  1. Write (or copy) your AppleScript code.
  2. Copy the AppleScript into an Automator step
  3. Save the Automator script as a Service

AppleScript Code for this Service

–Credit where credit is due:
–capitalj 04-16-2007, 10:16 AM
http://hintsforums.macworld.com/archive/index.php/t-71077.html

try
    tell application “Finder” to set the this_folder to (folder of the front window) as alias
on error — no open windows
    set the this_folder to path todesktop folderasalias
end try

tell application “Finder”
    –grab the selected files and put them into a variable
    set selected_items to selection
    –prompt for a folder name.    
    set thefoldername to text returned of (display dialog “Folder name:” default answer “My New Folder”)
    –make the new folder in the same directory.
    set theFolder to (makenewfolderatthis_folderwith properties {name:thefoldername})
    –loop to move the selected files into the newly created folder.
    repeat with x in selected_items
        movextotheFolder–changed the variable from “thefoldername” to “theFolder”
    end repeat
    
end tell

Launch Automator and create a new Service

media_1313466455421.png

When Automator launches, it offers up different types of Templates to get you started. Select the ‘Service’ option.

Add the ‘Run AppleScript’ step to your Automator Routine

media_1313466226135.png
  1. Click on Utilities
  2. Click on Run AppleScript
  3. Paste in the AppleScript into the text area provided.
  4. Set the Service to run when Files or Folders are selected in the Finder.

Save the Automator Service

media_1313466560325.png

Save the Service and it will now show up in your Services Menu

Test the Service

media_1313466653488.png

Select the files you wish to move into a new folder.

Control click the files and a contextual menu will popup

media_1313466835786.png

Go to the bottom of the popup list and look for ‘Services’ and then choose your newly added Service. I named mine ‘Move Selected Items into Folder’.

Prompt for Folder Name

media_1313466877206.png

You will be prompted to name your folder. This part of the script may run a little slowly, because of the overhead involved with running an AppleScript and Automator. Be patient or purchase a faster computer.

Rename the Folder

media_1313466898733.png

Enter a meaningful name for your folder. Your selected files will show up in this folder.

Files Moved into the New Folder

media_1313466910814.png

While researching this article, I came across a small donationware app called ThisService that promises to cut Automator out of the equation. I tried it, but couldn’t get the newly created Service to show up in the contextual popup Services menu.

Hope you find this useful. I know it is going to make life easier for me. Thanks go out to ‘capitalj’ on the Macworld Forums for creating the working script.

Related Articles:
Growl Integration with FileMaker
Shortening URLs using FileMaker

Update: 2013-01-05 Since upgrading to OS X Mountain Lion, there is now a menu command to gather files into a newly created folder.

New-Folder-with-Selection

New-Folder-to-Selection2

4 Responses to “Move Selected Files into a New Folder using AppleScript and Automator”

  1. i love it…but, instead of prompting for a new folder always… I used ‘set theFolder to choose folder’ and I can reuse folders already in use elsewhere….

    Charles

    • I’ve tried this in Lion, and it always comes up with error in applescript…

      I would actually prefer Charles Huff’s script to choose/else make new folder but have no clue with Applescript, can someone paste the exact applescript that I can copy then paste exactly into Automator “run Applescript” step

  2. Hi Douglas, great article.
    We have put some related AppleScript on CoderRater.net and would welcome your input.

    I hope this helps your users find the solution they are looking for.

    http://www.coderater.net/Moving%20Files/AppleScript/2013/01/Move-Files-and-Folders-43.html

Trackbacks/Pingbacks

  1. Replacing a Time Machine Backup Hard Drive | HomeBase Software - July 21, 2012

    […] Articles: ScreenSteps vs MacSnapper Move Selected Files into a New Folder using AppleScript and Automator […]

%d bloggers like this: