If Internet Explorer 4 or later is installed on your machine, you can access a few functions of the Windows shell. In this section, I'll demonstrate a few neat tricks for using the Windows Shell object from a script.
NOTE
The upcoming samples require the Windows shell version 4.71 or later. Version 4.71 is installed with Internet Explorer 4 if the Active Desktop feature is present. Windows 98 comes with Windows shell version 4.72, and Windows 2000 uses version 5. Only users of Windows 95 and Windows NT 4 must check whether Shell32.dll contains the right version. Note that installing Internet Explorer 5 on a machine doesn't update the shell. You must install Internet Explorer 4 with the Active Desktop feature and then upgrade to Internet Explorer 5.
To test whether your Windows shell is the right version, you can use the small VBScript program in Listing 14-17. The program requires WSH 2. The paths to Shell32.dll are set for Windows NT or Windows 2000. To use the script in Windows 95 or Windows 98, you must uncomment the second statement, which sets the path for Windows 95 and Windows 98.
Listing 14-17 GetShellVersion.vbs
'******************************************************** ' File: GetShellVersion.vbs (WSH 2 sample in VBScript) ' Author: (c) G. Born ' ' Retrieving the file version of the Shell32.dll ' library file '******************************************************** Option Explicit Dim fso, oShell ' Object variables Dim file, tmp ' Filename ' Retrieve path to Shell32.dll. ' In Windows NT and Windows 2000, the file is located in file = "%WINDIR%\System32\Shell32.dll" ' In Windows 95 and Windows 98, the file is located in 'file = "%WINDIR%\System\Shell32.dll" Set oShell = WScript.CreateObject("WScript.Shell") file = oShell.ExpandEnvironmentStrings(file) ' Create FileSystemObject object to access the file system. Set fso = CreateObject("Scripting.FileSystemObject") ' Check whether file exists. If fso.FileExists(file) Then tmp = fso.GetFileVersion(file) ' Retrieve version. If Len(tmp) > 0 Then WScript.Echo "File " & file & vbCrLf & _ "Version: " & tmp Else WScript.Echo "File " & file & vbCrLf & _ "Version: undefined" End if Else WScript.Echo "File '" & file & "' not found" End If '*** End |
If you've ever customized your Desktop to put shortcut menu items such as Minimize All and Tile Windows Horizontally on your taskbar, you know that doing so can be handy—for example, to minimize all open folder windows after a system start. To automate this task using a WSH script, you can use several objects, methods, and properties of the Windows shell.
You first create the Application object by using the following statement:
Set Shell = WScript.CreateObject("Shell.Application") |
This object provides the following methods to manipulate windows on your Desktop:
The VBScript program in Listing 14-18 shows several dialog boxes that ask the user whether to minimize or tile horizontally or vertically the Desktop windows (Figure 14-7). The script provides an undo function for each step, so you can test how the methods work.
Figure 14-7 Manipulating Desktop windows using a WSH script
To undo the most recent action, you can use a trick. After arranging the windows by using the TileVertically method, for example, you can undo this step by using the UndoMinimizeAll method (even though the name suggests a different action).
Listing 14-18 Shell.vbs
'*************************************************** ' File: Shell.vbs (WSH sample in VBScript) ' Author: (c) G. Born ' ' Accessing the Windows shell from a WSH script and ' minimizing and aligning horizontally/vertically ' the windows on the Desktop '*************************************************** Option Explicit Dim Shell, Title Title = "WSH sample - by G. Born" ' Create Shell object. Set Shell = WScript.CreateObject("Shell.Application") If (MsgBox("Minimize all windows?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.MinimizeAll ' MinimizeAll method WScript.Echo "Undo minimize all. " Shell.UndoMinimizeAll ' Restore windows. End If If (MsgBox("Tile windows vertically?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.TileVertically ' Vertically WScript.Echo "Undo tile vertically." Shell.UndoMinimizeAll ' Restore windows. End If If (MsgBox("Tile windows horizontally?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.TileHorizontally WScript.Echo "Undo tile horizontally." Shell.UndoMinimizeAll ' Restore windows. End If If (MsgBox("Cascade all windows?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.CascadeWindows WScript.Echo "Undo cascade windows." Shell.UndoMinimizeAll ' Restore windows. End If WScript.Echo "Ready?" '*** End |
You can use the Run method to launch Windows Explorer and browse a folder window. Alternatively, you can use the Open and Explore methods of the Shell object from a script to open a folder in Shell mode (single pane) or Explorer mode (two panes). Both methods require the path to the requested folder as an argument. The sample in Listing 14-19 uses these methods to open the Windows folder in Windows Explorer and then open the \System subfolder as a folder window (in Shell mode).
Listing 14-19 Shell1.vbs
'************************************************ ' File: Shell1.vbs (WSH sample in VBScript) ' Author: (c) G. Born ' ' Accessing the Windows shell and opening a ' folder window '************************************************ Option Explicit Dim Shell, wsh, Title, path Title = "WSH sample - by G. Born" ' Create WshShell object. Set wsh = WScript.CreateObject ("WScript.Shell") ' Windows directory path = wsh.ExpandEnvironmentStrings("%WINDIR%") ' Create Windows shell Application object. Set Shell = WScript.CreateObject ("Shell.Application") ' Open a folder in the Explorer window. WScript.Echo "Windows Explorer folder" ' This runs Explorer with the folder tree rooted at the path specified ' (same as wsh.Run "explorer.exe /e,/root," & path). Shell.Explore path ' Show folder in a shell window. WScript.Echo "Open Windows folder \System" Shell.Open path & "\System" WScript.Echo "Ready?" '*** End |
The Windows shell comes with some integrated dialog boxes, including the Run, Find, and Date/Time dialog boxes, that you can invoke using the taskbar and the Start menu. The shell also provides an Automation interface that you can use from a script. The following command retrieves an object instance of the shell:
Set oShell = WScript.CreateObject("Shell.Application") |
You use methods of the Windows Shell object to invoke these dialog boxes or open the Control Panel folder and the property sheets in Control Panel. You can access Control Panel by using the ControlPanelItem method:
oShell.ControlPanelItem |
You can submit the name of the requested module (such as Desk.cpl) as a parameter to this method. If you submit an empty string or nothing, the Control Panel folder is opened.
To open the Run dialog box, for example, you simply apply the FileRun method to the Windows Shell object. The following statement opens the Run dialog box and displays the last command entered:
oShell.FileRun |
To invoke the Find dialog box to search for files and folders, you use this method:
oShell.FindFiles |
To invoke the dialog box to search for a computer, you use this method:
oShell.FindComputer |
If you need the property page to set the current date and time, you use the SetTime method:
oShell.SetTime |
I already mentioned several techniques for shutting down Windows 95 or Windows 98. The Shell object also has two methods that suspend the PC (activate energy-saving mode) or shut down Windows. The following command initiates Windows Suspend mode. (The effect of the Suspend method depends on your machine and environment.)
oShell.Suspend |
You can invoke the Shut Down Windows dialog box (Figure 14-8) by using the following command:
Shell.ShutdownWindows |
Figure 14-8 The Shut Down Windows dialog box
The VBScript program in Listing 14-20 shows several of the dialog boxes just mentioned.
Listing 14-20 Shell2.vbs
'************************************************ ' File: Shell2.vbs (WSH sample in VBScript) ' Author: (c) G. Born ' ' Accessing the Windows shell and invoking ' several dialog boxes '************************************************ Option Explicit Dim Shell, wsh, Title, path Title = "WSH sample - by G. Born" ' Create WshShell object. Set wsh = WScript.CreateObject ("WScript.Shell") ' Create Windows shell Application object. Set Shell = WScript.CreateObject ("Shell.Application") ' Invoke the Control Panel folder. If (MsgBox("Open Control Panel?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.ControlPanelItem "" ' Without arguments End If ' Now try to open the module Desk.cpl. If (MsgBox("Show display properties?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.ControlPanelItem "Desk.cpl" End If ' Invoke Run dialog box. If (MsgBox("Open Run dialog box?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.FileRun End If ' Invoke Search Files dialog box. If (MsgBox("Open Find: All Files dialog box?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.FindFiles End If ' Invoke Search Computers dialog box. If (MsgBox("Open Find: Computer dialog box?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.FindComputer End If ' Invoke Date/Time property sheet. If (MsgBox("Change date/time?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.SetTime End If ' Call Suspend method. If (MsgBox("Suspend Windows?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.Suspend End If ' Invoke Shut Down Windows dialog box. If (MsgBox("Open Shut Down Windows dialog box?", _ vbYesNo + vbQuestion, Title) = vbYes) Then Shell.ShutdownWindows End If '*** End |