On 1/5/23, Joel Moberg <joel.mob...@gmail.com> wrote:
> Maybe I can adjust the flag in the Trigger object but I have no idea what
> flags are available.

The following loosely adapts Microsoft's time trigger task example,
with commented links to get further information. The property that
directly addresses your question is
taskDefinition.Settings.StartWhenAvailable.

---

import win32com.client
from pywintypes import com_error
from datetime import datetime, timedelta

# Connect a TaskService instance.
# https://learn.microsoft.com/windows/win32/taskschd/taskservice

service = win32com.client.Dispatch('Schedule.Service')
service.Connect()

# Create a TaskDefinition.
# https://learn.microsoft.com/windows/win32/taskschd/taskdefinition

taskDefinition = service.NewTask(0)

# Set the task description and author name.
# https://learn.microsoft.com/windows/win32/taskschd/registrationinfo

taskDefinition.RegistrationInfo.Description = (
    "Start notepad at a certain time")
taskDefinition.RegistrationInfo.Author = "Author Name"

# Enable the task to start after the scheduled time.
# https://learn.microsoft.com/windows/win32/taskschd/tasksettings

taskDefinition.Settings.Enabled = True
taskDefinition.Settings.StartWhenAvailable = True

# Add a TimeTrigger.
# https://learn.microsoft.com/windows/win32/taskschd/triggercollection
# https://learn.microsoft.com/windows/win32/taskschd/timetrigger

TASK_TRIGGER_TIME = 1
# The time format is "YYYY-MM-DD" "T" "HH:MM:SS" [(+-)"HH:MM"]. For
# example, "1980-01-01T00:00:00+00:00". Local time is used if the
# optional UTC offset is omitted.
TIME_TEMPLATE = "{:%Y-%m-%dT%H:%M:%S}"

trigger = taskDefinition.Triggers.Create(TASK_TRIGGER_TIME)
trigger.Id = "TimeTriggerId"
trigger.Enabled = True
trigger.StartBoundary = TIME_TEMPLATE.format(
                            datetime.now() + timedelta(seconds=30))
trigger.EndBoundary = TIME_TEMPLATE.format(
                            datetime.now() + timedelta(minutes=5))
trigger.ExecutionTimeLimit = "PT5M" # 5 minutes

# Add an ExecAction.
# https://learn.microsoft.com/windows/win32/taskschd/actioncollection
# https://learn.microsoft.com/windows/win32/taskschd/execaction

TASK_ACTION_EXEC = 0

action = taskDefinition.Actions.Create(TASK_ACTION_EXEC)
action.Id = "ExecActionid"
action.Path = r"C:\Windows\System32\notepad.exe"

# Register the task to run only in the user's interactive session.
# https://learn.microsoft.com/windows/win32/taskschd/taskfolder

TASK_CREATE_OR_UPDATE = 6
TASK_LOGON_PASSWORD = 1
TASK_LOGON_S4U = 2 # requires admin access
TASK_LOGON_INTERACTIVE_TOKEN = 3
TASK_LOGON_GROUP = 4
TASK_LOGON_SERVICE_ACCOUNT = 5

service.GetFolder("\\").RegisterTaskDefinition(
    "Time Trigger Test", taskDefinition, TASK_CREATE_OR_UPDATE,
    None, None, TASK_LOGON_INTERACTIVE_TOKEN)
_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to