I work almost exclusively in Finder's List View, which has an annoying folder creation "feature" that I dislike. Once you've used the Right Arrow to drill down into a few subfolders, when you hit Command-Shift-N to create a new folder, that folder will be created at the root folder of the Finder's current view, instead of in the folder you're looking at.

What I wanted to do was select any folder, press a keyboard combo, and have a new folder created inside that currently-selected folder. Similarly, selecting a file and hitting a shortcut would create a new folder where that file is located. So I set to work with AppleScript; here's the code

(Please note: I'm no uber-scripter, so there may be simpler ways of doing this.) There does seem to be a bug where Finder will not select the newly created folder. Also, to make this work via the keyboard, you'll want to use something like Spark to hook up a keyboard shortcut to this script.

[robg adds: I tested this using Butler, and it worked as described. This much older hint explained how to create a new file in the currently-selected folder via AppleScript.]

Create a new folder in the current list-view directory
Authored by: Fenton on Nov 25, '08 09:10:05AM

I'm afraid the original did not work for me when I selected a file; I believe because the path is set to a string instead of an alias. I am on 10.4.11, if that matters. In any case, there is a shorter way to get the parent folder, using the class "container," which allows it to remain an alias (like current Path). I also used class to check whether the selection is a folder. I didn't have the problem needing quit for System Events, but I used the more usual AppleScript exit of "return." I don't know how to make the name selected at the end either :-]

tell application "Finder"
try
set selectedItem to selection
set currentPath to ((the first item of the selectedItem) as alias)
if class of currentPath is folder then
-- it is a folder
set the parentPath to currentPath
else
-- it is a file
set parentFolder to container of currentPath
end if
set newFolder to (my createFolder(parentFolder))
on error -- no folder or file is selected
set the parentFolder to (folder of the front window as alias)
set newFolder to (my createFolder(parentFolder))
end try
end tell
on createFolder(folderLocation)
tell application "Finder"
set thisFolder to make new folder at folderLocation
set selection to thisFolder
tell application "System Events"
keystroke return
return
end tell
end tell
end createFolder

Create a new folder in the current list-view directory
Authored by: dhjdhj on Jun 12, '10 04:18:09PM

I'm no expert on applescript (indeed trying to learn it right now) but that "as alias" does not seem to work correctly. In particular, I selected a folder and this script always seemed to create new folder at the same level as selected folder, not inside it.

I inserted the line
set foo to class of currentPath
and took a look at the value of foo. Its value is "alias" and so the test to see if "class of currentPath is folder" can never succeed as far as I can tell.

I'm sure I'll figure it out after reading a bit more about applescript but unless I'm missing something, I can't see how this script could ever have worked when a folder is selected.

Create a new folder in the current list-view directory
Authored by: dhjdhj on Jun 12, '10 04:29:37PM

The line

set the parentPath to currentPath

seems useless...... parentPath is not used anywhere (unless it's a special system variable that I don't know about yet).....however, changing parentPath to parentFolder actually fixes the problem (at least in the case where the selected item was a folder, which is all I care about)

Create a new folder in the current list-view directory
Authored by: Dr. T on Nov 25, '08 10:18:12AM

This same task can be accomplished quite easily without AppleScript and without touching the mouse.

1. With enclosing folder highlighted, type command-O. (The Finder view changes.)
2. Type command-shift-N to create a new folder.
3. Type the name of the folder followed by Enter or Return.
4. Type command-[ to return to the previous Finder view.

These steps can easily be automated (in QuicKeys or Automator or similar programs) and mapped to a keystroke combination.

Create a new folder in the current list-view directory
Authored by: sjmills on Nov 25, '08 09:26:03PM

The command-[ thing probably depends on your "Always open folders in a new window" pref. If you've opened a new window, command-[ has nothing to do. But if you've changed to the new view within the same window, then it will "go back".

Also, I'd change the wording of this hint from "Right Arrow to drill down into a few subfolders" to "right arrow to twist open the disclosure triangle of a few levels of folders". When I think of "drill down", I think of the old, common practice of opening one item while closing the parent item, as in command-option-downArrow, not expanding the viewable contents of the current item.

Create a new folder in the current list-view directory
Authored by: rgray on Nov 26, '08 06:19:20AM

I don't get this hint...???

I always use the list view in Finder and I customised it so the 'New Folder' button shows in the tool bar. Hit that 'button' and bang the new folder is created inside the selected folder. I played with this about 100 times yesterday after reading the hint and it works 100% of the time.

Create a new folder in the current list-view directory
Authored by: Satinder S Sidhu on Nov 26, '08 08:31:00AM

I agree with rgray.

I tested new folder creation in list view in a folder 9 levels deep from root, WITH and WITHOUT New Folder… button in the toolbar. Even relaunched Finder after changing the toolbar; new folder ALWAYS appeared inside the current folder. (10.5.5)

Here's how to see the problem.
Open your Documents folder and switch to list view. Highlight a folder that you know will have other folders inside. (We'll call it Folder 1.) Click the triangle to the left of Folder 1: The list of files and folders in Folder 1 will be indented. Select a folder from those in the indented list. (We'll call it Folder 2.) Click the triangle to see a more indented list of files and folders. With Folder 2 still highlighted, type command-shift-N or click on the New Folder button in the toolbar. A new "Untitled" folder is created within the Document folder. This occurs regardless of Finder preferences.
I beleive some people misunderstood the problem and were double-clicking on folders in list view instead of using the list display triangles.
The hint I posted earlier gets around the problem by opening the selected folder, creating the new folder there, then switching back to the previous list view. Apple chose the logic of its file system instead of the preferences of the users. Users who have a nested folder highlighted rarely want to make a new folder at the top level, they want it within the selected folder. We should be able to change this behavior within Finder preferences.