> pls impeach gov't tia_ 

c u t u p @ c u t u p . o r g


[automator filehere] last modified: 03/25/2007 02:00 pm

The pick of the week at macosxhints is a 8 dollar app that does that right click -> New Text Document thing that Windows has. I used to use that all the time, I miss it, and I got to thinking. They note that:

"There are workarounds available, such as using touch in the Terminal, or creating an Finder plug-in using Automator's New Text File action, but neither are really ideal."

but my interest was piqued. After messing with the Automator action, I could see why. All I could get it to do is throw up a dialog box with a name and location, and I couldn't seem to pass the current finder window value to it for the save location, which is what we want. And touch requires a terminal - even with the excellent pos package from fink it's not what we want.

But a bit more poking around, and I found something that works pretty well, using Automator and touch, together!

Open Automator and drag a Get Selected Finder Items action over to the workspace. Below it drag a Run Shell Script action.

Right under the little pipe thingy where the actions connect up, where it says "Text", click and make sure that "Use Results From Previous Action" is selected. Set the Pass input drop menu to "as Arguments", and enter the following into the text area:

# if no path we're on the Desktop
if ! [ "$1" ]
 if ! [ -e ~/Desktop/untitled.txt ]
  touch ~/Desktop/untitled.txt
# if path is a folder
if [ -d "$1" ]
 if ! [ -e "$1untitled.txt" ]
  touch "$1"untitled.txt
# an item is selected in the folder
 FPATH=$(echo "$1" | sed 's/[^/]*$//')untitled.txt
 if ! [ -e "$FPATH" ]
  touch "$FPATH"

Save the action with File -> Save As Plugin, give it a name, and save as a plugin for Finder. Now it will be available in the Automator submenu on Control-click.

When run, it makes a file called "untitled.txt" in the currently selected Finder window, but only if the file does not already exist. It will only run if a folder is selected - if a specific file is selected in the Finder, it will silently fail (UPDATE - I amended the script so this will just discard the filename and go ahead and try to create the file). The touch command will not overwrite a file that already exists, but it will change its access and mod times, so we restrict it to creating new files only.

When no Finder window is open the Get Selected Finder Items action passes nothing, so it would write the file to the root of the drive, but since we are always on the Desktop when no Finder windows were open I just wrote that in as a special case, and there we go.

If you have SubEthaEdit, TextWrangler, or any other editor with a command-line tool installed, you can replace the touch commands in the above script with the command to open the file in that editor. For example, using TextWrangler we would use:

edit "$1"untitled.txt

and then what happens is a new document called untitled.txt opens in TextWrangler. If you quit without saving the file is never actually created, but if you save it doesn't need a save dialog, it is automatically saved in the selected Finder window as untitled.txt. Pretty nifty.

I tried both ways, and so far I actually find the first method just makes more sense to me even if the second method is theoretically a bit faster.

I still have some complaints with this:

  1. automator seems kinda slow
  2. the plugin seems to have to be in that submenu, which is pretty annoying.

But it's still better than nothing. I use my "Get Selected Finder Items" -> "Open Finder Items" (with Text Wrangler) plugin all the time, and I've already used this one a few times in a couple hours.

UPDATE: If you are running Quicksilver, or another app that can create global hotkeys like that, you can save the workflow as an Application. You put the app anywhere you want, and assign a hotkey to run it. It needs to be run with a hotkey because if you didn't, the current folder would always be where you put the app, not where you want to open the document. I use Option-Space, it works great. I'm about to ask if this functionality can just be built in to Quicksilver, but in the meantime ...

[All Posts] [top]