3
votes

When programming I find it convenient to use Finder to explore directories. In the past I would often find myself wanting an easy way to copy the complete path of the selected file or files to the pasteboard (clipboard) which is (as of OSX v10.8.2) a facility that Finder lacks. So being a programmer, I added the desired functionality by adding an Automator Workflow that launches a simple Applescript to the file context menu that appears when a selected file is right-clicked in Finder.

The script works great! It adds the desired functionality and is convenient to use. However, there is a piece of behavior that I'd like to add to the script that I haven't yet been able to figure out how to do. The name of the Finder context menu item that launches the Workflow is "Copy Filename" which is fine when there is only one file selected but when multiple files are selected I'd like the name menu item to change to "Copy Filenames" or perhaps "Copy "+(The # of selected files)+" Filenames".

So my question is this: How do I, using Applescript, dynamically set the name of an item in the file context menu that appears when a selected file is right-clicked in Finder based on the number of selected files?

There is some precedent for this type of behavior. It can be seen when copying selected files in Finder. If only one file is selected the copy menu item in the context menu will read, Copy "somefile.txt" but if five files are selected the copy menu item will read, Copy 5 Items.

1
I suspect that the finder programatically creates the copy menu item at the time the call is made to show the contextual menu. Therefore the name is created at the same time and changes depending on the conditions.markhunte

1 Answers

2
votes

You can’t. Well, you could, but it’s really gross and probably not worth the effort. The name shown in the menu is stored in an Info.plist file inside the workflow, so in theory you could write a monitor script that watches the Finder selection and twiddles that file appropriately, and then calls the magic function NSUpdateDynamicServices(), for which you’ll need AppleScriptObjC. (Or you could kill and relaunch Finder, but doing that every time you change the Finder selection would be, shall we say, impractical.)

An interesting enhancement request would be a way to name Service workflows such that the name reflects the selection, so you name your workflow, say, “Copy Path from %”, and the system would substitute “foo.txt” or “5 items” or what have you automatically.

It’s also worth noting that Finder actually does deal in full paths to some extent. Try dragging files into a plain text document -- you’ll get the paths. Or try copying and pasting into Terminal, which knows about the “furl” flavor on the pasteboard.