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.

Not Dead

Hello out there, oh loyal, faithful readers. All three of you.

Just wanted to pop in and say hi. It's been a solid month since I've posted anything, and that's pretty unusual. I've even stopped responding to comments, which is almost unheard of. Unfortunately it's been somewhat out of my control. Both work and life have been insanely busy. I'm not so much making excuses here as posting this so that anyone who might be reading this site knows that:
A) I am not dead (or even injured)
B) I am not planning on discontinuing the blog.

That said, I wanted to just take a moment and explain what's been going on. Our department is getting a face lift. Which is to say that we are completely remodeling our floor. Gut rehab: Power. A/C. Network cabling. Seriously. Everything is being completely redone. It's freaking fantastic. It's also a hell of a lot of work as I'm the one overseeing the entire transition, from a technological standpoint anyway, which, since this is a digital art program, represents a fairly large piece of the pie. So I'm exceptionally busy helping plan the new floor — from actual per-room computer placement, to the power needs and layout of the server room. Much of this is new to me, so I'm being educated quite rapidly and on the fly. And since this is a gut rehab, we'll have to clear off the floor at the end of the semester, so I'm planning that transition as well — from moving staff computers to another floor and setting them up with an ad-hoc LAN, to moving our entire DMZ to said floor along with the requisite internet pipes. It's not easy, and it's very much a concerted effort. Fortunately, everyone here has really pulled together, and we're actually making it happen somehow. And while I'm elated to finally be getting all this done (it's been a long time coming, believe me, and we desperately need it, if for the cabling and power problems alone — don't even get me started on the A/C!) I'm way too overwhelmed to write about any of it intelligently right now. Or much of anything else for that matter.

We have been forging ahead with our various long term projects, however. Most notably, I've started building and testing an authentication server for the external network. So far it's going exceedingly well, and it shouldn't be long before our external network has a single authentication source (hopefully this summer sometime). I've been taking notes, so expect a post sometime when things get back to normal. We've also recently bought and set up a firewall appliance which we're loving. It's now being used to properly host our internal and DMZ networks, and is really making our network management a thousand times easier and more sensible. We'll be using it for limited VPN as well, which will be sweet. Today a friend will be running a demo of Leopard in his class as well. These — along with all the goodies associated with the renovation — are all posts in the pipe. Just as soon as I get some time.

You know — if I may wax reflective for just a second — it's not even so much that I have no time to write. Clearly, I'm able to write this post. The real problem is that writing about technology takes a certain amount of brain power. No, not even brain power. Brain space is maybe more like it. With everything I'm dealing with, I've got plenty to write about, and even some time to write about it. What I'm missing is the time to actually process what I'm experiencing. And before I can write about it, I have to process it. It's not so much that I need time to write as that I need to time to think. And that I just don't have right now.

So that's what's been going on here. This can't all last forever, of course. And I'm sure I'll be posting regularly again soon. Until then, do check back from time to time. I'll try to post small bits of stuff as I can.

Okay then. Back to the trenches. This has been fun.