Jump to content
radioman

Ruby workflows in Mavericks

Recommended Posts

I'm becoming less convinced that this is a Ruby problem at all, but an Alfred / Mavericks problem.  

 

If I run my Ruby script directly using the same path/command as Alfred it works fine.  However, from Alfred it immediately fails silently as described earlier in this thread.

 

If I go one step further and modify the workflow itself, for instance flip it to try and run the Ruby script with Python, I see exactly the same behaviour, and still nothing in the logs.

 

It seems that Alfred is immediately failing the execution of the script and not logging anything to the Alfred-Workflow.log.

 

It looks like it's an encoding issue. When I ran your workflow in Alfred, it output this error message:

plist-3.1.0/lib/plist/parser.rb:91:in `scan': invalid byte sequence in US-ASCII (ArgumentError)
	from plist-3.1.0/lib/plist/parser.rb:91:in `parse'
	from plist-3.1.0/lib/plist/parser.rb:29:in `parse_xml'
	from alfred-workflow-1.11.1/lib/alfred.rb:122:in `info_plist'
	from alfred-workflow-1.11.1/lib/alfred.rb:127:in `bundle_id'
	from alfred-workflow-1.11.1/lib/alfred.rb:100:in `ui'
	from alfred-workflow-1.11.1/lib/alfred.rb:47:in `rescue in with_friendly_error'
	from alfred-workflow-1.11.1/lib/alfred.rb:28:in `with_friendly_error'
	from ./search.rb:62:in `<main>'

I did some further testing and it looks like the external encoding of Ruby is set to US-ASCII when run in Alfred, while it's normally set to UTF-8 when a script is run elsewhere. Looks like this is why it works in the Terminal but errors out in Alfred.

 

You can manually set the external encoding to UTF-8 by adding the following line of code at the top of your Ruby file:

Encoding::default_external = Encoding::UTF_8 if defined? Encoding

That seems to fix the issues on my system anyway. Hope that helps.

Share this post


Link to post

Can I confirm that this is the only change that you made to the workflow?  I just did a fresh install of it from https://github.com/neilmiddleton/alfred-devcenter-search/raw/master/Heroku%20DevCenter%20Search.alfredworkflow and made that one change, but no cigar.

 

Ah yeah, that download doesn't seem to have the Ruby 2.0.0 versions of the gems bundled with it. When I cloned the repository into Alfred it worked fine.

Share this post


Link to post

Got it now, thanks.

 

So, for posterity, there's two things here.  Firstly you need to include the Ruby 2.0.0 gems as well as 1.8 for pre-mavericks installs, and then you need to respect the different encoding that Alfred seems to run with Ruby scripts (as per Clinton's response).

Share this post


Link to post

Got it now, thanks.

 

So, for posterity, there's two things here.  Firstly you need to include the Ruby 2.0.0 gems as well as 1.8 for pre-mavericks installs, and then you need to respect the different encoding that Alfred seems to run with Ruby scripts (as per Clinton's response).

 

I suppose there is another option: you could just force your workflow to run on Ruby 1.8.7. Mavericks comes with both 1.8.7 and 2.0.0 installed, you just have to point it to the full path for 1.8.7.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

That should work with older installs of OS X as well. Just throwing that out there.

Share this post


Link to post

I suppose there is another option: you could just force your workflow to run on Ruby 1.8.7. Mavericks comes with both 1.8.7 and 2.0.0 installed, you just have to point it to the full path for 1.8.7.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

That should work with older installs of OS X as well. Just throwing that out there.

 

Thanks for helping run this to ground Clinton

Share this post


Link to post

FYI: I have a markdown ruby script I run via Alfred - actually a bash script in alfred that just passes selected text to the ruby script.  That too was failing since upgrading to Mavericks with the raw ruby script just generating errors.

 

Updating the script to specifically call Ruby 1.8 as Clinton suggests immediately fixed the issue.

Share this post


Link to post

I guess we can say that a simple 

   sudo ln -s /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby /usr/bin/ruby

will help most Mavericks users to keep using their SL/ML workflows, right?

Share this post


Link to post

I guess we can say that a simple 

   sudo ln -s /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby /usr/bin/ruby

will help most Mavericks users to keep using their SL/ML workflows, right?

 

And cause untold issues for anyone doing anything else with Ruby.

Share this post


Link to post

And cause untold issues for anyone doing anything else with Ruby.

I see.. I am not a ruby user and have very little understanding of the "programming" part of the workflows, so may i ask how one can 

modify a workflow so that the workflow itself calls the older version of ruby and not the whole system? 

If its a 2-3 lines answer that is and doesn't require reading like 10+ pdf's about programming workflows/ruby :D

Share this post


Link to post

I see.. I am not a ruby user and have very little understanding of the "programming" part of the workflows, so may i ask how one can 

modify a workflow so that the workflow itself calls the older version of ruby and not the whole system? 

If its a 2-3 lines answer that is and doesn't require reading like 10+ pdf's about programming workflows/ruby :D

 

I think you kinda already have the answer. What you would do is, instead of letting the workflow use /usr/bin/ruby, which is not the desired version, move all the ruby code to a file, leave the language as /usr/bin/bash, and execute the file with the desired version of ruby (unless ruby has some method for specifying the version to run as?). So, as a bash command do..

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby <file.rb>

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