| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- """
- 创建桌面快捷方式并添加到 Windows 开机自启。
- 用法:
- python tools/install_shortcut.py # 自动查找 dist 下的 exe
- python tools/install_shortcut.py --exe "C:\path\to\HookVision.exe"
- python tools/install_shortcut.py --remove # 移除开机自启和桌面快捷方式
- """
- import os, sys, argparse
- from win32com.client import Dispatch
- APPTITLE = "HookVision"
- def _app_name():
- try:
- import configparser
- base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
- for d in (base, os.path.join(base, "dist", APPTITLE)):
- p = os.path.join(d, "config.ini")
- if os.path.exists(p):
- cfg = configparser.ConfigParser()
- cfg.read(p, encoding='utf-8')
- return cfg.get("App", "name", fallback=APPTITLE)
- except Exception:
- pass
- return APPTITLE
- def _desktop():
- return os.path.join(os.path.expanduser("~"), "Desktop")
- def find_exe():
- base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
- p = os.path.join(base, "dist", APPTITLE, f"{APPTITLE}.exe")
- if os.path.exists(p):
- return p
- for root, dirs, files in os.walk(base):
- for f in files:
- if f.lower() == f"{APPTITLE.lower()}.exe":
- return os.path.join(root, f)
- return None
- def create_shortcut(exe_path):
- name = _app_name()
- lnk_path = os.path.join(_desktop(), f"{name}.lnk")
- shell = Dispatch('WScript.Shell')
- shortcut = shell.CreateShortCut(lnk_path)
- shortcut.Targetpath = exe_path
- shortcut.WorkingDirectory = os.path.dirname(exe_path)
- shortcut.IconLocation = exe_path
- shortcut.Description = name
- shortcut.Save()
- print(f"桌面快捷方式已创建: {lnk_path}")
- def add_autostart(exe_path):
- name = _app_name()
- key_path = r"Software\Microsoft\Windows\CurrentVersion\Run"
- import winreg
- try:
- key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path, 0, winreg.KEY_SET_VALUE)
- winreg.SetValueEx(key, name, 0, winreg.REG_SZ, exe_path)
- winreg.CloseKey(key)
- print(f"开机自启已添加 (HKCU\\{key_path}\\{name})")
- except Exception as e:
- print(f"添加开机自启失败: {e}")
- def remove_autostart():
- name = _app_name()
- key_path = r"Software\Microsoft\Windows\CurrentVersion\Run"
- import winreg
- try:
- key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path, 0, winreg.KEY_SET_VALUE)
- winreg.DeleteValue(key, name)
- winreg.CloseKey(key)
- print("开机自启已移除")
- except FileNotFoundError:
- print("开机自启未设置")
- except Exception as e:
- print(f"移除开机自启失败: {e}")
- def remove_shortcut():
- name = _app_name()
- lnk_path = os.path.join(_desktop(), f"{name}.lnk")
- if os.path.exists(lnk_path):
- os.remove(lnk_path)
- print(f"桌面快捷方式已移除: {lnk_path}")
- else:
- print("桌面快捷方式不存在")
- def main():
- parser = argparse.ArgumentParser(description="安装/卸载桌面快捷方式和开机自启")
- parser.add_argument("--exe", help="指定 exe 路径")
- parser.add_argument("--remove", action="store_true", help="移除快捷方式和开机自启")
- args = parser.parse_args()
- if args.remove:
- remove_autostart()
- remove_shortcut()
- return
- exe_path = args.exe or find_exe()
- if not exe_path:
- print("未找到 exe,请先运行 build_exe.bat 或指定 --exe 路径")
- sys.exit(1)
- exe_path = os.path.abspath(exe_path)
- try:
- create_shortcut(exe_path)
- add_autostart(exe_path)
- print("安装完成")
- except Exception as e:
- print(f"安装失败: {e}")
- sys.exit(1)
- if __name__ == '__main__':
- main()
|