2015-09-20 243 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