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.