Had an idea to use task scheduler. Perhaps not very elegant, but does the job. As it turns out, most of the work was already done for me by Shajul in
this wonderful post.
Code:
RunAsStd(Target) {
; http://www.autohotkey.com/forum/topic65768.html
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 := "System Task"
regInfo.Author := "System"
;********************************************************
; 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 += 1, Seconds ;start 1s from now
FormatTime,startTime,%startTime%,yyyy-MM-ddTHH`:mm`:ss
endTime += 2, Seconds ;end time = 2s from now
FormatTime,endTime,%endTime%,yyyy-MM-ddTHH`:mm`:ss
trigger.StartBoundary := startTime
trigger.EndBoundary := endTime
trigger.ExecutionTimeLimit := "PT1M" ; 1 minute
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 := Target
;***********************************************************
; Register (create) the task.
rootFolder.RegisterTaskDefinition("System Task", taskDefinition, TaskCreateOrUpdate ,"","", 3)
}