I can start a .rbw file using ruby filename.rbw
.
But can I open it using double click?
You can, but it takes a little setup. The reason you can't do this directly is that OS X will only let you associate applications with a file type—you can't associate a file type with any old executable. There's some good information on OS X's behavior in this answer: How to run a shell script in OS X by double-clicking?
Here are the steps you need to take:
This is easier than it sounds. First, run Automator and in the "Choose a type for your document" dialog choose "Application." Then, from the Actions menu on the left, double-click on "Run AppleScript" (use the search box to find it quickly). Now paste the following script into the box:
on run {input}
set filepath to quoted form of POSIX path of input
do shell script "ruby " & filepath
end run
This is pretty basic AppleScript. What it does is take the input, which will be the filename of the file you double-click on, and runs ruby
with that filename as an argument.
Now save the application with a name like "Run Ruby Script." You can save it wherever you want, but I think ~/Applications
is a good choice.
.rbw
files with the application you just createdFind the .rbw
file in Finder. Right-click on it and choose Open With and then Other.... The "Choose Application" dialog will appear. At the bottom, choose "All Applications" in the Enable drop-down. Navigate to the "Run Ruby Script" application you just created and select it. Finally, check the Always Open With checkbox and click on Open.
The above duplicates the behavior of .rbw
files on Windows, which is to say that the script is executed silently—no terminal window is opened and no output is seen.
If you want to capture the output you could change the AppleScript run the command in Terminal or iTerm instead—and you might have to do that if your script is interactive, but it's beyond the scope of the question—but I think a simpler solution is to just display the output in the default text editor, which can be accomplished thusly:
on run {input}
set filepath to quoted form of POSIX path of input
do shell script "ruby " & filepath & " | open -f "
end run
This just pipes the output from ruby
to open -f
, which writes the output to a temporary file and opens it in the default text editor, e.g. TextEdit.