User Interaction with Common Dialogs in WSH - Watch it in action
(Page 4 of 4 )
Let's take a look at the IE object's File Open dialog in action. We'll use it to make a backup copy of a file. The file will be copied to the same directory with a BAK extension.
WScript.Echo "Please select the file you would like to copy."
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
Do While objIE.ReadyState <> 4
WScript.Sleep 1
Loop
objIE.Document.Write("<html><body><input id=field " _
& "type=file></body></html>")
objIE.Document.all.field.focus
objIE.Document.all.field.click
strFile = objIE.Document.all.field.value
objIE.Quit
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.GetFile(strFile)
objFile.Copy(objFile.Path & ".bak")
WScript.Echo "Backup completed."
This is a pretty basic script. The IE Object's File Open dialog returns a file path. We use that file path along with the FileSystemObject's GetFile method to create a File object and make a copy of it.
I also decided to show a different looping method for checking if Internet Explorer is busy. I've replaced the Do Until...Loop with its converse Do While...Loop.
Let's take a look at another example. In this example, we'll use the File Open Dialog to choose a file. Then we'll create a shortcut on the Desktop to that file.
WScript.Echo "Please select a file to make a shortcut to."
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
While objIE.ReadyState <> 4
WScript.Sleep 1
Wend
objIE.Document.Write("<html><body><input id=field " _
& "type=file></body></html>")
objIE.Document.all.field.focus
objIE.Document.all.field.click
strFile = objIE.Document.all.field.value
objIE.Quit
Set WshShell = CreateObject("WScript.Shell")
With WshShell
strPath = .ExpandEnvironmentStrings("%homepath%DesktopTest.lnk")
Set WshShortcut = .CreateShortcut(strPath)
End With
With WshShortcut
.Description = "Test Shortcut"
.TargetPath = strFile
.Save
End With
Now I've taken the result of the Open dialog box and used the file path to create a Desktop shortcut to the file. I've implemented the WshShell object coupled with its WshShortcut object to do the dirty work.
I've also changed up the looping scheme again replacing the Do While...Loop with a While...Wend loop. I've also used the With statement to shorten things up a bit and save myself some typing.
I've shown you how to implement Microsoft's Common Dialog control to present a more user-friendly script. As I've stated, these methods each have their own drawbacks. Be sure to stick around for part two of this series when I show you some more viable alternatives. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |