Create a new (.txt) File in Finder - Keyboard Shortcut

I wanted to know if there is way to set keyboard shortcuts to make new files in Finder. For example, by default, you can create a new folder using CMD+Shift+N. Is there a keyboard shortcut to create a new text file? I'm a programmer so this would be extremely useful

I would also be interested in creating other file type using keyboard shortcuts.

I have attempted to use automator but I'm finding it quite confusing and I'm not sure how I would connect a keyboard shortcut to it.


Solution 1:

Well, here you go with an AppleScript for that.

First, create the AppleScript:

  1. Open Automator
  2. Create a Quick Action
  3. Set the input to no input
  4. Drag and Drop the Run AppleScript workflow element onto the grey space.
  5. Paste the code from below into the AppleScript
  6. Save the workflow as Create new file
    If you have iCloud Drive activated, make sure you are saving the file under Library/Services/ in your Home Folder.

try
  tell application "Finder" to set the this_folder ¬
   to (folder of the front window) as alias
on error -- no open folder windows
  set the this_folder to path to desktop folder as alias
end try

set thefilename to text returned of (display dialog ¬
 "Create file named:" default answer "filename.txt")
set thefullpath to POSIX path of this_folder & thefilename
do shell script "touch \"" & thefullpath & "\""

To add it as a shortcut:

  • Go to System Preferences -> Keyboard -> Shortcuts -> Services
  • Scroll down until you find the Service Create new file
  • Assign a shortcut to it by clicking on the right side none, which turns into a Add Shortcut.
    Click the Button and type the shortcut you wish to use.
    I use ⌘ Command+⌥ Option+N

Solution 2:

I've created an AppleScript very similar to the @YoshiBotX's one, but with some improvements.

The idea is to create an Automator workflow and assigning a shortcut to it using the following steps:

  • Open Automator and create a Service;
  • Set the input to no input, and the application to Finder.app;
  • Drag and Drop the Run AppleScript workflow element onto the grey space;
  • Put the contents of this AppleScript in the textbox;
  • Save the workflow with a reasonable name (like New File);
  • Go to Settings -> Keyboard -> Shortcuts -> Services and assign a shortcut to it.

Now, let's show the AppleScript:

set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false

-- get folder path and if we are in desktop (no folder opened)
try
    tell application "Finder"
        set this_folder to (folder of the front Finder window) as alias
    end tell
on error
    -- no open folder windows
    set this_folder to path to desktop folder as alias
    set is_desktop to true
end try

-- get the new file name (do not override an already existing file)
tell application "System Events"
    set file_list to get the name of every disk item of this_folder
end tell
set new_file to file_name & file_ext
set x to 1
repeat
    if new_file is in file_list then
        set new_file to file_name & " " & x & file_ext
        set x to x + 1
    else
        exit repeat
    end if
end repeat

-- create and select the new file
tell application "Finder"

    activate
    set the_file to make new file at folder this_folder with properties {name:new_file}
    if is_desktop is false then
        reveal the_file
    else
        select window of desktop
        set selection to the_file
        delay 0.1
    end if
end tell

-- press enter (rename)
tell application "System Events"
    tell process "Finder"
        keystroke return
    end tell
end tell

For convenience, I'm putting this AppleScript in this GitHub Gist.