robertmuench.de

To the point & Out of the box


Starting scripts via Finder

These days I'm using scripting languages a lot. Especially Rebol. Further I use some pre-processing scripts to setup the environment for my scripts so that dynamic libraries etc. can found on different platforms. Working on Windows, Linux and OSX with the same scripts requires this.

On Windows it's quite "simple" because you can assign special commands to specific file-types. And these commands are shown if you right-click on a file. In my case I assigned a "pre-process" action with Rebol scripts. The command line looks something like this: . The last argument is provided via the famous %1 parameter reference syntax of batch files. The whole setup works very well.

On Linux I use a simple shebang line at the moment. Works to. I haven't thought about a generic way, because I mostly use Linux in non-graphic mode. But I think setting up a launcher script would help.

Next was OSX and I expected this to take just a couple of minutes to setup. WRONG! It really took me quite some time to fiddle together all parts. On OSX there is no way like on Windows where I can specify a command line to use as default action when a certain file-type is clicked in the finder.

Ok, my workaround is simple: Create a launcher script that can be associated with files of a specific filetype.

But Problem 1: Finder doesn't know how to start shell scripts. I don't know why. There is a simple workaround to this, if you find it. Just rename your script from .sh to .command and Finder can start the script using the shebang line of the shell script.

Ok, I changed my script and I thought it now works. WRONG! It's not possible to associate a filetype with this kind shell script. Further it seems that the clicked filename isn't provided to the shell script via the common $1 syntax.

Back to start. Than I remembered AppleScript. From what I read it's quite powerful. So I took a look at it. After some hours getting my hands on it and finding what I need, I finally came up with the following simple launcher script.

on open input_file
set f to (POSIX path of input_file)
set p to do shell script "dirname " & f

tell application "Terminal"
do script "cd " & p & "; /usr/local/bin/interpreter /Volumes/MyDriver/MyDir1/MyDir2/my-preprocessing-script " & f

tell window 1
set size to {850, 450}
end tell

activate
end tell
end open

Some comments on this:

  1. You have to save such a script as an "Application Bundle". This will give it an .app extension so finder recognizes it as application.
  2. This script, when saved as an app package, can now be associated with a specific filetype. After this you can start scripts from Finder.
  3. First I get the path to the script (variable p) because we need to change directories later, so that the script is executed in its home directory.
  4. The actual shell stuff is done in one call. First change directory, second start script.
  5. I change the window size to be bigger than default and finally active the terminal window.
0 Comments