twblue/src/mysc/autostart.py

45 lines
1.6 KiB
Python
Raw Normal View History

2019-06-06 11:52:23 -05:00
from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
from builtins import str
import winreg
import os
import sys
from platform_utils import paths
2019-06-06 11:52:23 -05:00
RUN_REGKEY = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
def is_installed(app_subkey):
"""Checks if the currently running copy is installed or portable variant. Requires the name of the application subkey found under the uninstall section in Windows registry."""
try:
2019-06-06 11:52:23 -05:00
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s" % app_subkey)
inst_dir = winreg.QueryValueEx(key,"InstallLocation")[0]
except WindowsError:
return False
2019-06-06 11:52:23 -05:00
winreg.CloseKey(key)
try:
return os.stat(inst_dir) == os.stat(paths.app_path())
except WindowsError:
return False
def getAutoStart(app_name):
"""Queries if the automatic startup should be set for the application or not, depending on it's current state."""
try:
2019-06-06 11:52:23 -05:00
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_REGKEY)
val = winreg.QueryValueEx(key, str(app_name))[0]
return os.stat(val) == os.stat(sys.argv[0])
except (WindowsError, OSError):
return False
def setAutoStart(app_name, enable=True):
"""Configures automatic startup for the application, if the enable argument is set to True. If set to False, deletes the application AutoStart value."""
if getAutoStart(app_name) == enable:
return
2019-06-06 11:52:23 -05:00
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_REGKEY, 0, winreg.KEY_WRITE)
if enable:
2019-06-06 11:52:23 -05:00
winreg.SetValueEx(key, str(app_name), None, winreg.REG_SZ, paths.get_executable())
else:
2019-06-06 11:52:23 -05:00
winreg.DeleteValue(key, str(app_name))