Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to get „Create New Text File” from context menu

Well, I searched entire google, and I still can’t find solution. Everybody knows how convenient is ability to create new text file straight from context menu. I found some solution by making droplet and yes, it does work, but I also know, there is the way to achieve this functionality just by clicking right mouse button and choose it from context menu just like windows. Now, does any one has any solution ? How to get „Create New Text File” from context menu ?

iMac, macOS High Sierra (10.13.3)

Posted on Feb 17, 2018 2:43 PM

Reply
Question marked as Best reply

Posted on Feb 20, 2018 1:29 PM

After trying a few things, you can't get a contextual menu in Finder if you have not selected any context.

You could make one that would work if you right-click on a file or folder, but not on empty space.


You can also make it an Application and drag it into the Toolbar. Then, when you have a Finder window open, just click on the button in the toolbar.


For the service in the contextual menu:

Create a new Automator Service

Set it to receive files and folders in Finder

drag in a Run AppleScript action form the Utilities Library.

Set the code to this:

on run {input, parameters}
  set fileName to text returned of (display dialog "Please choose a name for your file:" default answer "Untitiled" buttons {"Cancel", "Create"} default button "Create")
  if fileName does not contain "." then
    set fileName to fileName & ".txt"
  end if

  tell application "Finder"
    set windowList to index of Finder windows as list
    set activeWindow to item 1 of windowList
    set windowTarget to (target of Finder window activeWindow) as alias
    set folderPath to POSIX path of windowTarget
  end tell

  return folderPath & "/" & fileName
end run
  • Line 2 asks you for a file name.
  • Line 3 to 5 appends .txt to a the file name if you don't put one in yourself.
  • Line 8 gets the list of open windows in Finder.
  • Line 9 gets the index number of the topmost window.
  • Line 10 gets the Target of the Finder window (i.e. the directory which the Finder is showing in that window).
  • Line 11 gets the folder path as a string of text in POSIX format (unix path).
  • Line 14 combines the file name with the path and returns it to the next Action

Drag in a Run Shell Script Action from the Utilities Library.

Set "pass input" to "as arguments"

Set the code to:

for f in "$@"
do
  touch "$f"
done

The arguments passed to the shell script action are in $@.

While this should just be passing a single argument, I wrote it to handle multiple just in case.

Line 3 creates a file using the path and file name that were passed to the Action. Touch is a unix command that will create a file if it doesn't exist, or can be used to change file system dates for the file.


Save this with a useful name such as New empty file…


For the Application in the Toolbar,

Create a new Automator Application and use the same two actions as above.

Save it as an Application. You can create an Applications folder in your Home folder and save it there, or if you want it available to all users, save it somewhere everyone can access the app.


Open the folder where you saved the application. Hold down the command key and drag the application into the Toolbar. If you decide to move it to another spot on the toolbar, just cmd-drag it wherever you want. You can change the Icon by copying a picture and pasting it into the icon well of Get Info.

8 replies
Question marked as Best reply

Feb 20, 2018 1:29 PM in response to IrasDeeJay

After trying a few things, you can't get a contextual menu in Finder if you have not selected any context.

You could make one that would work if you right-click on a file or folder, but not on empty space.


You can also make it an Application and drag it into the Toolbar. Then, when you have a Finder window open, just click on the button in the toolbar.


For the service in the contextual menu:

Create a new Automator Service

Set it to receive files and folders in Finder

drag in a Run AppleScript action form the Utilities Library.

Set the code to this:

on run {input, parameters}
  set fileName to text returned of (display dialog "Please choose a name for your file:" default answer "Untitiled" buttons {"Cancel", "Create"} default button "Create")
  if fileName does not contain "." then
    set fileName to fileName & ".txt"
  end if

  tell application "Finder"
    set windowList to index of Finder windows as list
    set activeWindow to item 1 of windowList
    set windowTarget to (target of Finder window activeWindow) as alias
    set folderPath to POSIX path of windowTarget
  end tell

  return folderPath & "/" & fileName
end run
  • Line 2 asks you for a file name.
  • Line 3 to 5 appends .txt to a the file name if you don't put one in yourself.
  • Line 8 gets the list of open windows in Finder.
  • Line 9 gets the index number of the topmost window.
  • Line 10 gets the Target of the Finder window (i.e. the directory which the Finder is showing in that window).
  • Line 11 gets the folder path as a string of text in POSIX format (unix path).
  • Line 14 combines the file name with the path and returns it to the next Action

Drag in a Run Shell Script Action from the Utilities Library.

Set "pass input" to "as arguments"

Set the code to:

for f in "$@"
do
  touch "$f"
done

The arguments passed to the shell script action are in $@.

While this should just be passing a single argument, I wrote it to handle multiple just in case.

Line 3 creates a file using the path and file name that were passed to the Action. Touch is a unix command that will create a file if it doesn't exist, or can be used to change file system dates for the file.


Save this with a useful name such as New empty file…


For the Application in the Toolbar,

Create a new Automator Application and use the same two actions as above.

Save it as an Application. You can create an Applications folder in your Home folder and save it there, or if you want it available to all users, save it somewhere everyone can access the app.


Open the folder where you saved the application. Hold down the command key and drag the application into the Toolbar. If you decide to move it to another spot on the toolbar, just cmd-drag it wherever you want. You can change the Icon by copying a picture and pasting it into the icon well of Get Info.

Feb 17, 2018 2:56 PM in response to macjack

Hello mate, thanks for prompt reply. Well, I found this solution some time ago, but this creates droplet in finder toolbar. How about if I want to quickly create text file just right on my desktop ? I know I can simply just copy this droplet to my desktop and by clicking on it, ill get new empty text file, but is there no way to create service in automator so I can have it in context menu like on Windows ?

Feb 18, 2018 10:59 AM in response to Barney-15E

I was trying to follow instructions from that link, and I keep getting error in Automator. It also looks a bit complicated for me, please don’t get me wrong, I don’t mind getting myself encouraged but im still newbie. I will really appreciate if you could explain this a bit more like step by step guide 🙂 I hope I’m not overusing your kindness.

Feb 18, 2018 12:38 PM in response to IrasDeeJay

There is no default contextual menu feature in OS X, or macOS that enables creation of a new blank text document on the Desktop. If TextEdit, or your favorite editor is in your Dock, then it is a single click to launch it. Two, or three clicks away if using Launchpad, depending on pre-Sierra, or afterward.


If you want to launch TextEdit with the contents of a text selection in another application, this is a Services feature enabled in System Preferences : Keyboard panel : Shortcuts : Services panel.

Feb 20, 2018 1:38 PM in response to Barney-15E

Oh man, you are absolutely amazing. Maybe its not a perfect solution, as you can’t create a file by clicking on empty space, but this is a milestone comparing to this what I had before which is an automator app in toolbar. Thank you very much for solution and sorry for late reply. Just didn’t have a time to play with this due to long hours in work. I also really appreciate that you explained this step by step, I definitely need to learn something about Automator.

Looks like this automator is god **** powerful tool 😉

How to get „Create New Text File” from context menu

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.