Scheduled Task Running
I like to think that I learn more every day, but I still really embrace being a computer novice in a lot of ways. About four years ago, I learned about using Windows’ Task Scheduler to run processes on a defined timetable. The typical things it became useful for in my daily activities were: backing up data from my local machine to a server, running processes during off-hours that consume significant system resources, and automating web download workflows/preparing the data before I arrived at work. It’s best for uses requiring no user input, since the user is often absent during a scheduled task run.
If you are unfamiliar with this process, there can be fun and simple ways of figuring out how to use the Task Scheduler, and the logic applied will eventually help you automate legitimate work processes. I promise! My typical scheduled task workflow goes as follows: create a python script that runs the process you’d like to complete, create ‘.bat’ file that points to the python script, and schedule a task in the Task Scheduler that runs the ‘.bat’ file at a specified time of day.
Here’s the tutorial idea I came up with to make learning about scheduled tasks a bit more entertaining: Download ten JPEGs of Snoop Dogg (Lion) into a folder directory. Then create a python script within that directory (ChangeWallpaper.py) that loops through the JPEGs and sets the desktop background to a different picture every half a second. Here’s what my simple python script looks like:
import ctypes, time, os jpg_list =  directory = "C:\Users\Zekiah\Desktop\Snoop" for item in os.listdir(directory): if item.endswith('.jpg'): jpg_list.append(directory+"\\"+item) for thing in jpg_list: SPI_SETDESKWALLPAPER = 20 ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, thing , 0) time.sleep(.5)
It essentially loops through the specified desktop directory adding all ‘.jpg’ files to a list. Once done, it loops through that list and changes the desktop wallpaper, waits half a second, then repeats until it has gone through all files in the list.
You can run the ChangeWallpaper.py script stand-alone to make sure it works before moving on to creating the ‘.bat’ file. Creating a ‘.bat’ file to point to the script is as easy as opening Notepad and saving the document as ‘Snoop.bat’ in ‘All Files’ format. Here’s what it looks like:
@echo off C:\Users\Zekiah\Desktop\Snoop\ChangeWallpaper.py EXIT /B
“@echo off” produces no statements when the command window launches and “exit /b” exits the batch file.
If you’d like, from here you can create a new Scheduled Task to call this ‘.bat’ file and assign its trigger properties. That’s often what I do. However, I took it one small step further in this case and instead created a file called ‘Run.vbs’ that calls ‘ChangeWallpaper.py’ and runs it silently, or without allowing the cmd window to pop up. That logic could be useful if you intend to deploy scheduled tasks on each user’s machine in your organization, who may have differing levels of computer literacy. You might not want them to be aware, or see the cmd window, when certain tasks are running if you intend to run them during normal work hours. Here’s the syntax for that:
Set oShell = CreateObject("Wscript.Shell") Dim strArgs strArgs = "cmd /c C:\Users\Zekiah\Desktop\Snoop\ChangeWallpaper.py" oShell.Run strArgs, 0, false
Setting up a scheduled task to execute ‘Run.vbs’ every day at about the same time is as easy as: launching the Task Scheduler, right clicking ‘Task Scheduler Library’ and Selecting ‘Create Task’. Navigate through the tabs in the ‘Properties’ dialog box to set your Trigger (time of day/schedule):
Set your actions:
And that’s pretty much it unless you want to specify some additional advanced parameters. As it sits right now, the Task Scheduler will run ‘Run.vbs’, which runs ‘ChangeWallpaper.py’ every day at the specified time. You can run it on demand within the Task Scheduler as well to confirm its functionality. Hope this logic will help give you some ideas about processes that would be worth automating using the Task Scheduler!
This post was written by Christian.
Contact us for more information on this post or our services.