Skip to content

Instantly share code, notes, and snippets.

@markoa
Created July 5, 2023 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markoa/8a4ba77c657a2c17b397d6eace72e23d to your computer and use it in GitHub Desktop.
Save markoa/8a4ba77c657a2c17b397d6eace72e23d to your computer and use it in GitHub Desktop.
How to create a custom macOS Finder action to create a text file inside a folder

Being able to create plain text files easily anywhere in Finder helps with using the iCloud-backed native file system for personal knowledge management. It's my preferred alternative to third-party apps whose longevity may be uncertain.

Create an Automator service

  1. Open Automator.
  2. From Library, choose "Utilities", then "Run AppleScript" action.
  3. At the top of the workflow window, you'll see a few dropdown menus. Set the first one ("Workflow receives current") to "files or folders", and the second one to "Finder".
  4. Paste the following AppleScript code:
on run {input, parameters}
    
    set thePath to POSIX path of (first item of input as text)

    if (thePath does not end with "/") then
        set thePath to thePath & "/"
    end if

    set fileName to "untitled"
    set fileExtension to "txt"
    set counter to 1
    set theFile to thePath & fileName & "." & fileExtension

    tell application "Finder"
        repeat
            try
                if exists (POSIX file theFile) then
                    set counter to counter + 1
                    set theFile to thePath & fileName & " " & counter & "." & fileExtension
                else
                    exit repeat
                end if
            on error
                exit repeat
            end try
        end repeat
    end tell

    do shell script "touch \"" & theFile & "\""
	
	delay 0.1

    set hfsPath to POSIX file theFile as text

    tell application "Finder"
        activate
        select file hfsPath
    end tell

    delay 0.1

    tell application "System Events"
        keystroke return
    end tell

    return input
end run

Save the workflow: Go to File > Save and give your service a name, like "New Text File".

Now, when you right-click on a folder in Finder, you should see the option "New Text File" at the bottom of the context menu, in the 'Quick Actions' submenu.

To create a keyboard shortcut

  1. Open "System Preferences".
  2. Click on "Keyboard".
  3. Choose the "Shortcuts" tab.
  4. In the list on the left, select "Services" or "Quick Actions" (depends on your macOS version).
  5. Scroll down to find your script under the "Files and Folders" section. It should have the same name as you saved it in Automator.
  6. Click on the "Add Shortcut" button or the area to the right of the script name, then press the key combination you want to use as the shortcut. Be sure to choose a shortcut that doesn't conflict with any other shortcuts used by Finder or any other apps.
  7. Restart Finder: option-right-click on the Finder icon in the Dock and select "Relaunch".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment