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: 36 Responses to “Use a Shortcut To Create a New Text File In a Folder On a Mac”

    Jacques Du Biel
    10 months 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

    10 months ago

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

    Roger
    10 months 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.

    10 months 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
    10 months 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
    10 months 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.

    10 months 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
    10 months 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
    10 months ago

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

    Joe
    10 months 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

    10 months 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
    10 months 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!!

    10 months ago

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

    Mike
    10 months 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.

    10 months 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
    10 months 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.

    10 months ago

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

    Steve
    10 months ago

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

    10 months ago

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

    Ken Nellis
    10 months 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
    10 months ago

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

    Avner Ben Zvi
    10 months ago

    Dear Gary,

    there is any option to download the script ??

    Thanks

    10 months ago

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

    Tom Theiring
    10 months ago

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

    Ljupcho Nakov
    8 months 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

    8 months 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
    8 months 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
    7 months ago

    Thank you

    ADTC
    6 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.

    6 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
    6 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
    5 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
    5 months ago

    Hi Gary, I am getting below error for sonoma

    Error: ReferenceError: Can't find variable: windows

    5 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
    2 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?

    2 months ago

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

Leave a New Comment Related to "Use a Shortcut To Create a New Text File In a Folder On a Mac"

:
:
:
0/500 (500 character limit -- please state your comment succinctly and do not try to get around this limit by posting two comments)