Jump to content
buddylreno

Creating a Basic Workflow with Ruby

Recommended Posts

I really love workflows and I wanted to start creating my own.  I'm mostly a front-end developer and use ruby for the most part on any back-end dev work I do.  I saw that Alfred supports Ruby workflows which made me very excited.

 

I did some googling and had a hard time finding some tutorials about creating workflows with ruby, plenty with php though.  I needed to learn the basics of workflows and wanted to do it in a language I knew (not familiar with php).

 

I wrote a post detailing how a basic workflow functions with ruby.  It's nothing you can't find anywhere else, but it was a good exercise to get the basics figured out and create something that helps me weekly.

 

http://buddylreno.github.io/adventures-with-alfred-a-basic-ruby-workflow/

 

I look forward to all the interesting workflows people are creating!

Share this post


Link to post

I look forward to all the interesting workflows people are creating!

 

I'll just pop this in the Workflow Help & Questions section of the forum, as you'll get more replies relating to workflows there :)

Share this post


Link to post

I'll just pop this in the Workflow Help & Questions section of the forum, as you'll get more replies relating to workflows there :)

 

oh awesome! I didn't realize there was a separate forum for that.  Thank you!

Share this post


Link to post

Cool intro.
 
FWIW, I generally discourage people from pasting more than a couple of lines in the Script box.
 
It makes both coding and debugging far more difficult. There's no syntax highlighting or any of the other benefits you get from a real programmer's editor, and it's difficult to track down exceptions. Ruby, Python etc. will tell you the line number where the error occurred. This is super helpful, but not if your code is in the Script box where there are no line numbers.
 
A much better idea is to put the code in a file in your workflow's directory and call it via the Script box (and bash):

/usr/bin/ruby myscript.rb "{query}"

It's also a good idea to use the full path to the interpreter. If you distribute your workflow, this insulates your code somewhat from a user messing about with their environment. For example, if someone has buggered about with their environment sufficiently that python resolves to some version of Python 3, your workflow will very likely not work (the system Python is version 2, and they're not particularly compatible).

 

If a user has replaced the system Python at /usr/bin/python, that's their own stupid fault.

 

So, will you be writing some more tutorials using the Ruby template? That's an awesome library. I stole a lot of its ideas for my Python library.

Share this post


Link to post

Hi @deanishe,

 

Yeah, the script box isn't very useful for larger concepts.  I actually developed the code with Atom and ran my script from terminal directly to get proper errors while making it. I wasn't sure yet how the workflow folder worked and wanted to get a grasp on the data moving through workflow items before jumping into better script maintenance.  However, I will definitely update my workflow and post now that you've helped me understand the piece I was missing.

 

I definitely plan to write more tutorials using the Ruby template! Now that I've got a much better grasp of workflows, I feel more apt to take it on and understand the reasons why pieces of code are in certain places.  This particular workflow I wrote about definitely has some improvements that can be made using that Ruby template.

Share this post


Link to post

I imagine you've figured it out already, but Alfred runs your workflow with the workflow's directory as the current working directory.

 

You have to watch out a bit when running scripts from a shell due to the environment. Alfred has an essentially empty environment—in particular, LANG etc. aren't set, so Ruby will default to ASCII encoding when run from Alfred. This causes quite a few bugs, as most people have a more sensible (i.e. UTF-8) environment in their shell.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...