Move current selection to new folder with Applescript

Posted on 27 May 2009 (12:33 AM)

I often want to instantly move a selection of files into a newly created folder. The scenario is this: I select a bunch of files in Finder, click a button and *bam*: a new folder is created in the current location containing my selection.

This cannot be done natively, but I wrote an Applescript that does this for me.

Here's the complete script:

  1. tell application "Finder"
  2. try
  3. set theLocation to folder of front window
  4. set newFolder to make new folder at theLocation with properties {name:"New Folder"}
  5. move selection to newFolder
  6. set selection to newFolder
  7. end try
  8. end tell
  9. Download this code: /code/move-current-selection-to-new-folder-with-applescript1.txt

It's a very simple script, but here's a rundown of what it does:

  1. tell application "Finder"
  2. ...
  3. end tell
  4. Download this code: /code/move-current-selection-to-new-folder-with-applescript2.txt

This line tells Applescript I want to talk to the application "Finder".

  1. tell application "Finder"
  2. try
  3. ...
  4. end try
  5. end tell
  6. Download this code: /code/move-current-selection-to-new-folder-with-applescript3.txt

If something goes wrong in the script (for instance, when no Finder window is opened), the try block ensures no ugly error message pops up.

  1. tell application "Finder"
  2. try
  3. set theLocation to folder of front window
  4. set newFolder to make new folder at theLocation with properties {name:"New Folder"}
  5. ...
  6. end try
  7. end tell
  8. Download this code: /code/move-current-selection-to-new-folder-with-applescript4.txt

The next two lines save the location of the folder of the top Finder window in a variable, and create a new folder inside that one named "New Folder".

  1. tell application "Finder"
  2. try
  3. set theLocation to folder of front window
  4. set newFolder to make new folder at theLocation with properties {name:"New Folder"}
  5. move selection to newFolder
  6. set selection to newFolder
  7. end try
  8. end tell
  9. Download this code: /code/move-current-selection-to-new-folder-with-applescript5.txt

The last two lines move the current selection of files to the newly created folder and select the folder (for easy access, if you want to change its name for instance).

That's it! You can save this script as an .app and drag it into your Finder toolbar so you can access it directly from Finder.

Let me know if you got any improvements!

Back to top

Filed under Applescript

Comments:

  1. 14 June 2009 (06:39 PM) by Andrew

    would there be any way to adapt this so it searches the folder for an mp3 file and moves it to a new specified folder?

    this comment has been quoted by Harmen Janssen

  2. 15 June 2009 (02:54 AM) by Harmen Janssen

    Andrew wrote:

    would there be any way to adapt this so it searches the folder for an mp3 file and moves it to a new specified folder?

    I'm sorry Andrew, I'm sure there is, but my knowledge of Applescript is unfortunately very limited. I wouldn't know how to that.

  3. 12 September 2009 (01:06 AM) by AppleScriptNoob

    It works fine, except if you try to move a folder with it, the new folder will be created inside the selected folder.

    Is there a way to check if the selection is a folder, and then truncate the last level of the path so the new folder will be created at the same location as the selection?

    this comment has been quoted by Harmen Janssen

  4. 12 September 2009 (10:22 AM) by Harmen Janssen

    AppleScriptNoob wrote:

    It works fine, except if you try to move a folder with it, the new folder will be created inside the selected folder.

    Is there a way to check if the selection is a folder, and then truncate the last level of the path so the new folder will be created at the same location as the selection?

    Hm, thanks for reporting.

    I will get back on that, it's probably an easy fix.

Sorry, due to spam, comments are temporarily out of order.

Back to top

Preferences

These settings will be saved for your convenience.