[AHK]调用计划任务组件(Schedule.Service)

189 篇文章 4 订阅
92 篇文章 3 订阅
;~ Create a scheduled task natively [AHK_L]
;~ http://www.autohotkey.com/forum/topic65768.html

;~ Examples of creating a scheduled task using the Schedule.Service COM class. 
;~ Requires: Windows Vista/7/Server 2008 


;------------------------------------------------------------------
; This sample schedules a task to start notepad.exe 30 seconds
; from the time the task is registered.
; Requires AutoHotkey_L
;------------------------------------------------------------------

TriggerType = 1    ; specifies a time-based trigger.
ActionTypeExec = 0    ; specifies an executable action.
LogonType = 3   ; Set the logon type to interactive logon
TaskCreateOrUpdate = 6

;********************************************************
; Create the TaskService object.
service := ComObjCreate("Schedule.Service")
service.Connect()

;********************************************************
; Get a folder to create a task definition in.
rootFolder := service.GetFolder("\")

; The taskDefinition variable is the TaskDefinition object.
; The flags parameter is 0 because it is not supported.
taskDefinition := service.NewTask(0)

;********************************************************
; Define information about the task.

; Set the registration info for the task by
; creating the RegistrationInfo object.
regInfo := taskDefinition.RegistrationInfo
regInfo.Description := "Start notepad at a certain time"
regInfo.Author := "shajul"

;********************************************************
; Set the principal for the task
principal := taskDefinition.Principal
principal.LogonType := LogonType  ; Set the logon type to interactive logon


; Set the task setting info for the Task Scheduler by
; creating a TaskSettings object.
settings := taskDefinition.Settings
settings.Enabled := True
settings.StartWhenAvailable := True
settings.Hidden := False

;********************************************************
; Create a time-based trigger.
triggers := taskDefinition.Triggers
trigger := triggers.Create(TriggerType)

; Trigger variables that define when the trigger is active.
startTime += 30, Seconds  ;start time = 30 seconds from now
FormatTime,startTime,%startTime%,yyyy-MM-ddTHH`:mm`:ss

endTime += 5, Minutes  ;end time = 5 minutes from now
FormatTime,endTime,%endTime%,yyyy-MM-ddTHH`:mm`:ss

trigger.StartBoundary := startTime
trigger.EndBoundary := endTime
trigger.ExecutionTimeLimit := "PT5M"    ;Five minutes
trigger.Id := "TimeTriggerId"
trigger.Enabled := True

;***********************************************************
; Create the action for the task to execute.

; Add an action to the task to run notepad.exe.
Action := taskDefinition.Actions.Create( ActionTypeExec )
Action.Path := "C:\Windows\System32\notepad.exe"

;***********************************************************
; Register (create) the task.
rootFolder.RegisterTaskDefinition("Test TimeTrigger", taskDefinition, TaskCreateOrUpdate ,"","", 3)

MsgBox % "Task submitted.`nstartTime :" . startTime . "`nendTime :" . endTime

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值