Sam Rayner

Hi, I’m Sam. I design and build apps for iOS and the web. I live in Sheffield, UK and play Ultimate far too much. You can reach me via email or on Twitter.

Using Ruby 1.9 with Alfred

I’ve been building a Sprint.ly Workflow for Alfred 2 (currently in beta) and wanted to use it as an opportunity to brush up on my TDD and Ruby-foo.

Alfred 2 let’s you run PHP, Python, Perl and Ruby scripts but they run from usr/bin/lib and Mountain Lion ships with (a pretty outdated) Ruby 1.8.7.

If you have a newer version of Ruby installed with RVM or rbenv, here’s how to bootstrap Alfred to use it instead. In your Workflow, set up a Ruby script action as normal and fill it with:

SCRIPT_FILE = "example"

def ruby_exec_path(manager)
  begin
    ruby_path = `~/.#{manager}/bin/#{manager} which ruby`
    if $?.exitstatus == 127
      raise Errno::ENOENT
    end
  rescue Errno::ENOENT
    ruby_path = ""
  end
  return ruby_path.strip
end

ruby_path = ruby_exec_path("rvm")
ruby_path = ruby_exec_path("rbenv") unless ruby_path.length
ruby_path = "ruby" unless ruby_path.length

parent_dir = File.expand_path(File.dirname(__FILE__))
puts `#{ruby_path} "#{SCRIPT_FILE}.rb" "{query}"`

This fetches your active Ruby and runs the script example.rb, passing the Alfred query as an argument.

Create example.rb in your Workflow directory and you can grab the query string inside it:

QUERY = ARGV[0]

Anything your file puts into STDOUT will be passed back to Alfred to use in your Workflow!

As well as letting you use The New Ruby Hotness, this has the added benefit of moving code out of Alfred and into files that are more easily tested and version controlled. The only down-side is that your Workflow will require users to be running RVM or rbenv if you share it.


Update (Aug 4): Good news! OS X Mavericks is set to ship with Ruby 2.0 by default so you shouldn’t need to use this hack for much longer.

Post Archive

2016

2015

July

2014

February