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.
You can also watch this video at YouTube.
Watch more videos about related subjects: Shortcuts (70 videos).
You can also watch this video at YouTube.
Watch more videos about related subjects: Shortcuts (70 videos).
Video Transcript
Hi, this is Gary with MacMost.com. Let me show you a shortcut that will let you create a new Text File anywhere you are in the Finder.
MacMost is brought to you thanks to a great group of more than 1000 supporters. Go to MacMost.com/patreon. There you can read more about it. Join us and get exclusive content and course discounts.
Now one thing that a lot of people ask me about is there a way to create a new blank text file when they are in the Finder. This is something you can do in Windows. So when Windows' users switched to Mac they're often bothered by the fact that there just doesn't seem to be a way to do that on the Mac. You would have to open up an app, like TextEdit, create a new text file there and then Save it to this location. So to add something like this to the Mac we're going to create a Shortcut that does this specifically.
So, in the Shortcuts App we're going to Add a new one by clicking the Plus button. Then in the new shortcut let's name it Create New Text File here. Then I'm going to add Actions that will create a new text file wherever you are in the Finder. Unfortunately, there's really nothing in Shortcuts that will allow you to do this. What I mean by this is the Actions don't really get you there. You can't really see where you are currently in the Finder. You can only see, maybe, if you have like Folder or File selected which may not be what you want here. Also, you can't really Save a brand new file to a location given just a path. You have to actually have a folder object. So using all of the different Actions in Shortcuts really doesn't do this at all.
However, you do have access to Scripting in Shortcuts. You can do AppleScripts. You can do JavaScripts which will accomplish the same thing. You can also use Terminal Shell Scripts which are also going to some in handy here. So, we're going to start off with using a little bit of JAVAScript and I'm going to use the Run JAVAScript from Mac Automation, right here. It fills in a little blank function and we're going to populate that with some code that will help us figure out where we are in the Finder. So I'm going to replace this with some code that I'm going to paste in here. This code here is going to assign a variable to the Finder so we can then call the Finder with things like, includeStandardAdditions which will allow advanced functionality.
We're then going to get a variable called Current Path from the current Finder window. The target for that, the URL for it. Then we're going to start after character 7 because this path is going to start with File://. We're going to get rid of that by starting after that. So we're going to get the actual current file path. So if I were to run this right now you could see the result here is the folder we were looking at before because that is still the front-most Finder window. You just can't see it behind here. So, so far we've got the current path. Now we need to create a file there.
To do that we're going to use a ShellScript. So I'm going to type in here Shell and I'm going to choose Run ShellScript as the next Action to take place. Now it just fills it in with a little Hello World line there. We're going to replace that. So we're going to start off here with a few variables. One is going to be the Folder, the place where we currently want to create this file. That's the Output of this. So what I'm going to do is select in here and Control Click and Insert Variable and take the JAVAScript result from this and place it in the variable folder. The ShellScript will actually run with the path literally in-between those quotes there. Just like we typed it. But it will be different depending upon where we run the script.
Now it's going to have a file name. In this case we'll just use New and it extension, in this case we use dot txt .txt. Then we're going to use the Touch command, which is a Shell command that basically takes a file and just touches it. In other words makes it kind of active. If the file doesn't exist then it will create it. So Touch is often used to create a blank text file. We're going to, in quotes, combine the folder path and then the name and then the extension. So it should be that complete folder path from here slash new dot txt and everything else is set to just the default here. If these were set to anything else just set it to Input, zshell, and standard in right there.
Now this should work to create a new text file wherever we currently are in the Finder. So let's try it. Here's that current folder and that is where we are in the Finder. If I go here into the Shortcuts App and run this you could see it creates that file new.txt. Perfect. I can now set this up under Details here to Pin in Menu Bar. Now without having shortcuts even running I an be in a folder like this, we will delete the new.txt. I can go to the Shortcuts Menu and say create new text file here. Run it and it runs. I can even assign a keyboard shortcut to this by selecting Uses Quick Action. Let's going to add this Receive at the top here. For neatness I'm going to go in here and Select All and then Deselect All and then select Files & Folders. The reason I'm going to do that is that I want to make sure that this is Active whenever a file or folder is selected. So basically anytime you're using the Finder.
Then here it is going to be used as Quick Action. I'm going to say Yes in the Finder. It should be in the Services Menu. Then I'm going to add a keyboard shortcut. You're going to want to choose something that is not being used by anything else. I'm going to do Control Shift Command N. Now when I'm in a place like this in the Finder if I go to Finder, Services I'll notice that it's right there under shortcuts. But I don't have to go there. I can just use the keyboard shortcut and now it will create this new file right there.
But we're not done yet because currently as it stands it is only going to create new.txt. But what if there already is a new.txt? It won't do anything. What will be nice if it had that kind of functionality where if you tried to use it again it would create new2.txt and a third time would be new3.txt. So I'm going to Add to the script here. So this bit I've added is going to search for the existence of this file as it currently stands. So new.txt. If it exists it is going to set a variable i to the number 2. Then it is going to loop and check for the path and then the name, in this case, new, then a space and then that number 2 and the extension. If that already exists it is going to increase the number by 1 and try again. So it is going to look for new2, new3, new4, etc. It will finally finish this loop and exit when it finds a number that doesn't exist. Then it is going to reset the name. Instead of it being just new it is going to be new2 or new4 or whatever. Then it exits the loop. So now when it does the touch it may be doing it with new.txt. But it may also be using name as new space 2.txt. So now when I use this in this location here the first time it's going to go and create new.txt. The second time it will create new 2.txt. Then every time after that it will keep going adding a number to it.
Now what if I want to do something besides the word new. I could put anything else I want in here. Text or blank. But I could also have a prompt. So I'm going to do that. I'm going to Search for Input and I'm going to get Ask For Input. I'm going to put it here at the top. I'm going to say Ask For Text with Prompt File Name? Under Show More I'm going to put a default answer. So that if I just want the word new I could just quickly use that. I don't have to type. But then Allow Multiple Lines, I'm going to turn that Off which is important because obviously multiple lines isn't what we need here. But also this will allow me to use the Return key to activate it. So now down here, where it says blank, I'm going to Delete that. Do Control Click, Insert Variable and Use Provided Input. So now when I'm here I could go and either use the menu up here or just that keyboard shortcut. It's going to prompt me and I could type Something and then Return and it's going to create a new blank text file at that location with the name that I chose. If I were to do it again and also do, something, then it would append 2 to it and I get something 2.txt.
Now I'm going to put the scripts for both of these at the post at MacMost.com. But also at that post I'm going to put a link where you can directly access the shortcut and add it to your Shortcut's App on your Mac without having to recreate it from scratch on your own. I hope you find this useful. Thanks for watching.
Link to the Shortcut.
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
Jacques: Make sure you are following along with everything and using it exactly as I am.
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.
Roger: All I can suggest is to start over and check everything carefully. I can't debug it for you from afar.
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?
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.
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.
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.
And...thanks bunches...I did not know one could comment at your site instead of YouTube....
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
Joe: When writing Shell scripts, you need to enclose paths with spaces in quotes, or escape the spaces with a \ like My\ Folder\ Name.
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!!
Brian: Look under the video and above the comments for the "Link to the Shortcut."
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.
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?
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.
Mike: No explanation for that. Try a restart and see if it persists.
Can you add one more line to open the new.text file with TextEdit?
Steve: Yes. just repeat the last line but replace touch with open.
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.
Thanx, works great! It would be great if it would work on the Desktop. (Currently complains with an error message.)
Dear Gary,
there is any option to download the script ??
Thanks
Avner: Yes. There is a link below the video.
You're amazing, Gary! This is exactly what I've been needing! Many thanks!!
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
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.
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.
Thank you
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.
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.
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));
}
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.
Hi Gary, I am getting below error for sonoma
Error: ReferenceError: Can't find variable: windows
rush: Are you using the downloaded Shortcut? What do you have selected when you use the Quick Action? Have you tried selecting something else?
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?
Benjamin: Keep experimenting. Add Full Disk Access for Terminal too.
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.
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.
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.
Ahhhh, got it. Thanks for the quick response!
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
Magix: Not sure what the issue could be there, sorry, Maybe something to do with the way that drive is formatted?
It’s volume on synology nas, btfrs.
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.