2015-09-20 268 views

回答

5

我與亞歷克斯同意,使用AppleScript是最好的選擇。

這是我的「iterm」腳本,我chmod作爲可執行文件並將其放在我的路徑中的一個目錄中。我可以這樣使用它:

報價封閉外殼參數:

iterm "ls -l" 

傳遞多個CMDS運行:

iterm "calculatesomthing" "exit" 

傳遞多個CMDS,分號separtated:

iterm "cd ~/mediaprojects; ./gitSyncAll; exit" 

自封閉的bash/Applescript:

#!/bin/bash 
read -r -d '' script <<'EOF' 
on run argv 
tell application "iTerm" 
    activate 
    set myterm to (make new terminal) 
    tell myterm 
     launch session "Default" 
     tell the last session 
      repeat with arg in argv 
       say arg 
       write text arg 
      end repeat 
     end tell 
    end tell 
end tell 
end run 
EOF 
echo "$script" | osascript ``-'' [email protected] 

僅供參考:您可能需要刪除「say」命令,我將其用作每個正在執行的cmd的遠程/聲音通知。我通過一堆cmds到多個自定義iTerm配置文件/外殼,平鋪到一個大的純平屏幕,以顯示覆雜的多DC Azure部署的狀態...

PS:我添加了一個要點作爲報價腳本的最後一行沒有被剪切/正確粘貼給某人@https://gist.github.com/sushihangover/7563e1707e98cdf2b285

2

你最好使用AppleScript這一點。 iTerm2有一些examples腳本。這些文檔有點粗俗,但這些例子應該讓你知道從哪裏開始。

您可以在bash腳本包裹AppleScript的字符串,然後使用osascript啓動它:

#~/bin/bash 
tell application "iTerm" 
    # etc... 
    exec command "[email protected]" 

然後運行該腳本很簡單,只要:

./run-in-iterm.sh "echo 'hello world'" 
+1

我同意,AppleScript是要走的路,我添加了一個Answer,它具有我使用的自封閉bash/AppleScript腳本。 – SushiHangover

3

我發現了official documentation,但沒有想過用像SushiHangover這樣的osascript包裝Applescript-非常好。他的回答對我不起作用,可能是因爲我使用的是最新的beta 3.0版本,所以這裏有一個工作(並且簡化了一點)。

#!/bin/bash 
osascript - "[email protected]" <<EOF 
on run argv 
tell application "iTerm" 
    activate 
    set new_term to (create window with default profile) 
    tell new_term 
     tell the current session 
      repeat with arg in argv 
       write text arg 
      end repeat 
     end tell 
    end tell 
end tell 
end run 
EOF