MacMusic  |  PcMusic  |  440 Software  |  440 Forums  |  440TV  |  Zicos
going
Search

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

Friday August 25, 2023. 05:00 PM , from MacMost
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.
Want to know more about how to use Shortcuts on your Mac?Check out this MacMost course!


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.Related Subjects: Shortcuts (58 videos)
Related Video Tutorials:
View Zip File Contents With a Shortcut on a Mac ― How To Resize Photos With a Shortcut On Your iPhone or iPad ― Building a Mac Shortcut To Add a Border And Caption To a Photo ― macOS Shortcuts: Capture Text From Your Screen

Link to the Shortcut.
https://macmost.com/use-a-shortcut-to-create-a-new-text-file-in-a-folder-on-a-mac.html
News copyright owned by their original publishers | Copyright © 2004 - 2024 Zicos / 440Network
Current Date
Apr, Tue 30 - 06:36 CEST