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.