Cause an arbitrary Unix command to be invoked when opening a file

Hippo Man

Hippo Man
I'd like to cause an arbitrary Unix command to be invoked when opening a given file in the Finder.

In other words, I'd like /path/to/arbitrary/command FILE to be invoked, where FILE is the path name of the file that I'm opening in the finder.

For example, if the file FooBar.quack lives in /Users/me/Documents, and if I want it to be fed to /usr/local/bin/bonzo, I'd like to somehow set up an association so that when I select that file in the Finder, the following command gets invoked:
/usr/local/bin/bonzo /Users/me/Documents/FooBar.quack
Is there a way to do this in MacOS 10.5?

Thanks in advance.
.​
 
Not directly (AFAIK), but there are possible workarounds.

I've done something like that with AppleScript. It requires making a script applet, and then setting the file to open with that applet. The applet in turn launches the desired unix command. The AppleScript code is simple:

Code:
on open {x}
	tell application "Terminal"
		do script "/usr/local/bin/bonzo " & quoted form of (POSIX path of x)
	end tell
end open

Save that as an application (using Script Editor in /Applications/AppleScript), then Get Info on your file in the Finder, select "Open With" > "Other..." and then point it to the applet.

The end result will be that when you double-click the file, you'll see the script appear in the Dock for a second, then Terminal will open, the command will execute in Terminal, and the script will disappear from the Dock.
 
Back
Top