The Adventures of Systems Boy!

Confessions of a Mac SysAdmin...

Scripts Part 7: Contextual Menus with Automator

Recently, for some odd reason, there has been a spate of solutions to the problem of creating new files in the Finder via a contextual menu. One involves a contextual menu plugin called NuFile. Another involves installing Big Cats Scripts and linking it to an Applescript. But honestly — and I'm surprised someone else didn't think of this first — when faced with simple contextual menu tasks, these days my first thought is to look to Automator.

And by golly, that's just what I did. Here are a few Automator workflows that do, more or less what the afore-linked methods do. To me, the advantage of the Automator approach is that you don't need to install anything. It's all baked in. Which means you don't ever need to update anything either. Nice. Simple. And, yeah, kind of the whole point of Automator.

So here you go. Maybe someone will find this useful, if for nothing other than as an exercise in creating contextual menu functionality with Automator. Or skinning a cat multiple ways. Or something. To use this, download the .zip file, unzip it and place it in:
~/Library/Workflows/Applications/Finder

NewTextFile Workflow

It should become active immediately.

Also, here are a couple variants. One will create a text file, and then open it in TextWrangler (if you have TextWrangler, and if you don't, go get it now); the other creates a Word document, and opens it in Word. I'm far to lazy to completely duplicate the functionality of NuFile. But if you examine these workflows, you can at least see now how that would be possible (in fact, fairly easy) to accomplish.

NewTextFile Workflow Variants

I actually think it would be great if Apple made it drop dead simple to create true contextual menus for the Finder. Fortunately, Automator gets us pretty close.

Oh, yeah, and since this is technically script writing, and since I haven't posted to that series in some time, we're gonna go ahead and call this a Script Sharing post. Deal with it.

Right. Good night.

UPDATE: Revised March 31, 2007, 3:00 PM
Stephan Cleaves has added yet another implementation of this idea. He's using a combination of Automator and AppleScript. I certainly think his implementation is better than mine in a few ways. Certainly more full-featured. It will prompt for a file name, for instance, and takes pains not to overwrite a preexisting file with the same name. Nice. But we're taking very different approaches to the same idea (his version places a file in the front-most Finder window, my version places it in the right-clicked folder), and he was confused by my approach. After speaking to him via comments on his blog, I realized that some clarification as to how my workflow is actually constructed might be in order.

Basically, my workflow takes the folder selected in the Finder as input and assigns that input to the variable "$@". That variable and the for loop in my workflow are automatically generated by Automator when you select “as arguments” from the “Pass input:” field in the “Do Shell Script” action. It’s how you get the context (the selected folder) passed to the script. Apparently Automator takes “$@” as the variable for “the folder you just selected” whenever there’s no input from a previous action. This was something I learned while fiddling around with all of this, and it's really my favorite part. The coolest thing for me here, really, was figuring out how to pass the context — i.e. the right-clicked folder — to an Automator "Do Shell Script" action. This opens up worlds of potential.

Finally, as I said, the for loop in the action is auto-generated by Automator. The workflow will work almost as well with the simple script:
touch “$@/NewText.txt”

Using the for loop, however, allows you to create a new text file in multiple folders by selecting said folders and running the workflow.

It's really kind of amazing how many ways there are to do this. Wow. Fun stuff.

Labels: , , , ,

« Home | Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »

3:56 AM

All right, you tell 'em systemsboy! ha ha. It's funny, I stared at Automator for almost two years before I realized how useful it could be. It struck me one day as I was trying to solve this workflow problem. Trying to convert some PDFs to image files. I turned to Unix scripting with sips, ImageMagick, etc. It just didn't work properly. Then AppleScript -even worse. Finally in desparation I turned to Automator. In 2 minutes it was done. Wow. Shee-it. For some things it really does work. Especially for contextual menu (right-click) action.    



12:34 PM

thanks!    



12:53 PM

matx,

Yup, same for me. And once I realized that it can be used in contextual menus, I started looking at it as a way to replace the handful of contextual menus I have that are no longer being developed and will thus die a horrible death once I make the inevitable move to either Intel hardware or Leopard. Or both.

Seeing all those posts got me wondering if maybe people have forgotten this side of Automator.

Okay... Now I must go back to work...

-systemsboy    



9:57 PM

Thank you for this simple and effective Automator workflow. I adapted it for use with TextMate: http://www.zolton.org/?p=395

Mark    



10:28 PM

Awesome! Thanks for linking to your version. Glad you found this article useful.

-systemsboy    



8:52 PM

Many many many many thanks!!!!!!

I just discovered your discovery and OMG thank you!!!!!

I have thousands of jpg's to compress and am wanting to do it with mogrify. Your script allowed me to do it sooo much faster! Thank you again!    



9:31 PM

Nabil,

No problem. Glad to be of help.

-systemsboy    



» Post a Comment