原生的 MacOS 终端
可以设置无效和有效窗格的透明度。iTerm2 可以设置有效窗格的透明度,但我找不到无效窗格的设置,或许是没有这个选项? 我知道 iTerm2 可以使无效分割窗格变暗,但我想要的是透明度。
感谢帮助。
原生的 MacOS 终端
可以设置无效和有效窗格的透明度。iTerm2 可以设置有效窗格的透明度,但我找不到无效窗格的设置,或许是没有这个选项? 我知道 iTerm2 可以使无效分割窗格变暗,但我想要的是透明度。
感谢帮助。
在这篇博客中找到了答案,但为了保存起见,在此重复一遍。
更新:我将这两个脚本上传到GitHub,并附上了一个漂亮的演示GIF :)
同时进行了一些重构和错误修复。请查看仓库以获取最新的脚本。
#!/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)
#!/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)
settings.json
删除代码-否则,您可以在iTerm2 Scripts文件夹(/Users/{username}/Library/Application Support/iTerm2/Scripts)
中创建一个settings.json文件。{
"pane_transparency": {
"active": 0.2,
"inactive": 0.8,
"blur": false,
"blur_radius": 0
}
}
./AutoLaunch
文件夹中)。#!/usr/bin/env python3.7
import iterm2
import json
import os
# Global settings
SETTINGS_FILE = os.path.join(os.path.dirname(__file__), '../settings.json')
# Default pane transparency settings (overridden by settings.json)
DEFAULT_TRANSPARENCY_ACTIVE = 0.25
DEFAULT_TRANSPARENCY_INACTIVE = 0.75
DEFAULT_BLUR = False
DEFAULT_BLUR_RADUIUS = 0
async def load_settings():
"""Load settings from the settings.json file."""
with open(SETTINGS_FILE) as f:
settings = json.load(f)
pane_transparency = settings.get("pane_transparency", {})
transparancy_active = pane_transparency.get("active", DEFAULT_TRANSPARENCY_ACTIVE)
transparancy_active = transparancy_active if (0 <= transparancy_active <= 1) else DEFAULT_TRANSPARENCY_ACTIVE
transparency_inactive = pane_transparency.get("inactive", DEFAULT_TRANSPARENCY_INACTIVE)
transparency_inactive = transparency_inactive if (0 <= transparency_inactive <= 1) else DEFAULT_TRANSPARENCY_INACTIVE
blur = pane_transparency.get("blur", DEFAULT_BLUR)
blur = blur if blur == True or False else DEFAULT_BLUR
blur_radius = pane_transparency.get("blur_radius", DEFAULT_BLUR_RADUIUS)
blur_radius = blur_radius if (isinstance(blur_radius, int) and 0 <= blur_radius <= 30) else DEFAULT_BLUR_RADUIUS
return transparancy_active, transparency_inactive, blur, blur_radius
async def transparancy_update(
app,
active_session,
transparency_active,
transparency_inactive,
blur,
blur_radius):
"""Updating the transparency for the active and all inactive panes."""
inactive_change = iterm2.LocalWriteOnlyProfile()
inactive_change.set_transparency(transparency_inactive)
active_change = iterm2.LocalWriteOnlyProfile()
active_change.set_transparency(transparency_active)
active_change.set_blur(blur)
active_change.set_blur_radius(blur_radius)
# Updates all inactive panes
await transparancy_update_inactive(app, active_session, inactive_change)
# Updates the the active pane
await active_session.async_set_profile_properties(active_change)
async def transparancy_update_inactive(app, active_session, inactive_change):
"""Updating the transparency for all inactive panes."""
# Looping over all inactive panes and changing transparency
for window in app.terminal_windows:
for tab in window.tabs:
for session in tab.all_sessions:
if session != active_session:
await session.async_set_profile_properties(inactive_change)
async def main(connection):
"""Pane transparency main method"""
app = await iterm2.async_get_app(connection)
transparency_active, transparency_inactive, blur, blur_radius = await load_settings()
async with iterm2.FocusMonitor(connection) as mon:
active_session = app.current_terminal_window.current_tab.current_session
while True:
update = await mon.async_get_next_update()
if update.active_session_changed:
active_session = app.get_session_by_id(update.active_session_changed.session_id)
if update.active_session_changed or update.selected_tab_changed:
await transparancy_update(
app,
active_session,
transparency_active,
transparency_inactive,
blur,
blur_radius)
iterm2.run_forever(main)