The Windows Scripting Host (Cont.) Shell Object Properties and Methods Environment: This property provides access to environment collection. Information such as number of processors, paths, OS, etc can be determined from this collection. oEnv = wshshell.environment For Each x In oEnv ?oEnv Next x
The following is a partial listing of some typical environmental variables: - NUMBER_OF_PROCESSORS
- OS
- PROCESSOR_ARCHITECTURE
- PROCESSOR_IDENTIFIER
- WINDIR
So, to find out the number of processors: Numprocessors = oEnv.Item("NUMBER_OF_PROCESSORS")
Like most collections, if you want to find out how many members are contained in the collection, you can refer to the Count Property: Numitems = oEnv.Count
To be compliant with the Java Language, collections in the Windows Scripting Host also support the Length Property ? which provides the same functionality as the Count Property: Numitems = oEnv.Length
SpecialFolders: This property provides access to the collection of Windows Shell Folders. The following is a listing of folders: - AllUsersDesktop
- AllUsersStartMenu
- AllUsersPrograms
- AllUsersStartup
- Desktop
- Favorites
- Fonts
- MyDocuments
- NetHood
- PrintHood
- Programs
- Recent
- SendTo
- StartMenu
- Startup
- Templates
To find the actual path of the desktop folder, issue this line of code: DesktopPath = WSHShell.SpecialFolders("Desktop") CreateShortcut (strPathname)
This method creates and returns a shortcut object. The following block of code illustrates how to create a desktop shortcut: */ Read desktop path using WshSpecialFolders object DesktopPath = WSHShell.SpecialFolders("Desktop") */ Create a shortcut object on the desktop MyShortcut = ; WSHShell.CreateShortcut(DesktopPath + "\Shortcut to MyFile.lnk") */ Set shortcut object properties and save it FileName = GetFile() If !Empty(FileName) FileDir = JustPath(FileName) With MyShortcut .TargetPath = Filename .WorkingDirectory = FileDir .Save EndWith Endif
The CreateShortcut Method returns a WSHShortcut Object. This object is only exposed through the CreateShortcut Method. The WSHShortcut object has the following properties: Arguments Parameters to a shortcut object. Description A description of a shortcut object. Hotkey The hot key of a shortcut object. IconLocation The icon location of a shortcut object. TargetPath The target path of a shortcut object. WindowStyle The window style of a shortcut object. WorkingDirectory The working directory of a shortcut object. The WSHShortcut Object only has one method, Save(), which saves the shortcut object to the file system. ExpandEnvironmentSettings (strString)
This method expands a process environment variable and returns the result. A typical environment variable is WINDIR. The following code illustrates how this method works: Fullpath = WSHShell.ExpandEnvironmentStrings("%windir%\notepad.exe, 0") Popup (strText, [natSecondsToWait], [strTitle], [natType])
This method displays the Window MessageBox. Unlike the existing MessageBox Function, the Popup method accepts an optional argument to clear the dialog after a specified amount of time. The following line of code illustrates how the Popup Method works: WSHShell.Popup("Message",1,"Title",64) RegDelete(strName) RegRead(strName) RegWrite(strName, anyValue, [strType] )
The Windows Scripting Host Shell Object provides three methods for working with the Windows Registry. With these methods, entries can be created, written, read, and deleted. If you attempt to write to a key that does not exist, the key will be created. The following code illustrates how these three methods work: To create a new key: WSHshell.RegWrite("HKCU\Software\Microsoft\VisualFoxPro\6.0\Desktop\JVP",'Default Value')
Then, to modify the value: WSHshell.RegWrite("HKCU\Software\Microsoft\VisualFoxPro\6.0\Desktop\JVP",'New Value')
Next, to just read a value from a key: WSHshell.RegRead("HKCU\Software\Microsoft\VisualFoxPro\6.0\Desktop\JVP")
Finally, to delete a key: WSHshell.RegDelete("HKCU\Software\Microsoft\VisualFoxPro\6.0\Desktop\JVP") Run(strCommand, [intWindowStyle], [blnWaitOnReturn])
The Run Method creates a new process that executes a specified command with a specified window style. In addition to being able to specify the command and the window style, a third parameter can be used to determine if script execution should pause until the new process has terminated. Applications windows can be started in several states. The following outlines the various options available: SW_HIDE 0 SW_MINIMIZE 6 SW_RESTORE 9 SW_SHOW 5 SW_SHOWMAXIMIZED 3 SW_SHOWMINIMIZED 2 SW_SHOWMINNOACTIVE 7 SW_SHOWNA 8 SW_SHOWNOACTIVATE 4 SW_SHOWNORMAL 1 The following code starts Notepad with a normal window: WSHshell.Run("notepad",1)
This code starts Notepad with a maximized window. In addition, the user must close Notepad before control will return to Visual FoxPro: WSHshell.Run("notepad",3,.T.)
Fully qualified paths can be used to denote a file to execute. The following code starts Visio: WSHshell.Run("D:\VISIO\VISIO32.EXE")
Also, script files can be executed with the Run Method as well: WSHshell.Run("hello world.vbs") WSHshell.Run("hello world.js")
|