Python学习群 iTerm2 的 文档页面 显示,其最新测试版增加了 Python API。具体来说就是,iTerm2 提供了一个 Python 包,通过它我们可以轻松编写控制 iTerm2 并扩展其行为的 Python 脚本。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1553600654913" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);"></tt-image>
Python学习群:683380553,有大牛答疑,有资源共享!是一个非常不错的交流基地!欢迎喜欢Python的小伙伴!
当然,该功能目前尚处于 Beta 阶段,API 可能偶尔会发生变化。
示例代码
Function Key Tabs(通过功能键切换选项卡)
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#!/usr/bin/env python3
import asyncio
import iterm2
async def main(connection):
app = await iterm2.async_get_app(connection)
keycodes = [ iterm2.Keycode.F1,
iterm2.Keycode.F2,
iterm2.Keycode.F3,
iterm2.Keycode.F4,
iterm2.Keycode.F5,
iterm2.Keycode.F6,
iterm2.Keycode.F7,
iterm2.Keycode.F8,
iterm2.Keycode.F9,
iterm2.Keycode.F10,
iterm2.Keycode.F11,
iterm2.Keycode.F12 ]
async def keystroke_handler(connection, keystroke):
if keystroke.modifiers == [ iterm2.Modifier.FUNCTION ]:
try:
fkey = keycodes.index(keystroke.keycode)
if fkey >= 0 and fkey < len(app.current_terminal_window.tabs):
await app.current_terminal_window.tabs[fkey].async_select()
except:
pass
pattern = iterm2.KeystrokePattern()
pattern.forbidden_modifiers.extend([iterm2.Modifier.CONTROL,
iterm2.Modifier.OPTION,
iterm2.Modifier.COMMAND,
iterm2.Modifier.SHIFT,
iterm2.Modifier.NUMPAD])
pattern.required_modifiers.extend([iterm2.Modifier.FUNCTION])
pattern.keycodes.extend(keycodes)
async def monitor():
async with iterm2.KeystrokeMonitor(connection) as mon:
while True:
keystroke = await mon.async_get()
await keystroke_handler(connection, keystroke)
# Run the monitor in the background
asyncio.create_task(monitor())
# Block regular handling of function keys
filter = iterm2.KeystrokeFilter(connection, [pattern])
async with filter as mon:
await iterm2.async_wait_forever()
iterm2.run_forever(main)
</pre>
使用该脚本,我们可以通过按下功能键来选择选项卡。F1 表示选择第一个选项卡,F2 表示选择第二个选项卡等。 >>> 脚本下载
官方还提供了 关于该功能的教程 ,包含了编写脚本的详细指南,并描述了 iTerm2 脚本系统的架构。
iTerm2 是iTerm的后继者,也是 Terminal 的替代者。这是一款用于 macOS 的终端模拟器,支持窗口分割、热键、搜索、自动补齐、无鼠标复制、历史粘贴、即时重播等功能特性,适用于 macOS 10.10 及以上版本。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1553600654923" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);"></tt-image>
:683380553,有大牛答疑,有资源共享!是一个非常不错的交流基地!欢迎喜欢Python的小伙伴!
网友评论