Spinner Software logo Home News Products Buy online Solutions Contact us
Sitemap About Download Support Services Feedback

How to switch between two windows continously

The following is an example Vbscript file that can be run on a Windows 2000 / XP computer. It will continously switch between two windows (applications) with a given interval.

You need two files: a small command file that runs the proper command line, and the actual script. The command file makes it easy to schedule the action, or start it after you log into Microsoft Windows.

Create the following text file named runscript.cmd:

@ECHO OFF
@ECHO.
@ECHO Running script to switch between two windows
@ECHO.

CScript //nologo switch.vbs

@ECHO.
@ECHO Script completed.
@ECHO.

Create the following text file named switch.vbs:

' Define the titles to look for
gTitle1		= " - Notepad"
gTitle2		= "Computer Management"
gSecondsToWait	= 60

' If one of the window closes, this variable becomes False and the script exits.
Dim bWindowFound

' Create scripting object
Dim WShell, lRunUninstall
Set WShell = CreateObject("WScript.Shell")

Do
	' Activate the window that has a title matching, starting, or ending with gTitle1.
	bWindowFound = WShell.AppActivate(gTitle1)

	' Check if we activated the window ?
	If(bWindowFound) Then

		' Send ALT+SPACEBAR (system menu) M (move) ESCAPE (cancel) to activate window.
		WShell.SendKeys "% M{ESCAPE}"

		' Wait 60 seconds
		WScript.Sleep (gSecondsToWait * 1000)

		' You can either send ALT+TAB and then wait 60 seconds, or activate the other window
		'WShell.SendKeys "%{TAB}"

		' Switch to the other window
		bWindowFound = WShell.AppActivate(gTitle2)
		If(bWindowFound) Then

			' Send ALT+SPACEBAR (system menu) M (move) ESCAPE (cancel) to activate window.
			WShell.SendKeys "% M{ESCAPE}"

			' Wait 60 seconds
			WScript.Sleep (gSecondsToWait * 1000)
		End If
	End If

Loop Until Not bWindowFound

' Completed. Stop the script
WScript.Echo "Quitting script"
Set WShell = Nothing
WScript.quit(0)

--------------------------------------------------------------------------------

(c) 2004 Nicolai Kjaer, Spinner Software B.V.

http://www.spinnersoftware.com


Copyright 1997-2004 Spinner Software BV (www.spinnersoftware.com).

Home | Products | Buy Online | Privacy statement | Price policy | About Spinner | Contact Us | Search

This page is fully HTML 4.01 and CSS compliant. Please email the webmaster if you encounter any problems with this site.