python更换iterm2背景图片

背景

在看知乎的时候,突然看到了这样的一个视频教程,用python代码更换iterm2的背景。于是我细细的研究一下。视频地址

视频中提到的参考文章地址:

实现过程

我直接把作者的代码粘贴如下,首先需要安装iterm2

pip install iterm2

相关的代码如下:

# -*- encoding: utf-8 -*-
__date__ = '2023/07/27 14:52:52'

'''
https://www.zhihu.com/zvideo/1411103379930705920
'''

import asyncio
import random
import os

import iterm2

base_path = 'xxxx'


async def main(connnection):
    while True:
        images = os.listdir(base_path)
        image_path = os.path.join(base_path, random.choice(images))
        app = await iterm2.async_get_app(connnection)
        session = app.current_terminal_window.current_tab.current_session
        profile = await session.async_get_profile()
        await profile.async_set_background_image_location(image_path)
        
        await asyncio.sleep(3)
        
async def disable_image(connection):
        app = await iterm2.async_get_app(connection)
        session = app.current_terminal_window.current_tab.current_session
        profile = await session.async_get_profile()
        await profile.async_set_background_image_location("")

if __name__ == "__main__":
    iterm2.run_until_complete(main)
    # iterm2.run_until_complete(disable_image)

贴上我的好看的代码截图:

运行

在控制台输入如下命令:

python iterm2_changebg.py

发现出错了

└> python3 iterm2_changebg.py

There was a problem connecting to iTerm2.

Please check the following:
  * Ensure the Python API is enabled in iTerm2's preferences
  * Ensure iTerm2 is running
  * Ensure script is running on the same machine as iTerm2

If you'd prefer to retry connecting automatically instead of
raising an exception, pass retry=true to run_until_complete()
or run_forever().

网上查找了一下,发现我的iterm2的一个配置没有开

至此,脚本可以争取的执行了

最终效果

类似轮播图的效果,动态的更新iterm2的照片背景。

猜你喜欢

转载自blog.csdn.net/weixin_55768452/article/details/131961397
今日推荐