Skip to content

Commit

Permalink
Add autorun on startup (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexIII committed Oct 28, 2023
1 parent 5771a04 commit b93cfa6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
4 changes: 2 additions & 2 deletions make-release.bat
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@echo off

echo Compiling Portable version...
pyinstaller --icon=icons/gaugeIcon.ico --add-data icons/gaugeIcon.png;icons -w -F -y src/tcc-g15.py || goto :error
pyinstaller --icon=icons/gaugeIcon.ico --add-data icons/gaugeIcon.png;icons --add-data tcc_g15_task.xml;. -w -F -y src/tcc-g15.py || goto :error

echo Compiling version for installer...
pyinstaller --icon=icons/gaugeIcon.ico --add-data icons/gaugeIcon.png;icons -w -y src/tcc-g15.py || goto :error
pyinstaller --icon=icons/gaugeIcon.ico --add-data icons/gaugeIcon.png;icons --add-data tcc_g15_task.xml;. -w -y src/tcc-g15.py || goto :error

echo Compiling installer...
"C:\Program Files (x86)\Inno Setup 6\iscc.exe" installer-inno-config.iss || goto :error
Expand Down
21 changes: 18 additions & 3 deletions src/GUI/AppGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@
def resourcePath(relativePath: str = '.'):
return os.path.join(sys._MEIPASS if hasattr(sys, '_MEIPASS') else os.path.abspath('.'), relativePath)

def alert(title: str, message: str, message2: str = None, type: QtWidgets.QMessageBox.Icon = QtWidgets.QMessageBox.Icon.Information) -> None:
def autorunTask(action: Literal['add', 'remove']):
addCmd = f'schtasks /create /xml "{resourcePath("tcc_g15_task.xml")}" /tn "TCC_G15"'
removeCmd = 'schtasks /delete /tn "TCC_G15" /f'
cmd = addCmd if action == 'add' else removeCmd
res = os.system(cmd)
if res != 0:
print(f'Failed to {action} autorun task. Error: {res}. Command: {cmd}')
alert("Error", f"Failed to {action} autorun task (maybe it's already been done before)", QtWidgets.QMessageBox.Icon.Critical)
else:
alert("Success", f"Autorun on system startup {'Enabled' if action == 'add' else 'Disabled'}")

def alert(title: str, message: str, type: QtWidgets.QMessageBox.Icon = QtWidgets.QMessageBox.Icon.Information, *, message2: str = None) -> None:
msg = QtWidgets.QMessageBox()
msg.setWindowIcon(QtGui.QIcon(resourcePath(GUI_ICON)))
msg.setIcon(type)
Expand Down Expand Up @@ -49,7 +60,7 @@ class SettingsKey(Enum):
def errorExit(message: str, message2: str = None) -> None:
if not QtWidgets.QApplication.instance():
QtWidgets.QApplication([])
alert("Oh-oh", message, message2, QtWidgets.QMessageBox.Icon.Critical)
alert("Oh-oh", message, QtWidgets.QMessageBox.Icon.Critical, message2 = message2)
sys.exit(1)

class TCC_GUI(QtWidgets.QWidget):
Expand Down Expand Up @@ -83,7 +94,7 @@ def __init__(self, awcc: AWCCThermal):
self.setWindowIcon(QtGui.QIcon(resourcePath(GUI_ICON)))
self.mouseReleaseEvent = lambda evt: (
evt.button() == QtCore.Qt.RightButton and
alert("About", f"{self.APP_NAME} v{self.APP_VERSION}", f"{self.APP_DESCRIPTION}\n{self.APP_URL}")
alert("About", f"{self.APP_NAME} v{self.APP_VERSION}", message2 = f"{self.APP_DESCRIPTION}\n{self.APP_URL}")
)

# Set up tray icon
Expand All @@ -93,6 +104,10 @@ def __init__(self, awcc: AWCCThermal):
showAction.triggered.connect(self.showNormal)
exitAction = menu.addAction("Exit")
exitAction.triggered.connect(self.close)
addToAutorunAction = menu.addAction("Enable autorun")
addToAutorunAction.triggered.connect(lambda: autorunTask('add'))
removeFromAutorunAction = menu.addAction("Disable autorun")
removeFromAutorunAction.triggered.connect(lambda: autorunTask('remove'))
tray = QtWidgets.QSystemTrayIcon(self)
tray.setIcon(trayIcon)
tray.setContextMenu(menu)
Expand Down
45 changes: 45 additions & 0 deletions tcc_g15_task.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2023-06-10T21:33:33</Date>
<Description>Autorun for Thermal Control Center for Dell G15 | github.com/AlexIII/tcc-g15</Description>
<URI>\TCC_G15</URI>
</RegistrationInfo>
<Triggers>
<LogonTrigger>
<StartBoundary>2023-06-10T21:33:00</StartBoundary>
<Enabled>true</Enabled>
</LogonTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"C:\Program Files (x86)\Thermal Control Center\tcc-g15.exe"</Command>
<Arguments>--minimized</Arguments>
</Exec>
</Actions>
</Task>

2 comments on commit b93cfa6

@T7imal
Copy link

@T7imal T7imal commented on b93cfa6 Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a long time I didn't know how to set the program minimized in Task Scheduler. It turns out I got the arguments wrong...

I think I have tried /min /background -minimized -silent or more, and I didn't know it's --minimized...

@AlexIII
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a long time I didn't know how to set the program minimized in Task Scheduler. It turns out I got the arguments wrong...

I think I have tried /min /background -minimized -silent or more, and I didn't know it's --minimized...

--minimized did not exist before 1.5.0. I just added it...

Please sign in to comment.