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:
tell application "Finder"tryset theLocation to folder of front windowset newFolder to make new folder at theLocation with properties {name:"New Folder"}move selection to newFolderset selection to newFolderend tryend tell- 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:
tell application "Finder"...end tell- 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".
tell application "Finder"try...end tryend tell- 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.
tell application "Finder"tryset theLocation to folder of front windowset newFolder to make new folder at theLocation with properties {name:"New Folder"}...end tryend tell- 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".
tell application "Finder"tryset theLocation to folder of front windowset newFolder to make new folder at theLocation with properties {name:"New Folder"}move selection to newFolderset selection to newFolderend tryend tell- 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!
Filed under Applescript
- ← previous article: Quick PHP Tip: passing by reference creates array keys
- → next article: Disable fancy errors in CakePHP
Comments:
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
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.
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
Hm, thanks for reporting.
I will get back on that, it's probably an easy fix.