Python Forum
help to boot application on computer startup - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: help to boot application on computer startup (/thread-43913.html)



help to boot application on computer startup - jacksfrustration - Jan-21-2025

ok so i already googled it and found this article
i followed the steps but it still didn't work. i copied the application and csv file into the startup folder. i created a python script file with the code in the article and copied the path into the HKEY registry. Still no luck. Please help


RE: help to boot application on computer startup - deanhystad - Jan-21-2025

Please show what you have tried.


RE: help to boot application on computer startup - DeaD_EyE - Jan-21-2025

The example has some errors.
The registry path doesn't use raw string, which is necessary to prevent accidental escaping.

Instead of using os.path...., use pathlib.Path. I don't know why someone wrote 2021 a tutorial with code which was outdated for years since 2021.

You don't need to close the registry. OpenKey returns an object, which has support for context manager.

The naming was not Pythonic. Use PascalCase for classes. For functions lower case and word split by _.

autostart.py
from winreg import HKEY_CURRENT_USER, OpenKey, KEY_ALL_ACCESS, SetValueEx, REG_SZ
from pathlib import Path


def add_to_registry(name : str, script: Path | str):
    script = Path(script)
    quoted_script = f'"py.exe {script}"'
    
    if not script.exists():
        raise ValueError(f"Script '{script}' does not exist")

    # use raw string to avoid unwanted escaping
    # e.g. \r, \n, \b, \t are Escape Sequences interpreted
    sub_key = r"Software\Microsoft\Windows\CurrentVersion\Run"

    # OpenKey returns winreg.PyHKEY object
    # which supports context manager
    # it closes the registry before leaving the
    # block
    with OpenKey(
        key=HKEY_CURRENT_USER,
        sub_key=sub_key,
        access=KEY_ALL_ACCESS,
    ) as registry:
        SetValueEx(registry, name,0, REG_SZ, quoted_script)
 

if __name__=="__main__":
    add_to_registry("TEST", Path.home().joinpath("AppData", "Local", "script.py"))
The test script:
Path: C:\Users\USERNAME\AppData\Local\script.py
from datetime import datetime as DateTime
from pathlib import Path

with Path.home().joinpath("Desktop", "time.txt").open("w") as fd:
    fd.write(DateTime.now().isoformat())
    fd.write("\n")
This creates a file named time.txt on your Desktop.
If you want to hide the window during running the code, further investigation is required.
I don't use Widows, so my knowledge is limited.


RE: help to boot application on computer startup - chaebol2th - Jan-22-2025

(Jan-21-2025, 02:12 PM)jacksfrustration Wrote: ok so i already googled it and found this article
i followed the steps but it still didn't work. i copied the application and csv file into the startup folder. i created a python script file with the code in the article and copied the path into the HKEY registry. Still no luck. Please help

Please show what you have tried.