如何从命令行在 iTerm window 中执行命令?
How do I execute a command in an iTerm window from the command line?
我如何从命令行 运行 iTerm 会话,传递要在 iTerm window 中执行的命令?
xterm 模拟是 -e
,即
xterm -e sleep 10
您最好为此使用 Applescript。 iTerm2 有一些 examples 脚本。文档有点粗制滥造,但这些示例应该让您了解从哪里开始。
您可以将 Applescript 字符串包装在 bash 脚本中,然后使用 osascript
:
启动它
#~/bin/bash
tell application "iTerm"
# etc...
exec command "$@"
那么运行脚本就这么简单:
./run-in-iterm.sh "echo 'hello world'"
我同意 Alex 的观点,使用 AppleScript 是最好的方式。
这是我的 "iterm" 脚本,我将其 chmod 为可执行文件并将其放在我路径中的目录中。我可以这样使用它:
引用包含 shell 个参数:
iterm "ls -l"
将多个命令传递给 运行:
iterm "calculatesomthing" "exit"
传递多个命令,分号分隔:
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 ``-'' $@
仅供参考:您可能想要删除 "say" 命令,我将其用作每个正在执行的 cmd 的 remote/audible 通知。我将一堆 cmd 传递给多个自定义 iTerm profiles/shell,这些 iTerm profiles/shell 被平铺到一个大的平面屏幕上以显示复杂的多 DC Azure 部署的状态...
PS:我添加了一个要点,因为脚本最后一行中的引号对某人来说 cut/pasting 不正确 @ https://gist.github.com/sushihangover/7563e1707e98cdf2b285
我找到了 official documentation,但没想到像 SushiHangover 那样用 osascript 包装 Applescript - 非常好。他的回答对我不起作用,可能是因为我使用的是最新的 beta 3.0 版本,所以这里的答案确实有效(并且也简化了一点)。
#!/bin/bash
osascript - "$@" <<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
我如何从命令行 运行 iTerm 会话,传递要在 iTerm window 中执行的命令?
xterm 模拟是 -e
,即
xterm -e sleep 10
您最好为此使用 Applescript。 iTerm2 有一些 examples 脚本。文档有点粗制滥造,但这些示例应该让您了解从哪里开始。
您可以将 Applescript 字符串包装在 bash 脚本中,然后使用 osascript
:
#~/bin/bash
tell application "iTerm"
# etc...
exec command "$@"
那么运行脚本就这么简单:
./run-in-iterm.sh "echo 'hello world'"
我同意 Alex 的观点,使用 AppleScript 是最好的方式。
这是我的 "iterm" 脚本,我将其 chmod 为可执行文件并将其放在我路径中的目录中。我可以这样使用它:
引用包含 shell 个参数:
iterm "ls -l"
将多个命令传递给 运行:
iterm "calculatesomthing" "exit"
传递多个命令,分号分隔:
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 ``-'' $@
仅供参考:您可能想要删除 "say" 命令,我将其用作每个正在执行的 cmd 的 remote/audible 通知。我将一堆 cmd 传递给多个自定义 iTerm profiles/shell,这些 iTerm profiles/shell 被平铺到一个大的平面屏幕上以显示复杂的多 DC Azure 部署的状态...
PS:我添加了一个要点,因为脚本最后一行中的引号对某人来说 cut/pasting 不正确 @ https://gist.github.com/sushihangover/7563e1707e98cdf2b285
我找到了 official documentation,但没想到像 SushiHangover 那样用 osascript 包装 Applescript - 非常好。他的回答对我不起作用,可能是因为我使用的是最新的 beta 3.0 版本,所以这里的答案确实有效(并且也简化了一点)。
#!/bin/bash
osascript - "$@" <<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