3. Running Applications
When you need your script to launch another application, use the Run method:
WshShell.Run strCommand[, intWindowStyle][, bWaitOnReturn]
WshShell | The WshShell object. |
strCommand | The
name of the file that starts the application. Unless the file is in the
Windows folder, you should include the drive and folder to make sure
that the script can find the file. |
intWindowStyle | A constant or number that specifies how the application window will appear: |
| intWindowStyle | Window Appearance |
| 0 | Hidden |
| 1 | Normal size with focus |
| 2 | Minimized with focus (the default) |
| 3 | Maximized with focus |
| 4 | Normal without focus |
| 6 | Minimized without focus |
bWaitOnReturn | A logical value that determines whether the application runs asynchronously. If this value is True, the script halts execution until the user exits the launched application; if this value is False, the script continues running after it has launched the application. |
Here’s an example:
Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.Run "Control.exe Inetcpl.cpl", 1, True
This Run method launches the Control Panel’s Internet Properties dialog box.
4. Working with Shortcuts
The WSH enables your scripts to create and modify
shortcut files. When writing scripts for other users, you might want to
take advantage of this capability to display shortcuts for new network
shares, Internet sites, instruction files, and so on.
Creating a Shortcut
To create a shortcut, use the CreateShortcut method:
WshShell.CreateShortcut(strPathname)
WshShell | The WshShell object. |
strPathname | The full path and filename of the shortcut file you want to create. Use the .lnk extension for a file system (program, document, folder, and so on) shortcut; use the .url extension for an Internet shortcut. |
The following example creates and saves a shortcut on a user’s desktop:
Set WshShell = objWScript.CreateObject("WScript.Shell")
Set objShortcut = objWshShell.CreateShortcut("C:\Users\" & _
"Administrator\Desktop\test.lnk")
objShortcut.Save
Programming the WshShortcut Object
The CreateShortcut method returns a WshShortcut object. You can use this object to manipulate various properties and methods associated with shortcut files.
This object contains the following properties:
Arguments— Returns or sets a string
that specifies the arguments used when launching the shortcut. For
example, suppose that the shortcut’s target is the following:
C:\Windows\Notepad.exe C:\ToDoList.txt
In other words, this shortcut launches Notepad and loads a file named ToDoList.txt. In this case, the Arguments property would return the following string:
Description— Returns or sets a string description of the shortcut.
FullName— Returns the full path and filename of the shortcut’s target. This is the same as the strPathname value used in the CreateShortcut method.
Hotkey— Returns or sets the hotkey associated with the shortcut. To set this value, use the following syntax:
WshShortcut.Hotkey = strHotKey
WshShortcut | The WshShortcut object. |
strHotKey | A string value of the form Modifier+Keyname, where Modifier is any combination of Alt, Ctrl, and Shift, and Keyname is one of A through Z or 0 through 23. |
For example, the following statement sets the hotkey to Ctrl+Alt+7:
objShortcut.Hotkey = "Ctrl+Alt+7"
IconLocation— Returns or sets the icon used to display the shortcut. To set this value, use the following syntax:
WshShortcut.IconLocation = strIconLocation
WshShortcut | The WshShortcut object. |
strIconLocation | A string value of the form Path,Index, where Path is the full pathname of the icon file and Index is the position of the icon within the file (where the first icon is 0). |
Here’s an example:
objShortcut.IconLocation = "C:\Windows\System32\Shell32.dll,18"
TargetPath | Returns or sets the path of the shortcut’s target. |
WindowStyle | Returns or sets the window style used by the shortcut’s target. Use the same values outlined earlier for the Run method’s intWindowStyle argument. |
WorkingDirectory | Returns or sets the path of the shortcut’s working directory. |
Note
If you’re working with Internet shortcuts, bear in mind that they support only two properties: FullName and TargetPath (the URL target).
The WshShortcut object also supports two methods, as follows:
Save | Saves the shortcut file to disk. |
Resolve | Uses the shortcut’s TargetPath property to look up the target file. Here’s the syntax:
WshShortcut.Resolve = intFlag
|
WshShortcut | The WshShortcut object. |
intFlag | Determines what happens if the target file is not found: |
| intFlag | What Happens |
| 1 | Nothing. |
| 2 | Windows continues to search subfolders for the target file. |
| 4 | Windows updates the TargetPath property if the target file is found in a new location. |
Listing 3 shows a complete example of a script that creates a shortcut.
Listing 3. A Script That Creates a Shortcut File
Option Explicit
Dim objWshShell, objShortcut
Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objShortcut = objWshShell.CreateShortcut("C:\Users\" & _
"Administrator\Desktop\Edit Hosts File.lnk")
With objShortcut
.TargetPath = "C:\Windows\Notepad.exe"
.Arguments = "C:\Windows\System32\Drivers\etc\hosts"
.WorkingDirectory = "C:\"
.Description = "Opens the hosts file in Notepad"
.Hotkey = "Ctrl+Alt+7"
.IconLocation = "C:\Windows\System32\Shell32.dll,18"
.WindowStyle = 3
.Save
End With