python - 如何使用 Python API 在 iTerm 会话中运行某些命令
问题描述
我正在尝试编写一个脚本来在我的 iTerm2 上进行一些自动化。我正在为 iTerm 使用 Python API,因为我对 AppleScript 一无所知。
我需要做的基本上是,分屏是6个窗口,每个窗口本地运行6个微服务。我成功地分割了屏幕,但我无法在其中任何一个中运行命令。
提前致谢。
我现有的代码如下
#!/usr/bin/env python3.7
import iterm2
# This script was created with the "basic" environment which does not support adding dependencies
# with pip.
async def main(connection):
# Your code goes here. Here's a bit of example code that adds a tab to the current window:
app = await iterm2.async_get_app(connection)
window = app.current_terminal_window
if window is not None:
await window.async_create_tab()
else:
# You can view this message in the script console.
print("No current window")
leftOne = app.current_terminal_window.current_tab.current_session
rightOne = await leftOne.async_split_pane(vertical=True)
leftTwo = await leftOne.async_split_pane()
leftThree = await leftOne.async_split_pane()
rightTwo = await rightOne.async_split_pane()
rightThree = await rightOne.async_split_pane()
await leftOne.async_activate()
await leftOne.as
iterm2.run_until_complete(main)
解决方案
您可以使用该方法将击键发送到会话async_send_text()
。
在您的代码中,以下将执行命令:
leftOne = app.current_terminal_window.current_tab.current_session
await leftOne.async_send_text('whoami\n')
推荐阅读
- node.js - 在 Electron 应用中使用 socket.io P2P 共享文件
- swift - 无法从 json 响应创建字符串 SwiftUI 列表
- database - 尝试在 linux mint 中安装 oracle xe 18c 但出现错误。有什么建议么?
- reactjs - 在 TextArea 问题中反应计数单词
- python - 询问有关优化 while 循环的建议
- python - Pyplot 结合两个子图轴
- python - 无需回退(搜索) io.Bytes() 对象即可使用 docx add_picture 包含图像
- apache-spark - 为什么对一个很小的 azure databricks 数据库表的查询需要 5 秒才能返回?
- javascript - 触发 javascript 函数的输入
- python - python - 如何在python中以像素数为x轴,灰度颜色为y轴绘制图形?