Many Mac users wish there was a way to create a new text file in the Finder just like you can in Windows. With the help of a simple Automator service you can add this to your Mac, and even customize how it works.
You can also watch this video at YouTube.
Watch more videos about related subjects: Automator (50 videos), JavaScript (14 videos).
You can also watch this video at YouTube.
Watch more videos about related subjects: Automator (50 videos), JavaScript (14 videos).
Video Transcript
Hi, this is Gary with MacMost.com. Today let me show you how you can create new text files anywhere you want in the Finder using a keyboard shortcut.
MacMost is supported by more than 600 viewers just like you. Go to MacMost.com/patreon. There you could read more about the Patreon Campaign. Join us and get exclusive content and course discounts.
So something that Windows users miss when they switch over to the Mac is the ability to create to just a new blank text file anywhere they want. You could do the same thing on the Mac if you create a little script in Automator to do it. We're going to create a QuickAction. The reason we use QuickAction is that it can be easy to assign a keyboard shortcut to it as we'll see later.
So the QuickAction is going to run only in the Finder. That's the only place we need this. It's going to receive nothing as input. So the idea is that we want to figure out where the current location of the Finder is. So we have a Finder window open and we want to figure out where it is and then create the new file there. To figure out where the current location is we're going to use a bit to JavaScript.
Search for JavaScript here. Make sure you have Library selected. Use Run JavaScript in here and this is where we're going to put our code. So first we're going to put a couple of standard lines here. We're going to declare the variable finder as the Application Finder and we're going to use Include Standard Additions which will allow us to control the Finder. Now we're going to get the current path. We can do this with this piece of code here. This will get the frontmost window of the Finder. It's target url. So universal resource locator. So in other words the location of what's there.
Now we're going to test this out to see exactly what is returned when we get this. This piece of code here will tell the Finder to use display dialogue to throw up a little dialogue with some information in it and it's just going to put the current path there. Now to run this we're going to have to Save this first. So go to File, Save, and since it's a QuickAction it's going to save it in a special location for QuickActions. So let' give it the name we're going to eventually use. New Text File. We'll hit Save.
Now I'll go to the Finder with this new Finder window here. Then I'll go to Finder, Services and I'll look for New Text File and we'll run it. Here's the information we were looking for showing us the current location of the frontmost Finder window. This is, indeed, that location. The parentheses 20 stand for the ascii character 32 which is a space. So LocalspaceDocuments myspacestuff. Now at the beginning here we have file:// much like http:// you might see when browsing the web. So that's part of a universal resource locator to tell you what this is. It's a file location.
We don't need that.
We want it to actually just be the first slash and the rest of the path there. Let's get rid of the first seven characters here. We'll do that by adding a dot slice 7 to get rid of the first seven characters. Then we'll save this, switch back to the Finder, and now when we run it we'll get the exact file path that we want. So far, so good.
So now let's convert the text so we don't have those weird symbols, the %20 instead of spaces in there. Let's go and put decodeURI around it. That's a JavaScript function. So by running the return path through decodeURI we should see a normal looking path. So now let's run that inside of the Finder and we see we get a normal looking path with spaces there.
So let's save that into a variable. Look for Set Variable. Set value of variable is the next action we want to run and we will take what's returned from this. So instead of displaying as a dialogue we're just going to return the current path and we're going to put that into a new variable and we'll call that variable currentPath. Now at the bottom you should see it appear as a variable. If you don't you can click this little button here and it shows you all the variables.
So now that we have a variable within it we can create a new text file using the New Text File action. It's handy that that already exists in Automator so we don't have to program that in. File format can be plain text or rich text. We can create a default file name. So we'll call this New File. Then we can determine whether or not we want to replace if there's already a new file.txt there and whether it replaces it. It's safe to turn that off because you don't want it to override something if you've created a file there and forgot to change the name.
Now the where is set to Desktop and we can only choose a location. But with this variable down here we can drag and drop that and replace the Desktop with the contents of that variable. So we did all that JavaScript here, return to the currentPath, that went into the variable currentPath so it appeared here and then we dragged and dropped it there. Now it should create that file. Let's Save and try this out in the Finder. We run it and now it's going to go ahead and there it is. New File.txt.
Let's delete that for now and let's alter it a bit and say not only do we want to create the new file but we want to open it up. So let's do Open and Open Finder Items and we can put that here at the bottom and say Open the Finder Items that come out of this new text file action in the default application. So that's great. Now if you don't want to have that you simply just don't add this there to the end. Let's try it. So I want to go Finder, Services, and then New Text File. It creates it and it opens it up.
Now there's something in the file. It's the path name of the file itself. What happened here? Well, we set the value of the variable to the current path and then we passed that into the new text file. So it took that text and not only used it for the where location here as the variable but also used it as the text in there. Now you'd think you'd be able to Control click right here and say Ignore Input. But if you do so New Text File doesn't work. It wants some input. Something to use for the text file.
So let's create another piece of JavaScript. Run JavaScript and set that right here. Here we can go and Disable the input. Ignore Input. So the value of the variable is set and then there's a full break. No input going through to run JavaScript. Now all this JavaScript is going to do is return an empty string. So blank text there. Then it's going to send that into the next thing which is New Text File. So the New Text File should be created with blank text. Save that. Go over to the Finder. Let's get rid of that file there. Let's go to Finder, Services, New Text File. Now it's going to create it. Open it up and you could see it's blank. As a plus here you can go in and put whatever text here that you want and then this will be used as the default text. So that could be a bonus.
Now that we have all this worked out all we need to do is assign a keyboard shortcut to that. So go to System Preferences, then Keyboard, Shortcuts. Then go to Services and look under General here and you should find New Text File. You can add a shortcut for that. So, for instance, let's do Control, Option, Command N and now we have that as a shortcut. Now if we go to the Finder again get rid of the file and we can look under Finder, Services and we can see that it has a shortcut. So let's just do the shortcut. We can see it creates it and opens it up for us.
Here is the code from the first Run JavaScript action:
function run(input, parameters) { var finder = Application('Finder'); finder.includeStandardAdditions = true; var currentPath = decodeURI(finder.windows[0].target().url().slice(7)); return currentPath; }
Is it possible to populate the new text file with the contents of the clipboard?
Greg: Sure. Instead of the JavaScript code that returns "", use the Automator Get Contents of Clipboard action.
Thanks for this. I've been looking for a way to dip my toe into Automator and this perfectly meets my need to create a new text entry every day. With this start, I'm going to explore on my own, finding a way to add the current date to my file name. The teaching method and examples really help me. Being a Patron to your channel is the best money I spend every month.
Hi Gary, very timely —use Text Edit a lotduring every day mainly to save snippets. This would be extremely useful.
Followed your instructions meticulously, however when I ran it got this error message: 'The action “Run JavaScript” encountered an error: “Error: Error: Can't get object.”
Any clues as to what I'm doing. wrong?
Bill: Hard to say where you may be going wrong. Make sure all of the parts of the Automator workflow are like mine, including ins and outs of the various parts.
I'm also getting this error - ‘The action “Run JavaScript” encountered an error: “Error: Error: Can’t get object.”
I went through the process twice, not sure if I'm doing something wrong, any ideas?
Andre: See my note to Bill above.
Great job as always Gary. I really like these automation posts. Just wanted to provide this supportive note of your efforts.
This is great Gary. It worked for me. I was wondering for a long time how to do this on MacOS. On Wndows there is an entry already on the context menu, you right click, select "New" and then select "Text Document". It would also be nice to have this on the context menu in a "Finder" window.
Thank Gary, I really look forward to your Automator tutorials.
On this occasion I believe you've been trumped by an app called 'New Text File', which costs pennies and genuinely fills the gap for us ex-Windows users. Unobtrusive and just the right number of options.
Definitely one for your Top of the Apps...
Hi Gary!
When I try to run it, i get this message
"Error on line 9: Syntax error: Return statements are only valid inside functions"
I'm not too great with Automator, can you help?
Robert
Robert: Hard too say without seeing it. Carefully compare what you have with what you see in the video. Pay special attention to every detail to spot any differences.
In response to errors, First, the first time you run the it after boot, you have to be in an open Finder location AND use the Finder> Services >New Text file method. You also have tube at a place in Finder where you can actually create a new text document. For example, if you're just on the desktop or another application's window, you'll get an error message. After that OK to use f-key assigned. Error also if you already have file with name in same place. Will not add new text file(1). Thanks.
Is there anyway to convert this (or other) workflows into an app? I have found the mini-apps for addressing and sending and email (from a tutorial you posted many moons ago) very useful, easy, reliable and portable. To create and open a new text file would be great for this. I tried a copy and paste into an "app" in automator but, of course, got nowhere. Thanks for considering this for a future tutorial.
Bart: You can make it an app, but you have to deal with the change of input. Where will this file be created? An app isn't being run "in" a location like a Quick Action. You could set it up so it asks for a location, or you drag and drop a folder onto the app to have it get the location, but those extra steps make it less useful. So I think this particular workflow works better as a Quick Action, not an app.