Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
script to Create a Scheduled Task
#1
I need to create a method to create a scheduled task after receiving input from a dialog. I'll have the main macro create a 'temp macro' but i need to schedule that with info from the main dialog. is there a way to do that?

thanks.
An old blog on QM coding and automation.

The Macro Hook
#2
Scheduled tasks can be created using Windows Task Scheduler COM interfaces. I could give the code. However there are several problems. 1. The schedule will not be displayed in the list of macros until you restart QM or reload file or open task properties through macro Properties. 2. On Vista the task will have incorrect security settings, although it still works. Therefore it is better to create scheduled tasks through macro Properties. If it is not acceptable, I'll give the code.
#3
the 'problems' won't matter to me at all and especially since the macros would delete themselves just after running (it's for paging).

if you would supply the code, i would be very grateful.
An old blog on QM coding and automation.

The Macro Hook
#4
Better version: Create scheduled task to run a macro or program

Function ScheduleMacro
Code:
Copy      Help
;/
function $macro $_time [$_date] [$_enddate] [flags] [scheduletype] [daysinterval] [WEEKLY&weekly] [MONTHLYDATE&monthlydate] [MONTHLYDOW&monthlydow] [idleminutes] [idlewatchminutes] ;;scheduletype: 0 once, 1 daily, 2 weekly, 3 monthlydate, 4 monthlydow, 5 idle, 7 logon.  flags: 2 delete task if not scheduled to run again, 4 disabled, 0x10 start only if idle, 0x20 end macro if computer ceases to be idle, 0x40 don't start if on batteries, 0x80 end macro if battery mode begins, 0x200 hidden, 0x800 start again when idle state resumes, 0x1000 wake computer

;Creates scheduled task for a macro.
;If the task already exists, at first deletes it.
;Throws error if the task cannot be created or changed.
;Note: Schedule of the task is not displayed in QM until QM is restarted or file reloaded or task properties dialog is opened through macro Properties dialog.
;Requires QM 2.2.0 or later.


;macro - QM item name or +id. It should be function, but also can be macro or item of other type.
;_time - start time. Examples: "20:00", "00:41".
;_date - start date. Example: "12/31/2007". Default or "": today. Note: date string format depends on your computer locale settings.
;_enddate - end date. Default or "": has no end date.
;flags, scheduletype - see above and below.
;daysinterval - use if scheduletype is 1 (daily). Default or 0: every day.
;weekly - variable that defines weekly schedule. Required if scheduletype is 2 (weekly). Otherwise should be 0.
;monthlydate - variable that defines monthly schedule. Required if scheduletype is 3 (monthlydate). Otherwise should be 0.
;monthlydow - variable that defines monthly schedule. Required if scheduletype is 4 (monthlydow). Otherwise should be 0.
;idleminutes, idlewatchminutes - values specified in task properties Settings tab. Used if scheduletype is 5 or flags includes 0x10.


;WEEKLY, MONTHLYDATE and MONTHLYDOW types, as well as other scheduler types, interfaces, constants, etc are documented in the MSDN library on the internet.

;To set custom 'Run as' field, use global str variable g_sch_runas. Example: str+ g_sch_runas="COMPUTER\USER".

;EXAMPLES
;ScheduleMacro "Macro477" "09:00" ;;run once, today
;ScheduleMacro "Macro477" "15:30" "12/31/2007" ;;run once, at specified date
;ScheduleMacro "Macro477" "00:45" "" "" 2 ;;run once, today, delete when done
;ScheduleMacro "Macro477" "17:00" "" "" 0 1 ;;run every day
;ScheduleMacro "Macro477" "" "" "" 0 5 0 0 0 0 1 ;;run when idle for 1 minute
;WEEKLY w; w.WeeksInterval=1; w.rgfDaysOfTheWeek=TASK_MONDAY|TASK_FRIDAY
;ScheduleMacro "Macro477" "17:00" "" "" 0 2 0 w ;;run every week at monday and friday



str s ss sm; BSTR b; int retry
ITaskScheduler ts._create(CLSID_CTaskScheduler)
ITask task

;create new task
if(macro<0x10000) _i=macro; macro=sm.getmacro(_i 1)
b=s.from("QM - " macro)
;g1
ts.NewWorkItem(b CLSID_CTask IID_ITask &task)
err
,if(retry or _hresult&0xff!=ERROR_FILE_EXISTS) end _error
,retry=1; ts.Delete(b); goto g1

;set properties
b=s.expandpath("$qm$\qmcl.exe"); task.SetApplicationName(b)
b=s.format("T MS ''%s''" macro); task.SetParameters(b)

if(_winnt) flags|TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
task.SetFlags(flags)

if(_winnt)
,str+ g_sch_runas
,if(g_sch_runas.len) b=g_sch_runas
,else GetUserComputer s ss; b=s.from(ss "\" s)
,task.SetAccountInformation(b 0); task.SetCreator(b)

if(idleminutes) task.SetIdleWait(idleminutes iif(idlewatchminutes idlewatchminutes 60))

;set trigger (schedule)
ITaskTrigger trigger; TASK_TRIGGER t
task.CreateTrigger(+&_i &trigger)
trigger.GetTrigger(&t)
t.rgFlags=0
t.TriggerType=scheduletype
sel scheduletype
,case TASK_TIME_TRIGGER_DAILY t.Type.Daily.DaysInterval=iif(daysinterval>0 daysinterval 1)
,case TASK_TIME_TRIGGER_WEEKLY t.Type.Weekly=weekly
,case TASK_TIME_TRIGGER_MONTHLYDATE t.Type.MonthlyDate=monthlydate
,case TASK_TIME_TRIGGER_MONTHLYDOW t.Type.MonthlyDOW=monthlydow

DATE d; SYSTEMTIME st
if(len(_time))
,d=_time; err end "invalid time string"
,d.tosystemtime(st)
,t.wStartHour=st.wHour
,t.wStartMinute=st.wMinute
if(len(_date))
,d=_date; err end "invalid date string"
,d.tosystemtime(st)
,t.wBeginYear=st.wYear
,t.wBeginMonth=st.wMonth
,t.wBeginDay=st.wDay
if(len(_enddate))
,d=_enddate; err end "invalid date string"
,d.tosystemtime(st)
,t.wEndYear=st.wYear
,t.wEndMonth=st.wMonth
,t.wEndDay=st.wDay
,t.rgFlags|TASK_TRIGGER_FLAG_HAS_END_DATE

trigger.SetTrigger(&t)

;save
IPersistFile iFile=+task
iFile.Save(0 1)
ret

err+ end _error
#5
I seem to be getting an odd error on this.
"Error (RT) in Macro: 0x80041310, Unable to establish existence of the account specified."

im not able to go into the the scheduled tasks and run it either. but if i take off the "run only if logged in" and then put in a password, it works fine.

am I doing it wrong?

Code:
Copy      Help
_i=newitem("sched_page" "out ''test''" "Function" "" "" 0)
_s.getmacro(_i 1)
ScheduleMacro _s "15:58" "" "" 0
An old blog on QM coding and automation.

The Macro Hook
#6
Insert out b before task.SetAccountInformation line. Is it COMPUTERNAME\USERNAME?
#7
yes. it is the computer/user name but i think that i need my domain/user name instead (enterprise environment) so i changed ss to be my domain....works great now.

thanks!
An old blog on QM coding and automation.

The Macro Hook
#8
I'm having an issue with deleting the macro after it is triggered by the Scheduled Tasks.
the line
Code:
Copy      Help
newitem(_s "" "Function" "" "Sched Temp" 32)

works when i trigger it from the Editor but when the ST runs it keeps giving this error:
Error (RT) in sched_page: cannot delete item: item exists (in other folder)

Any ideas what i'm doing wrong here?

thanks.
An old blog on QM coding and automation.

The Macro Hook
#9
Check if _s is correct. It must be in "Sched Temp" folder. Works well for me.

Code:
Copy      Help
_s=
;out "test"
;_s.getmacro(getopt(itemid) 1)
;newitem(_s "" "Function" "" "Sched Temp" 32) ;;delete self


_i=newitem("sched_page" _s "Function" "" "Sched Temp" 0)
_s.getmacro(_i 1)
ScheduleMacro _s "16:55" "" ""

or

Code:
Copy      Help
_s=
;out "test"
;newitem(getopt(itemid) "" "Function" "" "Sched Temp" 32) ;;delete self


_i=newitem("sched_page" _s "Function" "" "Sched Temp" 0)
_s.getmacro(_i 1)
ScheduleMacro _s "16:55" "" ""
#10
Fixed bug: the task will be invalid if used item id instead of name.
Added global variable for custom 'Run as'.
#11
it was this line
_s.getmacro(getopt(itemid) 1)

i had it just
_i=qmitem
_s.getmacro(_i 1)

which works in the editor just not from sched....

thanks.
An old blog on QM coding and automation.

The Macro Hook
#12
This is so neat. I needed to be able to schedule tasks, but windows requires a password. The problem is that when you set a password for windows it requires you to use it on the welcome screen! The computer I am going to run a program on won't have a monitor, so if the computer shuts down, it will need to be able to boot up without a password.

This allow me to schedule tasks and not deal with windows!


Thank you Gintaras!

from One very satisfied QM user!
-Jimmy Vig
#13
How do I use this to create a scheduled Task with an exe file?

I'm sure it's easier than scheduling a macro Smile

Thanks,
Jimmy Vig
#14
change

b=s.expandpath("$qm$\qmcl.exe"); task.SetApplicationName(b)
b=s.format("T MS ''%s''" macro); task.SetParameters(b)
#15
Thanks man! Your the best...
#16
Dear Gintaras,

I understand that you may have multiple scheduling schemes, for example run twice a day, with macro properties dialog. I wonder whether you can advise me to improve this macro (ScheduleMacro) to include multiple schedules. Neverthless, I created a task link to the original task to be scheduled, and then I scheduled this task-link independently.

Regards, Simos
#17
Create scheduled task to run a macro or program


Forum Jump:


Users browsing this thread: 1 Guest(s)