Last active
September 21, 2023 22:37
-
-
Save gruber/3cd4c8cbddcee6e99490 to your computer and use it in GitHub Desktop.
Finder: Use Cmd-N to Create a New Folder or a New Finder Window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
This script is for anyone who still has the classic Mac OS era habit of using | |
Command-N to create a new folder in the Finder. It's intended for use with | |
Red Sweater Software's excellent FastScripts utility -- a replacement for the | |
system's build-in Scripts menu that allows for assigning keyboard shortcuts | |
to scripts, on a per-application basis. | |
FastScripts: https://red-sweater.com/fastscripts/ | |
Install the script here: ~/Library/Scripts/Applications/Finder/ | |
In FastScripts, assign the script a shortcut of Command-N. | |
Now, whenever you invoke Command-N in the Finder, you'll get prompted to | |
choose between creating a new folder or opening a new Finder window. This | |
sounds like a pain in the ass, but I've been using this script for years, and | |
however annoying it is to add an extra confirmation step each time I create | |
a new folder or window, it's far less annoying than getting a new unwanted | |
window every time I try to create a new folder. | |
Version 1.1, 16 Mar 2017 | |
- Added a property to remember the previous selection, and default to that the next time you invoke the script. | |
*) | |
property last_selection : "" | |
tell application "Finder" | |
choose from list {"Folder", "Window"} default items {last_selection} with title "New" with prompt "What:" OK button name "Create" without multiple selections allowed and empty selection allowed | |
try | |
set _what to item 1 of the result | |
if _what is "Folder" then | |
my make_new_folder() | |
else if _what is "Window" then | |
my make_new_finder_window() | |
end if | |
set last_selection to _what | |
end try | |
end tell | |
on make_new_folder() | |
tell application "System Events" | |
tell process "Finder" | |
click menu item "New Folder" of menu 1 of menu bar item "File" of menu bar 1 | |
end tell | |
end tell | |
end make_new_folder | |
on make_new_finder_window() | |
tell application "System Events" | |
tell process "Finder" | |
click menu item "New Finder Window" of menu 1 of menu bar item "File" of menu bar 1 | |
end tell | |
end tell | |
-- Could do this: | |
-- tell application "Finder" to make new Finder window | |
-- But that opens at the root. UI scripting lets the Finder give you exactly what New Window gives you from the menu. | |
end make_new_finder_window | |
on make_new_folder_old() | |
-- Doing it this way doesn't add the New Folder action to the Undo stack | |
-- And you need UI scripting to get the "untitled folder" text selected anyway | |
tell application "Finder" | |
set _location to folder of front window | |
set _new_folder to make new folder at _location | |
set selection to _new_folder | |
tell application "System Events" | |
tell process "Finder" | |
key code 36 -- return | |
end tell | |
end tell | |
end tell | |
end make_new_folder_old |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment