编辑:是原始脚本版本中的一个错误,他们没有处理标签中多个会话的情况。修复和重构:)
前面的每个答案都得到了一块拼图。一个改变透明度,但是是静态的;当窗口处于非活动状态时,其他变暗,而不是更改透明度。
要获得所需的行为(根据窗口焦点更改透明度),您需要使用 iTerm2 的 Python API 编写脚本,并将其保存为 AutoLaunch 脚本:
#!/usr/bin/env python3.7
import iterm2
# Store window change event reason objects in more convenient variables
window_selected = [iterm2.FocusUpdateWindowChanged.Reason.TERMINAL_WINDOW_BECAME_KEY]
async def update_tab_transparency(connection, tab, reason):
# Apply to any and all sessions in tab
for session in tab.sessions:
profile = await session.async_get_profile()
change = iterm2.LocalWriteOnlyProfile()
# For window change events bringing a window into focus,
# change transparency to zero
if reason in window_selected:
change.set_transparency(0)
# For window change events taking a window out of focus,
# make sure transparency matches the original profile
else:
# But, only need to do anything if the profile has been changed
# from the original
if profile.original_guid:
original = await iterm2.Profile.async_get(
connection, [profile.original_guid]
)
change.set_transparency(original[0].transparency)
await session.async_set_profile_properties(change)
async def main(connection):
app = await iterm2.async_get_app(connection)
# Initialize for first window
window = app.current_window
if window and window.current_tab:
tab = window.current_tab
reason = window_selected[0]
await update_tab_transparency(connection, tab, reason)
async with iterm2.FocusMonitor(connection) as mon:
while True:
# Block until a tab or window change
update = await mon.async_get_next_update()
# When switching to a new tab, treat as selecting a window
if update.selected_tab_changed:
tab = app.get_tab_by_id(update.selected_tab_changed.tab_id)
reason = window_selected[0]
# For window change events, use the provided event reason
elif update.window_changed:
window = app.get_window_by_id(update.window_changed.window_id)
tab = window.current_tab
reason = update.window_changed.event
# For other focus change events, do nothing
else:
continue
if tab:
await update_tab_transparency(connection, tab, reason)
iterm2.run_forever(main)
有关using Python scripts 的详细信息,请参阅 iTerm 文档
您想在个人资料上设置所需的透明度,并选中“新 Windows 设置”的“使用透明度”框。这听起来违反直觉,但似乎无法通过 API 切换“使用透明度”设置,因此脚本会在创建时快速将窗口切换为不透明。
至少在我的机器上,当第一次选择时,窗口会短暂闪烁,但我不确定我是否能对此做些什么。
注意:macOS 当前(11.6,Big Sur)处理窗口的方式与此相反——它使活动窗口透明(至少,它们的某些部分)和非活动窗口不透明(并褪色)。尝试在 Safari 中打开一个新标签页。这说得通!透明度创造了一种深度感。看到活动窗口后面有窗口强调它在它们前面。
要获得此行为,请使用以下脚本。这种变体更简单,而且运行起来似乎更顺畅(没有闪烁)。如果您将模糊设置为最大,透明度约为 25,则效果最佳。
#!/usr/bin/env python3.7
import iterm2
# Store window change event reason objects in more convenient variables
window_selected = [iterm2.FocusUpdateWindowChanged.Reason.TERMINAL_WINDOW_BECAME_KEY]
async def update_tab_transparency(connection, tab, reason):
# Apply to any and all sessions in tab
for session in tab.sessions:
profile = await session.async_get_profile()
change = iterm2.LocalWriteOnlyProfile()
# For window change events bringing a window into focus,
# make sure transparency matches the original profile
if reason in window_selected:
# But, only need to do anything if the profile has been changed
# from the original
if profile.original_guid:
original = await iterm2.Profile.async_get(
connection, [profile.original_guid]
)
change.set_transparency(original[0].transparency)
# For window change events taking a window out of focus,
# change transparency to zero
else:
change.set_transparency(0)
await session.async_set_profile_properties(change)
async def main(connection):
app = await iterm2.async_get_app(connection)
async with iterm2.FocusMonitor(connection) as mon:
while True:
# Block until a window change
update = await mon.async_get_next_update()
if update.window_changed:
window = app.get_window_by_id(update.window_changed.window_id)
tab = window.current_tab
if tab:
reason = update.window_changed.event
await update_tab_transparency(connection, tab, reason)
iterm2.run_forever(main)