如何用 shell 开启一个新的 Terminal 或者 iTerm,并在其中执行指定命令??

2015 年 8 月 28 日
 FatGhosta

问题的后半句是关键!!

我想在 Terminal 里,用 shell 命令再调用出一个 Terminal ,再在这个刚启动的执行刚才 shell 命令中带的命令???
我现在用exec /Applications/iTerm.app/Contents/MacOS/iTerm,在 iTerm 中启动新的 iTerm 。我想执行类似 exec /Applications/iTerm.app/Contents/MacOS/iTerm | xargs ls, 在新打开的 iTerm 中执行ls命令。这个命令显然不行。但是有没有这种命令??

或者通过 swift 可以?

9056 次点击
所在节点    Apple
12 条回复
fatestigma
2015 年 8 月 28 日
你说的方法我不太会。
但是 AppleScript 可以实现。对应的 shell 是 osascript -e;
比如 iTerm 的:
```AppleScript
osascript -e 'tell application "iTerm"
set _term to (make new terminal )
tell _term
launch session "Default"
set _session to current session
end tell
tell _session
write text "ls"
end tell
end tell'
```
fatestigma
2015 年 8 月 28 日
缩进怎么不见了。。另外倒数第三行那个双引号里的可以直接修改成你要执行的。
```
osascript -e 'tell application "iTerm"
set _term to (make new terminal )
tell _term
launch session "Default"
set _session to current session
end tell
tell _session
write text "ls"
end tell
end tell'
```
FatGhosta
2015 年 8 月 28 日
@fatestigma Mac 小白,不知道还有 AppleScript 这么个神器。。。
pityonline
2015 年 8 月 28 日
其实你需要的是 tmux ,只在一个终端里全搞定!
ooonme
2015 年 8 月 28 日
open -a Terminal
ooonme
2015 年 8 月 28 日
osx 里的
ooonme
2015 年 8 月 28 日
如果本身在 Termianl 执行 open -n -a Terminal
FatGhosta
2015 年 8 月 29 日
@ooonme exec 也可以,最重要的是能不能这个命令把在新 Terminal 中的要输入的命令带上。
Creolophus
2017 年 7 月 28 日
@fatestigma set myterm to (make new terminal)
Creolophus
2017 年 7 月 28 日
@fatestigma
tell application "iTerm"
activate
set myterm to (make new terminal)
end tell

error "The variable terminal is not defined." number -2753 from "terminal"
terminal 未定义是怎么回事啊
fatestigma
2017 年 7 月 28 日
@Creolophus /这是一个创建于 699 天前的主题,其中的信息可能已经有所发展或是发生改变。/
简单来说,就是 iTerm2 3.0 的 AppleScript 字典变了,terminal 关键字没有了。

tell application "iTerm"
create window with default profile
tell current session of current window to write text "ls"
end tell
noogler67
2018 年 3 月 21 日
open -a Terminal bash.sh
open -a iTerm bash.sh
如果要新窗口 -n
注意 bash.sh 需要有执行权限
提前执行 sudo chmod +x bash.sh

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/216776

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX