The
Windows Script Host 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\Paul\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:\Boot.ini
In other words, this shortcut launches Notepad and loads the Boot.ini file. 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 will be 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 12. |
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 PathIndex is the position of the icon within the file (where the first icon is 0). is the full pathname of the icon file and |
Here’s an example:
objShortcut.IconLocation = "C:\Windows\System32\Shell32.dll,21"
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:
Listing 1 shows a complete example of a script that creates a shortcut.
Listing 1. A Script That Creates a Shortcut File
Set objWshShell = WScript.CreateObject("WScript.Shell") Set objShortcut = objWshShell.CreateShortcut("C:\Users\Paul\Desktop\Edit BOOT.INI.lnk") With objShortcut .TargetPath = "C:\Windows\Notepad.exe " .Arguments = "C:\Boot.ini" .WorkingDirectory = "C:\" .Description = "Opens BOOT.INI in Notepad" .Hotkey = "Ctrl+Alt+7" .IconLocation = "C:\Windows\System32\Shell32.dll,21" .WindowStyle = 3 .Save End With
|