Ruby Scripting in FileMaker

Posted by Alex G 03/03/2010 at 05:08PM

There are many cases where FileMaker's scripting isn't always the best tool for the job and where a language like Ruby can bring a lot of power to your FileMaker development. The following is a description of a simple technique for using ruby scripts from within a FileMaker solution without the use of a plugin. I've found this technique useful for employing regular expressions for complex text parsing, making web requests to work with web APIs, and for parsing and generating XML and other serialized data structures. Ruby has a wealth of great libraries for doing anything you can imagine and is just plain fun to write.

How It Works

Ruby script execution process

Ruby code stored in a FileMaker global is saved as a ruby executable using AppleScript. AppleScript then cleans up the code's formatting, replacing FileMaker's line breaks with unix style line feeds. A shell script is then executed running the ruby script with any specified parameters. And finally, the stdout results are stored back into FileMaker by way of AppleScript.

Caveats

There are a number of caveats with this technique. The first and most obvious is that this is a Mac only solution – AppleScript = Mac. But another biggie is performance. There is considerable overhead in compiling and running AppleScript, pulling and pushing data to FileMaker, writing the executable, and then running the script. All these steps makes this technique unsuitable for repetitive operations.

This technique also lacks the ability to capture errors in the ruby runtime. This could effectively be handled by wrapping the ruby execution in an AppleScript try statement to capture errors, but would make filemaker side error capture of the applescript statement slightly more complex.

The Examples

Example File

Attached to this post is a FileMaker file that demonstrates the technique and that can be used to write and execute ruby scripts of your own. Also included are a number of example scripts that demonstrate passing parameters, using regular expressions, making http requests, making multi threaded web requests, parsing xml, and more.

Script parameters are passed as command line arguments and are available in the ARGV array inside the script. In most of the examples I make use of the getoptlong ruby library to handle passing parameters by name. Here is how that is handled in the "Update my twitter status ( user ; pass ; status )" script.

require 'getoptlong'

opts = GetoptLong.new(
  [ "--user",         "-u",   GetoptLong::REQUIRED_ARGUMENT ],
  [ "--password",     "-p",   GetoptLong::REQUIRED_ARGUMENT ],
  [ "--status",       "-s",   GetoptLong::REQUIRED_ARGUMENT ]
)

user, password, status = ''
opts.each do |opt, arg|
  case opt
    when '--user'
      user = arg
    when '--password'
      password = arg
    when '--status'
      status = arg
  end
end

After we properly handle parameter passing we only need the following lines, which employ ActiveResource, to update your twitter status.

class Twitter < ActiveResource::Base
  self.site = "http://twitter.com/"
  self.element_name = 'status'
  self.timeout = 5
end

Twitter.user = user
Twitter.password = password
Twitter.post(:update, :status => status)

Ruby script example

Comments

Leave a comment

  1. John Lannon 19 days later:

    Cool technique, Alex. As a rubyist, fmp enthusiast and mac user, this is right in my wheelhouse. I’ve been using bbox to execute ruby scripts, but this is a creative way to avoid the use of a plugin.

    Looks like the newest version of bbox has some placeholder functions for compilation and execution of ruby scripts (like the currently supported python functionality). Any word on if/when those functions might work?

    Also, have you experimented with this plugin-less technique on Windows? I avoid it when possible, but would imagine that by substituting VB or Jscript for AppleScript, one could accomplish the above solution on Windows.

    Thanks, John

  2. Simon 26 days later:

    @John

    I need to finish off the Python functionality first, which is at least partially complete, but more elaborate then what can easily be done with Ruby. Once that’s complete that I should be able to get the Ruby functions done quickly.

    Another developer at Beezwax, Ammar, has also done some work in FM/Ruby integration, although he is primarily concerned with Rails and FileMaker integration. Maybe we can get him to chime in at a later date on his work.

Comments