如何将变量从 ruby​​ 脚本传递到 automator 应用程序?

发布于 2024-11-29 12:45:42 字数 96 浏览 1 评论 0原文

所以我有一个 ruby​​ 脚本,显然使用了变量。该脚本的一部分打开一个应用程序并运行自动化工作流程。我的 ruby​​ 脚本中有一些变量需要我的工作流程使用。无论如何这可能吗?

So I have a script in ruby that obviously uses variables. Part of the script opens an app and runs an automator workflow. There are some variables in my ruby script that I need my workflow to use. Is this possible in anyway?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小姐丶请自重 2024-12-06 12:45:42

你是否能够:
a) 从 Ruby 执行命令行程序
b) 将您的 Automator 工作流程保存为应用程序

如果是这样,您应该能够运行 open 命令,例如 open test.app --args someArg。或者您可以使用 automator 命令,例如 echo "someArg" | automator -i - test.app

请注意,整个 Automator 脚本将为每个参数运行一次 - 尝试将“说出文本”作为第一个项目来验证这一点。

要一次性使用所有参数,您实际上只需要传递一个参数,然后将其拆分,例如 open test.app --args "one|two|third|four" 然后是类似的

on run input
    set myArray to my theSplit(input as string, "|")
    set a to item 1 of myArray
    set b to item 2 of myArray
    set c to item 3 of myArray
    set d to item 4 of myArray
    display dialog "c is " & c

        --do stuff
    return str
end run

on theSplit(theString, theDelimiter)
    -- save delimiters to restore old settings
    set oldDelimiters to AppleScript's text item delimiters
    -- set delimiters to delimiter to be used
    set AppleScript's text item delimiters to theDelimiter
    -- create the array
    --set theArray to every text item of theString
    set theArray to text items of theString
    --display dialog theArray as string
    -- restore the old setting
    set AppleScript's text item delimiters to oldDelimiters
    -- return the result
    return theArray
end theSplit

但是,AppleScript 似乎只有在不是第一个操作时才起作用。如果您需要将其作为第一个操作(您可能会这样做),请先插入一个仅传递参数的运行 Shell 脚本:

for f
do
  echo “$f"
done

Are you able to:
a) Execute a command line program from Ruby
b) Save your Automator workflow as an application

If so, you should be able to run the open command, e.g. open test.app --args someArg. Or you could use the automator command, such as echo "someArg" | automator -i - test.app

Note that the entire Automator script will run once for each argument - try having ‘Speak Text’ as your first item to verify this.

To work with all the parameters in one go you need to actually pass just one and then split it up, e.g. open test.app --args "one|two|three|four" then something like

on run input
    set myArray to my theSplit(input as string, "|")
    set a to item 1 of myArray
    set b to item 2 of myArray
    set c to item 3 of myArray
    set d to item 4 of myArray
    display dialog "c is " & c

        --do stuff
    return str
end run

on theSplit(theString, theDelimiter)
    -- save delimiters to restore old settings
    set oldDelimiters to AppleScript's text item delimiters
    -- set delimiters to delimiter to be used
    set AppleScript's text item delimiters to theDelimiter
    -- create the array
    --set theArray to every text item of theString
    set theArray to text items of theString
    --display dialog theArray as string
    -- restore the old setting
    set AppleScript's text item delimiters to oldDelimiters
    -- return the result
    return theArray
end theSplit

However, the AppleScript only seems to work if it’s not the first action. If you need it as the first action, which you probably do, insert a Run Shell Script first that just passes on the arguments:

for f
do
  echo “$f"
done
|煩躁 2024-12-06 12:45:42

是的,可以将参数传递给 ruby​​ 脚本。这是一个很好的教程:

http://ruby.about.com/od/rubyfeatures /a/argv.htm

Yes, it is possible to pass parameters to a ruby script. Here is a nice tutorial:

http://ruby.about.com/od/rubyfeatures/a/argv.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文