> ninivut's octangles_ 

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

 xml 

[quicksilver filehere] last modified: 03/25/2007 02:05 pm

Thanks to this hint, I was able to integrate creating a new file in the current Finder directory rather nicely into Quicksilver.

It involves an applescript. Copy/paste the code into Script Editor and save as "File Here.scpt" in ~/Library/Application Support/Quicksilver/Actions/. You may have to create this folder. I had to quit and restart Quicksilver to get it to recognize the new action.

You might need to have "Enable access for assistive devices" enabled under Universal Access in System Preferences, or that might just be something necessary for the script this was based on.

To use:

  1. activate Quicksilver
  2. type "." (i.e. hit the period key)
  3. type the file name you want to create (i.e. newfile.txt)
  4. press tab
  5. start typing "File Here"
  6. once Quicksilver finds "File Here.scpt", press return
  7. A new file will be created in the front Finder window, or on the Desktop if no window is open

Caveats apply. It works great for me so far, but though I've tried to use it as many ways as I can to test it, I can't say there won't be any bugs. Please let me know (djbidi@gmail.com) if you have any problems or suggested improvements.

If you use an editor that has a command line tool, such as TextMate, bbEdit, or Smultron, you can replace touch in the applescript with the full path to the command - for example I use TexMate and would use /usr/local/bin/mate

Then, instead of creating a new file, it will open the editor with the file all set up to save in the right spot. And if you quit the editor without saving, the file is never created.




[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" ]
 then
 if ! [ -e ~/Desktop/untitled.txt ]
  then
  touch ~/Desktop/untitled.txt
 fi
fi
# if path is a folder
if [ -d "$1" ]
 then 
 if ! [ -e "$1untitled.txt" ]
  then
  touch "$1"untitled.txt
 fi
# an item is selected in the folder
else
 FPATH=$(echo "$1" | sed 's/[^/]*$//')untitled.txt
 if ! [ -e "$FPATH" ]
  then
  touch "$FPATH"
 fi
fi

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]

tags:

automator
filehere
quicksilver