Creating a Schedule Task with AHK Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Reynbow
Posts: 10
Joined: 27 Sep 2017, 22:30

Creating a Schedule Task with AHK

27 Sep 2017, 22:42

I have a script that helps me create scheduled tasks with my specifications very quickly. I use it for many machines that I need tasks set up on, makes things significantly faster.
However, I need some settings changed and I'm struggling to figure out how to make it happen.
Here's what I currently have, it's set up to make a scheduled task to run Notepad at a time you choose at the start.
I did not write this code, I barely understand it. I have modified it for my purposes and commented out anything I understand I don't need.

Code: Select all

InputBox, count, SCHEDULE TASK,Please set the time you would like SCHEDULE TASK to start.`nFormat is '12:00' in 24 hour time., ,240,160,200,200,,,22:30

;=========== THIS BLOCK WILL SET THE SCHEDULE TASK ==========

TriggerType = 2    ; 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 := "Task Description"
regInfo.Author := "Author Name"

;********************************************************
; 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
;settings.DeleteExpiredTaskAfter := "P1D"

;********************************************************
; 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

startTime = 2017-01-01T%count%:00+10:00
;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 - Schedule", taskDefinition, TaskCreateOrUpdate ,"","", 3)

;========== END OF SCHEDULE TASK SECTION ===========
The schedule task is made fine, however there are two boxes in the scheduled task I want unticked.

Here: https://i.imgur.com/8Gzf4EH.png
And here: https://i.imgur.com/D0Y6Pfn.png

I've been trying to make heads and tails of these posts here: https://autohotkey.com/board/topic/6104 ... ely-ahk-l/
and here: https://github.com/halpo/NppToR/blob/ma ... eduler.ahk

Which may have my answer in there, but I don't understand it enough to implement it.
If anyone here can help me that would be excellent.
Thanks.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Creating a Schedule Task with AHK  Topic is solved

27 Sep 2017, 23:00

Reynbow wrote:The schedule task is made fine, however there are two boxes in the scheduled task I want unticked.
Remove the line ;trigger.ExecutionTimeLimit := "PT5M" ;Five minutes
Add settings.ExecutionTimeLimit := "PT0S" underneath settings.Hidden := False
Reynbow
Posts: 10
Joined: 27 Sep 2017, 22:30

Re: Creating a Schedule Task with AHK

27 Sep 2017, 23:15

qwerty12 wrote:
Reynbow wrote:The schedule task is made fine, however there are two boxes in the scheduled task I want unticked.
Remove the line ;trigger.ExecutionTimeLimit := "PT5M" ;Five minutes
Add settings.ExecutionTimeLimit := "PT0S" underneath settings.Hidden := False
Wow! Amazing. Thank you so much!

Is there somewhere that explains all the different types of things I can change? I've been google searching for the past couple days and can't really find anything that makes sense to me.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Creating a Schedule Task with AHK

27 Sep 2017, 23:24

No problem. I used Task Scheduler Scripting Objects as my reference.

To actually find what the relevant settings were, I used the Task Scheduler GUI itself to export the task as an XML file, I then made my changes, exported the task again to another file and used a file comparison tool. For the most part, the script object names match up pretty well with the names of the XML nodes.

P.S. I didn't realise it when posting, but when I untick those two options from the TS GUI, the following additional settings get removed too. I don't know if it makes any difference, but if you want your script to not also add these settings in, then you can add

Code: Select all

idlesettings := settings.IdleSettings
idlesettings.WaitTimeout := ""
idlesettings.IdleDuration := ""
after settings.ExecutionTimeLimit := "PT0S"
User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Creating a Schedule Task with AHK

27 Sep 2017, 23:48

Hi qwerty12,
I've been following this thread with great interest and I'm hoping you can explain something in the Task Scheduler GUI. As Reynbow showed in the two screenshots, when creating a Trigger in the Triggers tab, it has an "Advanced settings" section that contains a check-box that says "Stop task if it runs longer than:", and in the Settings tab there's a check-box that says "Stop the task if it runs longer than:". Do you know the difference between these two parameters? If they have different values, which one takes precedence? Thanks, Joe
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Creating a Schedule Task with AHK

28 Sep 2017, 07:56

Hi JoeWinograd,
JoeWinograd wrote:Do you know the difference between these two parameters? If they have different values, which one takes precedence?
Honestly speaking, I haven't a clue, sorry. With a task where the "stop task" setting from the Settings tab, as per Reynbow's 2nd screenshot, was set to 1 minute and a timed trigger where its "stop task" setting was set for two minutes, my task was still ended after one minute. So I assumed that the task stop time has higher priority.

I then tried to see what would happen if the trigger stop task time was shorter than the task's task stop time, thinking that that particular case would have given the trigger's limit priority, but when I set the task's task stop time to 2 minutes and the stop task time on the trigger itself to one minute, the task still ended itself after two minutes, despite being started from that very trigger.
When I disabled the task's task stop time but left a stop time enabled on the trigger, the task wasn't ended at all. I don't know if this is a peculiarity of my system. My Google-skills aren't great, because I couldn't find anything official, just the following two links which may or may not answer your question:

https://superuser.com/questions/506662/ ... inside-tri
https://serverfault.com/questions/76275 ... t-too-soon

Best regards

EDIT:
JoeWinograd wrote:Hi qwerty12,
Thanks very much for taking the time to do that web research and run the experiments — I really appreciate it. Your results are extremely interesting — and not what I expected. I think it's unlikely that it's a peculiarity of your system. I'll run some similar tests and will post back here with the results. Regards, Joe
Hi JoeWinograd,

No problem, and thank you. I'm sorry that I wasn't able to find a definitive answer.

Best regards
Last edited by qwerty12 on 29 Sep 2017, 09:53, edited 1 time in total.
User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Creating a Schedule Task with AHK

28 Sep 2017, 12:55

Hi qwerty12,
Thanks very much for taking the time to do that web research and run the experiments — I really appreciate it. Your results are extremely interesting — and not what I expected. I think it's unlikely that it's a peculiarity of your system. I'll run some similar tests and will post back here with the results. Regards, Joe

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mstrauss2021, Spawnova, william_ahk and 359 guests