本文介绍了Mac Automator(OS 10.7)无法读取Ruby 1.9.3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.rb文件中有一些Ruby代码,试图与Automator(而不是命令行)一起运行.这是代码示例(文件名是"filelines_revise.rb"):

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

当我使用命令行命令"ruby​​/Desktop/filelines_revise/filelines_revise.rb"运行此代码时,代码运行正常.它找到原始的.txt文件,读取每一行,按照代码要求更改文件,然后使用修改后的行创建一个新文件.

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.

当我尝试将它作为工作流或应用程序放入Automator时,我将Run Shell Script放入我的流中,使用/bin/bash和Pass输入:到stdin,然后输入命令"ruby​​/Desktop/filelines_revise/filelines_revise.rb".当我去运行脚本时,出现错误:

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语法错误,意外的.",期望肯德

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

第18行是上面代码中列出的第二个.gsub".gsub(/ES/,",东侧)".

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

是否有可能Automator不像命令行那样使用我的Ruby 1.9.3?也许我应该对此有所不同?预先感谢.

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.

推荐答案

如何确定您的Ruby版本?我敢打赌,Automator对此一无所知.它可能正在运行内置的Ruby 1.8.7.您可以通过在脚本编写过程中检查RUBY_VERSION来轻松地进行检查.

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.

然后的问题是,双击对象的外壳环境(例如Automator)与终端(使用您的.bash_profile)的外壳环境不同.

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在10.5版之前也有同样的问题.双击可运行的应用程序需要花费特别的精力才能使用Terminal shell环境,而大多数应用程序却没有这样做.

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.

我看了一下Automator,发现如果您运行一个用Run Shell Script操作直接输入的Shell脚本,则必须将弹出窗口切换到/usr/bin/ruby.但是,当然,我的红宝石不是上的红宝石. /usr/bin/ruby为1.8.7;那是我想要使用的红宝石.而且您无法提供shebang线路!

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!

所以我将弹出窗口设置为/bin/bash/并运行此脚本:

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

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

test.rb的内容为:

puts RUBY_VERSION

在Automator中运行它,我仍然得到"1.8.7".我也尝试将其作为应用程序和服务使用.同样的结果.因此,我认为您无法让Automator使用内置的红宝石1.8.7而不直接指向所需的红宝石;它不会像Terminal那样从外壳中拾取它.

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.

这篇关于Mac Automator(OS 10.7)无法读取Ruby 1.9.3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 11:49