Spaces:
Running
Running
File size: 2,154 Bytes
10865e1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import time
import pythoncom
import win32api
from win32com.taskscheduler import taskscheduler
test_task_name = "test_addtask_1.job"
ts = pythoncom.CoCreateInstance(
taskscheduler.CLSID_CTaskScheduler,
None,
pythoncom.CLSCTX_INPROC_SERVER,
taskscheduler.IID_ITaskScheduler,
)
tasks = ts.Enum()
for task in tasks:
print(task)
if test_task_name in tasks:
print("Deleting existing task " + test_task_name)
ts.Delete(test_task_name)
new_task = pythoncom.CoCreateInstance(
taskscheduler.CLSID_CTask,
None,
pythoncom.CLSCTX_INPROC_SERVER,
taskscheduler.IID_ITask,
)
ts.AddWorkItem(test_task_name, new_task) ## task object is modified in place
new_task.SetFlags(
taskscheduler.TASK_FLAG_INTERACTIVE | taskscheduler.TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
)
new_task.SetIdleWait(1, 10000)
new_task.SetComment("test task with idle trigger")
new_task.SetApplicationName("c:\\python23\\python.exe")
new_task.SetPriority(taskscheduler.REALTIME_PRIORITY_CLASS)
new_task.SetParameters(
"-c\"import win32ui,time;win32ui.MessageBox('why aint you doing no work ?');\""
)
new_task.SetWorkingDirectory("c:\\python23")
new_task.SetCreator("test_addtask_1.py")
new_task.SetAccountInformation(win32api.GetUserName(), None)
## None is only valid for local system acct or if Flags contain TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
run_time = time.localtime(time.time() + 30)
end_time = time.localtime(time.time() + 60 * 60 * 24)
tr_ind, tr = new_task.CreateTrigger()
tt = tr.GetTrigger()
tt.TriggerType = taskscheduler.TASK_EVENT_TRIGGER_ON_IDLE
tt.Flags = taskscheduler.TASK_TRIGGER_FLAG_HAS_END_DATE
tt.BeginYear = int(time.strftime("%Y", run_time))
tt.BeginMonth = int(time.strftime("%m", run_time))
tt.BeginDay = int(time.strftime("%d", run_time))
tt.StartMinute = int(time.strftime("%M", run_time))
tt.StartHour = int(time.strftime("%H", run_time))
tt.EndYear = int(time.strftime("%Y", end_time))
tt.EndMonth = int(time.strftime("%m", end_time))
tt.EndDay = int(time.strftime("%d", end_time))
tr.SetTrigger(tt)
print(new_task.GetTriggerString(tr_ind))
pf = new_task.QueryInterface(pythoncom.IID_IPersistFile)
pf.Save(None, 1)
|