0

I can start a .rbw file using ruby filename.rbw.

But can I open it using double click?

Lane
  • 387
  • 1
  • 12

1 Answers1

1

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:

1. Create an application that runs Ruby scripts

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.

2. Tell OS X to open .rbw files with the application you just created

Find 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.

What about the output?

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.

Community
  • 1
  • 1
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • It seems not worked for me. If I ran `ruby xxx.rbw` in a terminal, the "xxx.rbw" worked well. But if I tried to use `do shell script "ruby " & filepath` command in Automator, `ruby` cannot locate some environment variables correctly. For example, if I use Automator, ruby cannot find `tk` library. Why? – Lane Jan 22 '16 at 16:24
  • I don't know. Are you using RVM or rbenv? Automator may be running the system Ruby. – Jordan Running Jan 22 '16 at 16:57
  • Yes, I used RVM to reinstall ruby2.0.0 to get `tk` support. Is there any way to make Automator run the RVM ruby? – Lane Jan 23 '16 at 02:33
  • I think your Automator solution is very good. But I cannot solve my ruby path problem. I choose to use `ruby *.rbw` to open my *.rbw files... – Lane Jan 23 '16 at 08:48