如何将命令行参数传递给使用open命令运行的程序?

时间:2015-04-08 09:27:08

标签: macos bash

有没有办法将参数传递给正在运行的程序:

open -a /Applications/Utilities/Terminal.app ~/my_executable

我试过了:

open -a /Applications/Utilities/Terminal.app ~/my_executable arg1 arg2

但这被解释为告诉终端打开~/my_executable ~/arg1 ~/arg2.

我试过了:

open -a /Applications/Utilities/Terminal.app '~/my_executable arg1 arg2'

但是它选择了arg1和arg2,好像它们是路径的一部分而不是参数。

我试过了:

open -a /Applications/Utilities/Terminal.app ~/my_executable | xargs arg1 arg2

我也尝试过:

open -a /Applications/Utilities/Terminal.app ~/my_executable --args arg1 arg2

但是使用那个标志,args会传递给终端。

我只允许将参数更改为Terminal.app([]中的部分):

open -a /Applications/Utilities/Terminal.app [~/my_executable arg1 arg2]

3 个答案:

答案 0 :(得分:9)

您可以通过不带参数运行来找到答案:

% open Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]

[...]

--args All remaining arguments are passed in argv to the application's main() function instead of opened.

[...]

您可以看到有一个选项--args,您可以像这样使用它:

open ./Untitled.app --args arg1 arg2 arg3

我在el Capitan(10.11.3)上测试过,所以我不知道早期版本中是否有该选项。

答案 1 :(得分:7)

最简单的方法可能是创建一个临时shell脚本,例如

$ echo "~/my_executable arg1 arg2" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh ; rm /tmp/tmp.sh

答案 2 :(得分:1)

是的,我知道。需要管理另一个脚本。 但想一想。你不是在终端上工作,而是在脚本编辑器上工作。 (不是bash脚本,而是AppleScript&#39; ing)

property testScript : "/tmp/sh.sh"

set input to display dialog "args?" default answer ""
log input
tell application "Terminal"
    activate
    do script testScript & " " & text returned of input
end tell