Use a Shortcut To Create a New Text File In a Folder On a Mac

There is no command in the Finder that will let you create a new blank file at your current location. But you can create a Shortcut that will let you do this each time easily. The Shortcut needs to use both JavaScript and shell scripting to get the job done.



Link to the Shortcut.

Comments: 44 Responses to “Use a Shortcut To Create a New Text File In a Folder On a Mac”

    Jacques Du Biel
    1 year ago

    Hi Gary,
    I just tried this shortcut, and when I run it it errors out on the decodeURI javascript call with a "Error: Error: Can't get object.". Am I missing something?
    Thanks

    1 year ago

    Jacques: Make sure you are following along with everything and using it exactly as I am.

    Roger
    1 year ago

    I added the Ask for Input and now get this error message: folder=/Users/rogermas/Documents/Testfolder/: Command not found.
    name=New: Command not found.
    extension=.txt: Command not found.
    folder: Undefined variable.

    1 year ago

    Roger: All I can suggest is to start over and check everything carefully. I can't debug it for you from afar.

    Bill Toney
    1 year ago

    Instead of a Text File, would this work in opening other file designations. Would this be as simple as changing .txt in the Shortcut to .xlsx to create a new Excel file?

    Roddy
    1 year ago

    Gary, I appreciate all you've contributed throughout the years. I've learned so much since I first started using Macs in the 1980s! I always recommend to those I help with their Apple computer/tech questions to go watch your tutorials.

    I added the Input text prompt to the shortcut script link you've posted as stated in the video. I'm on Ventura, and at first, the two script windows within Shortcuts didn't have permission to run, but a simple click on the message enabled it.

    1 year ago

    Bill: No. Plain text files are unique in that an empty one is simply a completely empty zero-bytes-long file. Other apps create "empty" files that are very different from that.

    Elliot Kaufman
    1 year ago

    I tried Bill Toney's workaround for Word, Excel, Photoshop and Keynote files. The modified shortcut created a new.*** file for all of these. However, only the .docx Word file was able to be opened by its host application. All of the others were not seen as files that could be opened.

    Sheldon Hough
    1 year ago

    And...thanks bunches...I did not know one could comment at your site instead of YouTube....

    Joe
    1 year ago

    Hi Gary, I slightly modify this shortcut to make a small script to copy and paste onto terminal for a ffmpeg audio conversion, but I am having problems when there are empty spaces in the finder folder, if the finder folder does not have empty spaces in the name it works but if it does have, terminal throw an error "No such file or directory"
    Also how can I make shell script to make ffmpeg conversions directly? Thank you

    1 year ago

    Joe: When writing Shell scripts, you need to enclose paths with spaces in quotes, or escape the spaces with a \ like My\ Folder\ Name.

    Brian Oxley
    1 year ago

    Hi Gary - this shortcut would be a Godsend for me. I can't find where to download it. Could you post a link. Sorry if it is staring me in the face!!

    1 year ago

    Brian: Look under the video and above the comments for the "Link to the Shortcut."

    Mike
    1 year ago

    I downloaded the Shortcut and tried adding the 'Ask For Text' prompt per the video. But when I run the Shortcut, I can't type into the prompt. I'm using macOS Monterey 12.6.8. Please advise. Thanks.

    1 year ago

    Mike: Monterey is going to be different, for sure. But you should still be able to type in it. Can you click in the field and then type?

    Mike
    1 year ago

    Hi Gary. No, I'm not able to click in the prompt and type. I was able to right-click and paste into it, but no typing.

    1 year ago

    Mike: No explanation for that. Try a restart and see if it persists.

    Steve
    1 year ago

    Can you add one more line to open the new.text file with TextEdit?

    1 year ago

    Steve: Yes. just repeat the last line but replace touch with open.

    Ken Nellis
    1 year ago

    Great shortcut! I shall be playing with that. I often miss that Windows feature. I would call adding the name option as gilding the lily, and be happy with "untitled.txt", but the missing feature for me is that the shortcut should also auto-select the file in the Finder. Then it can be immediately opened.

    Ken Nellis
    1 year ago

    Thanx, works great! It would be great if it would work on the Desktop. (Currently complains with an error message.)

    Avner Ben Zvi
    1 year ago

    Dear Gary,

    there is any option to download the script ??

    Thanks

    1 year ago

    Avner: Yes. There is a link below the video.

    Tom Theiring
    1 year ago

    You're amazing, Gary! This is exactly what I've been needing! Many thanks!!

    Ljupcho Nakov
    1 year ago

    Hi Gary, I've switched to a Mac from Windows and rely on your tutorials to learn macOS. However, after updating to the latest OS, Sonoma, my keyboard shortcut to create a file doesn't work on my external SSD. The script works fine through the Shortcode app on the SSD path, there's an error in /Users/ljupchonakov/Library/Group Containers/group.is.workflow.shortcuts/Temporary/com.apple.WorkflowKit.BackgroundShortcutRunner/XXX-XXX:11 operation not permitted: /Path/To/Extrnal/SSD

    1 year ago

    Ljupcho: You'll need to debug it to figure out what is going on. For instance, what format is the drive? Are there some permissions involved, etc.

    Ken Nellis
    1 year ago

    The title of this video, “New Blank File Anywhere”, suggests it can do so on the Desktop, but it fails in the provided JavaScript run() function when it can’t find an open window. I asked ChatGPT to modify the function to allow file creation on the Desktop, and it came through! I tried to paste the code here, but was thwarted by the 500 character limit. Happy to provide it given the opportunity.

    Isaac
    12 months ago

    Thank you

    ADTC
    11 months ago

    1. Hi, you have not provided a download for the Shortcut with the prompt to enter the file name (the later half of your video). I would like it if you can provide that Shortcut as well.

    2. Instead of prompting for the file name, could we create the file with a default name, then in Finder select the file and activate the rename option on it? (Basically automating what happens when you manually click the file and press the return key on your keyboard)

    Ken: Post as GitHub Gist and put link.

    11 months ago

    ADTC: Use the download link above and it is fairly easy to make the change I show in the video. I suppose you could use the Reveal Files In Finder Shortcuts action to have the Shortcut go to that file. You'd need to press Return yourself to start the rename though.

    Ken Nellis
    11 months ago

    ADTC: would if I knew how to do that, but here is the replacement JavaScript function, courtesy of ChatGPT:
    function run(input, parameters)
    {
    const finder = Application('Finder');
    finder.includeStandardAdditions = true;
    const path = finder.windows.length ?
    finder.windows[0].target() :
    finder.desktop();
    return decodeURI(path.url().slice(7));
    }

    Tapani Otala
    11 months ago

    I also encountered that problem "Error: Can't get object." I figured out that it will happen if the topmost Finder window is currently showing a pseudo-folder such as "Recents", "Airdrop", etc., instead of a true filesystem folder.

    I wrapped the JS code in try/catch and then branch on having gotten a folder path, or not. I also swapped the JS code and the input prompt to avoid the unnecessary question for a name that would just fail to create anyway.

    rush
    10 months ago

    Hi Gary, I am getting below error for sonoma

    Error: ReferenceError: Can't find variable: windows

    10 months ago

    rush: Are you using the downloaded Shortcut? What do you have selected when you use the Quick Action? Have you tried selecting something else?

    Benjamin
    7 months ago

    Thanks Gary!! It helps a ton...
    But I have the same problem as Ljupcho: !touch :/Volumes/EXT_HD/new.txt: Operation not permitted
    It's a APFS format external SSD, permission are granted to write & read to everyone and FULL DISK ACCESS was granted to Shortcuts & Finder (maybe it doesn't matter - but I figured I tried)... Any ideas?

    7 months ago

    Benjamin: Keep experimenting. Add Full Disk Access for Terminal too.

    Down10
    5 months ago

    This is a handy tip! I added this line to the end of the bash script:

    open -R "$folder$name$extension"

    It reveals to reveal the newly created empty file in the Finder window and selects it. From there, you can rename it (again) or open it or do whatever you want with it, without having to select it yourself.

    PaperQueen
    3 months ago

    Thanks for the great Shortcut; the workflow created ages ago in Automator decided to stop working. :::sigh:::

    Question: Is it possible to create a rich text file instead of plain text? I tried swapping .rft in place of .txt, which created a file but when I tried opening it, an error popped up saying the document could not be opened.

    3 months ago

    PaperQueen: You'd have to take a different approach. An empty plain text file is just a file with absolutely nothing at all. But any other file type, like rtf, would need to have some basics in there. So creating a plain rtf file and then duplicating it to that location would be the way. You'll probably need some shell scripting skills for that.

    PaperQueen
    3 months ago

    Ahhhh, got it. Thanks for the quick response!

    Magix
    3 months ago

    Hi Gary, I use this shortcut very often. It is excellent. I have now encountered the problem that it does not work on a network drive mounted in macos. I get the message “touch: /Volumes/XXX/XXX1/XXX2/new.txt: Operation not permitted"
    Is there any easy way to fix this? Best

    3 months ago

    Magix: Not sure what the issue could be there, sorry, Maybe something to do with the way that drive is formatted?

    Magix
    3 months ago

    It’s volume on synology nas, btfrs.

    3 months ago

    Magix: Probably has to do with being a network NAS drive then. I don't have one to test. But if you mean "ntfs" then that would be a no-go as well. Macs can't even write to an NTFS drive natively. So you have a lot of red flags there.

Comments Closed.