'---------------------------------------------------------
'Visual Basic Script for WinXP automation
'Aurther: Siong Boon (www.siongboon.com)
'---------------------------------------------------------

introMsg = msgBox ("This program include a list of frequent use function that automate program execution in WinXp",vbOKCancel)
If introMsg = vbCancel Then
	Wscript.Quit 
End If

'Example to open notepad
openNotePad()

'Timer delay in msec
'delay(3000)

'Set the program window containing the keyword in focus.
'focusWindow("firefox")

'Send keyboard key stroke to the OS WinXp
'sendKeyStroke("12345")

'Execute the program
'executeProgram("C:\Program Files\Microchip\MPLAB IDE\Core\MPLAB.exe")

'quit VBscript
Wscript.Quit


'Example to trigger program shortcut.ink or programs with complex parameter. ("" -> means the char '"')
executeProgramRaw("""C:\Program Files\WebCamSplitter Pro\WebCamSplitterPro.exe"" /f ""AV USB 2.0 Pro""")
executeProgram("C:\wamp\wampmanager.exe")
delay(1000)
executeProgramRaw("""C:\Program Files\XID Smart ID\AccessMatching.exe"" ""c:\API_XID_EXE\DBApiXIDWeigandUSBVideo.ini"" ""c:\API_XID_EXE\VerificationXIDWeigandUSBVideo.ini""")
delay(5000)
executeProgramRaw("""C:\Documents and Settings\Administrator\Desktop\XID DEPLOY V7(No restart)\LAUNCHER\4. Abacus_IVPS""")		'launch a shortcut
delay(1000)
executeProgram("C:\Documents and Settings\Administrator\Desktop\XID DEPLOY V7(No restart)\JAVA\xid.bat")
delay(3000)
executeProgram("C:\Documents and Settings\Administrator\Desktop\XID DEPLOY V7(No restart)\LAUNCHER\6. STARhome_XID_UI.swf")
delay(3000)
sendKeyStroke("{Enter}")
delay(500)
sendKeyStroke("^f")


'Example to set map network drive user name and password
'net use \\192.168.234.16\folder mypassword /user:myusername


Function executeProgram(exeName)
	exeName=longToShortProgramName(exeName)	
	executeProgramRaw(exeName)
End Function

Function executeProgramRaw(exeName)
	'example to open the executable program
	Dim shl
	Set shl = CreateObject("Wscript.shell")
	shl.Run exeName
	'Some program shortcut might not work in older version WinXp OS. The following parameter might be required to run shortcut.
	'shl.Run %comspec% /k "shortcut path.ink"
End Function

Function delay(msec)
	WScript.Sleep msec
End Function

Function longToShortProgramName(longName)
	Dim fso
	Set fso=CreateObject("Scripting.FileSystemObject")

	' Is object a file or folder?
	Dim shortName
	shortName=longName
	Dim flag		'check if long to short name operation is successful
	flag=0

	If fso.FolderExists(longName) Then
	   'It's a folder
	   Dim objFolder
	   Set objFolder = fso.GetFolder(longName)
	   shortName=objFolder.ShortPath
	   'rtrn = InputBox("Here's your short path:", "SHORT PATH", objFolder.ShortPath)
	   flag=1
	End If

	If fso.FileExists(longName) Then
	   'It's a file
	   Dim objFile
	   Set objFile = fso.GetFile(longName)
	   shortName=objFile.ShortPath
	   'rtrn = InputBox("Here's your short path:", "SHORT PATH", objFile.ShortPath)
	   flag=1
	End If

	If flag=0 Then
	   print("Fail to convert long to short path name. """ & longName & """")
	End If

	longToShortProgramName=shortName	'return result
End Function

Function openNotePad()
	'example to open a notepad
	Dim WshShell
	Set WshShell = CreateObject("Wscript.shell")
	WshShell.Run("%windir%\notepad.exe")
End Function

Function sendKeyStroke(keyStroke)
	'reference->http://articles.techrepublic.com.com/5100-10878_11-1056137.html
	Dim WshShell
	set WshShell = WScript.CreateObject("WScript.Shell")
	WshShell.SendKeys keyStroke
End Function

Function saveFileKeyStroke()
	sendKeyStroke("^s")	'ctrl+s
	sendKeyStroke("%s")	'alt+s
	sendKeyStroke("+s")	'shift+s
'Other key codes
'{Bksp},{Break},{Capslock},{Del},{Enter} or ~,{Esc},{Help},{Ins},{Numlock},{Scrolllock},{Tab}
'{UP},{Down},{Left},{Right}
'{Home},{End},{Pgdn},{Pgup},{Prtsc}
'{F1},{F2},{F3},{F4},{F5},{F6},{F7},{F8},{F9},{F10},{F11},{F12}
End Function

Function focusWindow(title)	'key in the string (partial or complete) that appears on the window title
	'reference->http://articles.techrepublic.com.com/5100-10878_11-1056137.html
	Dim WshShell
	set WshShell = WScript.CreateObject("WScript.Shell")
	WshShell.AppActivate(title)
End Function

Function print(string)
	WScript.Echo string
End Function


