[英]how to pass command-line arguments to a program run with the open command?
有沒有辦法將 arguments 傳遞給正在運行的程序:
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,就好像它們是路徑的一部分而不是 arguments。
我試過了:
open -a /Applications/Utilities/Terminal.app ~/my_executable | xargs arg1 arg2
我也試過:
open -a /Applications/Utilities/Terminal.app ~/my_executable --args arg1 arg2
但是有了那個標志,args 被傳遞到終端。
我只允許將 arguments 更改為 Terminal.app([ ] 內的部分):
open -a /Applications/Utilities/Terminal.app [~/my_executable arg1 arg2]
編輯:保留下面的原始答案,因為有些人似乎覺得它很有用,但請記住,這並不能真正回答 OP 的問題,這是將參數傳遞給使用“打開”而不是“打開”的應用程序打開的應用程序,而不是使用終端打開的應用程序.app 用“open”打開。
您可以通過不帶參數運行 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) 上對其進行了測試,所以我不知道早期版本中是否存在該選項。
可能最簡單的方法是創建一個臨時的 shell 腳本,例如
$ echo "~/my_executable arg1 arg2" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh ; rm /tmp/tmp.sh
是的我知道。 需要管理另一個腳本。 但換位思考。 您不是在終端上工作,而是在腳本編輯器上工作。 (不是 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
對於那些對 Paul R 的回答有疑問的人,請添加; rm /tmp/tmp.sh
; rm /tmp/tmp.sh
到 tmp.sh 腳本本身。 不要添加像 Maksim 這樣的睡眠命令,因為這會產生競爭條件。
echo "~/my_executable arg1 arg2 ; rm /tmp/tmp.sh" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh
在 arguments 之前使用--args
開關。
open./Terminal.app --args --your-args
從文檔:
--args: All remaining arguments are passed in argv to the application's main() function instead of opened.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.