Mac Automator (OS 10.7) not reading Ruby 1.9.3?

2019-03-03 11:09发布

I have some Ruby code in a .rb file that I am trying to run with Automator as opposed to the Command Line. Here is a sample of the code (filename is "filelines_revise.rb"):

lines = IO.readlines('filelines_before_CAP.txt').map do |line|

  array = line.split.each { |i| i.capitalize! }

  if array.include?("Ws")
    array.delete("Ws")
    array[-1,0] = "Ws"
  end

  if array.include?("Es")
    array.delete("Es")
    array[-1,0] = "Es"
  end

  array_2 = array.join(" ")

  array_2.gsub(/ Ws /, ", west side")
         .gsub(/ ES /, ", east side")
end

File.open('filelines_after_CAP.txt', 'w') do |file|
  file.puts lines
end

When I run this code using the command line command "ruby /Desktop/filelines_revise/filelines_revise.rb" and the code runs fine. It finds the original .txt file, reads each line, changes the file as the code dictates, then creates a new file with the revised lines.

When I try to put this into Automator as either a Workflow or an App, I put Run Shell Script to my flow, using /bin/bash with a Pass input: to stdin, then the command "ruby /Desktop/filelines_revise/filelines_revise.rb". When I go to run the script I get an error reading:

Desktop/filelines_revise/filelines_revise.rb:18 syntax error, unexpected '.', expecting Kend

Line 18 is the 2nd .gsub ".gsub(/ ES /, ", east side")" listed in the code above.

Is it possible Automator isn't using my Ruby 1.9.3 as the Command Line does? Perhaps I should be going about this differently? Thanks in advance.

1条回答
男人必须洒脱
2楼-- · 2019-03-03 11:32

How is your Ruby version determined? I'm betting that Automator knows nothing of it. It is probably running the built-in Ruby 1.8.7. You can easily check that by examining RUBY_VERSION in the course of your script.

The issue would then be that the shell environment for double-clickables (like Automator) is not the same as the shell environment for the Terminal (which uses your .bash_profile).

BBEdit had the same problem until version 10.5, by the way. It takes a special effort for a double-clickable app to pick up the Terminal shell environment, and most applications do not make that effort.

EDIT: I took a look at Automator and discovered that if you run a shell script that you enter literally with the Run Shell Script action, you have to switch the popup to /usr/bin/ruby. But of course my ruby is not the ruby at /usr/bin/ruby. /usr/bin/ruby is 1.8.7; that is the ruby I do not want to use. And you can't provide a shebang line!

So I set the popup to /bin/bash/ and ran this script:

/usr/bin/env ruby /Users/matt/Desktop/test.rb

Where test.rb reads:

puts RUBY_VERSION

Running that within Automator, I still got "1.8.7". I also tried it as an application and as a service; same result. So I don't think you can ever get Automator to use anything but the built-in ruby 1.8.7 without pointing directly to the ruby that you want; it doesn't pick it up from the shell the way Terminal does.

查看更多
登录 后发表回答