有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

有没有办法通过以下方式将参数传递给正在运行的程序:

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

但是有了这个标志,arg就被传递到终端

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

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

共 (3) 个答案

  1. # 1 楼答案

    是的,我知道。需要管理另一个脚本。 但是换个角度思考。您不是在终端上工作,而是在脚本编辑器上工作。 (不是bash脚本,而是AppleScript'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
    
  2. # 2 楼答案

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

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

    编辑:保留下面的原始答案,因为有些人似乎觉得它很有用,但请记住,这并不能真正回答OP的问题,这是将参数传递给使用“open”打开的应用程序,而不是传递给使用Terminal打开的应用程序。用“打开”打开的应用程序

    您可以在不带参数的情况下运行open来找到答案:

    % 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)上测试了它,所以我不知道早期版本中是否存在该选项