Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create a new folder in the current list-view directory Desktop
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
try
  tell application "Finder"
    set selectedItem to selection
    set currentPath to ((the first item of the selectedItem) as alias)
    set parentPath to ""
    
    if (currentPath as string) ends with ":" then
      -- it is a folder
      set the parentPath to currentPath
    else
      -- it is a file
      set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
      set the parentPath to (text items 1 thru -2 of (currentPath as string)) as string
      set AppleScript's text item delimiters to od
    end if
    
    set newFolder to (my createFolder(parentPath))
    
  end tell
on error -- no folder or file is selected
  tell application "Finder"
    set the currentPath to (folder of the front window as alias)
    set parentFolder to currentPath
    
    set newFolder to (my createFolder(currentPath))
    
  end tell
end try


on createFolder(folderLocation)
  tell application "Finder"
    set thisFolder to make new folder at folderLocation
    
    set selection to thisFolder
    tell application "System Events"
      keystroke return
      quit -- if i don't do this system events seems to hang???
    end tell
    return thisFolder
  end tell
end createFolder
(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.]
    •    
  • Currently 2.33 / 5
  You rated: 4 / 5 (9 votes cast)
 
[25,203 views]  

Create a new folder in the current list-view directory | 14 comments | Create New Account
Click here to return to the 'Create a new folder in the current list-view directory' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create a new folder in the current list-view directory
Authored by: NaOH-Lye on Nov 25, '08 08:06:12AM

That solves a long-standing annoyance I've had. Thanks. Works great in Keyboard Maestro as well.



[ Reply to This | # ]
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

---
FileMaker Developer
San Diego

[ Reply to This | # ]

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.

Cheers,
David



[ Reply to This | # ]
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)

Hope this helps others.



[ Reply to This | # ]
Create a new folder in the current list-view directory
Authored by: zacht on Nov 25, '08 09:17:10AM

This is really nice! Thanks!



[ Reply to This | # ]
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.



[ Reply to This | # ]
Create a new folder in the current list-view directory
Authored by: mael on Nov 25, '08 12:12:25PM

Dr. T - YOU MADE MY DAY!!!!!!
GREAT!
*JUST* what I was looking for...



[ Reply to This | # ]
Create a new folder in the current list-view directory
Authored by: dandj on Nov 25, '08 08:39:25PM

Dr T, Good call, but line 4 (Command-[) didn't work for me.

However, Command-W (Close Window) returns to the previous (open) window.



[ Reply to This | # ]
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.



[ Reply to This | # ]
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.

This in 10.5.5.

Why the hint? Cute as it may be it seems unnecessary.....

[ Reply to This | # ]

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)



[ Reply to This | # ]
Some readers seem to be confused
Authored by: Dr. T on Nov 26, '08 10:43:13AM
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.

        [ Reply to This | # ]
        Create a new folder in the current list-view directory
        Authored by: kyngchaos on Nov 26, '08 10:26:52AM

        Sorry, no go. In list view, select a folder, click New Folder button, new folder created in the window's folder, that is, at the top level on the window. NOT inside the folder selected in the list.

        Tested on OSX 10.4.11 and 10.5.5. I even tried expanding the selected folder, just in case that was needed.



        [ Reply to This | # ]
        Path Finder
        Authored by: swilcox on Nov 27, '08 08:10:13AM

        FWIW: Path Finder does this correctly.



        [ Reply to This | # ]