vscode 更换背景图片的 python 脚本

vscode 更换背景图片的 python 脚本

vscode 可以通过插件来实现更换背景图片,但是并不好看

还是要通过修改软件本身才能达到美观的目的

vscode 背景样式css文件的路径

win:盘符:\Users\用户名\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\workbench.desktop.main.css

linux:/usr/share/code/resources/app/out/vs/workbench\workbench.desktop.main.css

修改的样式

好几个版本了,都没啥变动,就是workbench.desktop.main.css文件中的第5个body{(一共有14个)

如果workbench.desktop.main.css文件结构变了,代码是要重写的

因为权限的一些问题,需要把要替换的图片放到与workbench.desktop.main.css同级目录下

body {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    font-size: 11px;
    user-select: none;
    pointer-events: auto !important;
    background-size: 100% !important;
    opacity: 0.8 !important;
    background-position: 0 0 !important;background-image: url('xxx.jpg') !important;content: '';
    position:absolute;
    z-index:99999;
    width:100%;
    background-repeat: no-repeat;
}

这是为了方便展示,保存的话,还是建议缩进成1行

python脚本替换vscode背景

直接看代码吧,不是很复杂的脚本

路径写死了,可以替换,可以传参

暂且命名为 change_backgroud.py

如果workbench.desktop.main.css文件结构变了,代码是要重写的

import os
import re
import sys
import shutil
from pathlib import Path

CSSPATH = Path(r"/usr/share/code/resources/app/out/vs/workbench" if os.name == "posix" else \
    r"D:\Users\username\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench")


def write_temp(change: str) -> None:
    """根据当前的 workbench.desktop.main.css 文件编写临时的 修改文件,以便不污染源文件

    Args:
        change (str): 修改的部分
    """
    with open(CSSPATH / "temp", "w", encoding="utf-8") as temp:
        is_have_pic = True

        with open(CSSPATH / "workbench.desktop.main.css", "r", encoding="utf-8") as pr:
            # workbench.desktop.main.css 前两行是copyright
            temp.writelines(pr.readline())
            temp.writelines(pr.readline())

            s = pr.readline()
            _math = re.findall(r"body{.*?}", s)
            _sl = re.split(r"body{.*?}", s)

            for i in range(len(_sl)):
                temp.write(_sl[i])
                if i == 4:
                    temp.write(change)
                    is_have_pic = not not re.search("background-image", _math[i])
                elif i < len(_math):
                    temp.write(_math[i])

    if not is_have_pic:
        # 保存一下最原始的 workbench.desktop.main.css 文件
        with open(CSSPATH / "workbench.desktop.main.css.bak", "w", encoding="utf-8") as bak:
            for s in open(CSSPATH / "workbench.desktop.main.css", "r", encoding="utf-8"):
                bak.write(s)


if __name__ == '__main__':
    pic_path = input() if len(sys.argv) == 1 else sys.argv[1]
    cp_path = "1.png" if pic_path.endswith("png") else "1.jpg"

    shutil.copyfile(pic_path, CSSPATH / cp_path)

    change = "".join(("body{height:100%;width:100%;margin:0;padding:0;overflow:hidden;" \
                      "font-size:11px;user-select:none;pointer-events:auto !important;" \
                      "background-size:100% !important;opacity: 0.8 !important;" \
                      "background-position: 0 0 !important;background-image: url('",
                      cp_path,
                      "') !important;content:'';position:absolute;z-index:99999;" \
                      "width:100%;background-repeat:no-repeat;}"))

    write_temp(change)
    os.remove(CSSPATH / "workbench.desktop.main.css")
    shutil.move(CSSPATH / "temp", CSSPATH / "workbench.desktop.main.css")

posted @ 2022-05-09 18:55  洛绫璃  阅读(104)  评论(1编辑  收藏  举报