install_shortcut.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. 创建桌面快捷方式并添加到 Windows 开机自启。
  3. 用法:
  4. python tools/install_shortcut.py # 自动查找 dist 下的 exe
  5. python tools/install_shortcut.py --exe "C:\path\to\HookVision.exe"
  6. python tools/install_shortcut.py --remove # 移除开机自启和桌面快捷方式
  7. """
  8. import os, sys, argparse
  9. from win32com.client import Dispatch
  10. APPTITLE = "HookVision"
  11. def _app_name():
  12. try:
  13. import configparser
  14. base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  15. for d in (base, os.path.join(base, "dist", APPTITLE)):
  16. p = os.path.join(d, "config.ini")
  17. if os.path.exists(p):
  18. cfg = configparser.ConfigParser()
  19. cfg.read(p, encoding='utf-8')
  20. return cfg.get("App", "name", fallback=APPTITLE)
  21. except Exception:
  22. pass
  23. return APPTITLE
  24. def _desktop():
  25. return os.path.join(os.path.expanduser("~"), "Desktop")
  26. def find_exe():
  27. base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  28. p = os.path.join(base, "dist", APPTITLE, f"{APPTITLE}.exe")
  29. if os.path.exists(p):
  30. return p
  31. for root, dirs, files in os.walk(base):
  32. for f in files:
  33. if f.lower() == f"{APPTITLE.lower()}.exe":
  34. return os.path.join(root, f)
  35. return None
  36. def create_shortcut(exe_path):
  37. name = _app_name()
  38. lnk_path = os.path.join(_desktop(), f"{name}.lnk")
  39. shell = Dispatch('WScript.Shell')
  40. shortcut = shell.CreateShortCut(lnk_path)
  41. shortcut.Targetpath = exe_path
  42. shortcut.WorkingDirectory = os.path.dirname(exe_path)
  43. shortcut.IconLocation = exe_path
  44. shortcut.Description = name
  45. shortcut.Save()
  46. print(f"桌面快捷方式已创建: {lnk_path}")
  47. def add_autostart(exe_path):
  48. name = _app_name()
  49. key_path = r"Software\Microsoft\Windows\CurrentVersion\Run"
  50. import winreg
  51. try:
  52. key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path, 0, winreg.KEY_SET_VALUE)
  53. winreg.SetValueEx(key, name, 0, winreg.REG_SZ, exe_path)
  54. winreg.CloseKey(key)
  55. print(f"开机自启已添加 (HKCU\\{key_path}\\{name})")
  56. except Exception as e:
  57. print(f"添加开机自启失败: {e}")
  58. def remove_autostart():
  59. name = _app_name()
  60. key_path = r"Software\Microsoft\Windows\CurrentVersion\Run"
  61. import winreg
  62. try:
  63. key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path, 0, winreg.KEY_SET_VALUE)
  64. winreg.DeleteValue(key, name)
  65. winreg.CloseKey(key)
  66. print("开机自启已移除")
  67. except FileNotFoundError:
  68. print("开机自启未设置")
  69. except Exception as e:
  70. print(f"移除开机自启失败: {e}")
  71. def remove_shortcut():
  72. name = _app_name()
  73. lnk_path = os.path.join(_desktop(), f"{name}.lnk")
  74. if os.path.exists(lnk_path):
  75. os.remove(lnk_path)
  76. print(f"桌面快捷方式已移除: {lnk_path}")
  77. else:
  78. print("桌面快捷方式不存在")
  79. def main():
  80. parser = argparse.ArgumentParser(description="安装/卸载桌面快捷方式和开机自启")
  81. parser.add_argument("--exe", help="指定 exe 路径")
  82. parser.add_argument("--remove", action="store_true", help="移除快捷方式和开机自启")
  83. args = parser.parse_args()
  84. if args.remove:
  85. remove_autostart()
  86. remove_shortcut()
  87. return
  88. exe_path = args.exe or find_exe()
  89. if not exe_path:
  90. print("未找到 exe,请先运行 build_exe.bat 或指定 --exe 路径")
  91. sys.exit(1)
  92. exe_path = os.path.abspath(exe_path)
  93. try:
  94. create_shortcut(exe_path)
  95. add_autostart(exe_path)
  96. print("安装完成")
  97. except Exception as e:
  98. print(f"安装失败: {e}")
  99. sys.exit(1)
  100. if __name__ == '__main__':
  101. main()