Working with Shortcuts

Microsoft® Windows® 2000 Scripting Guide

Shortcuts are links to local or network programs, files, folders, computers, or Internet addresses. Each shortcut is a file with either an .lnk or a .url extension. The two extensions correspond to the two types of shortcuts: standard shortcuts and Uniform Resource Locator (URL) shortcuts. Standard shortcuts can link to local or network programs, files, folders, or computers, while URL shortcuts link only to entities that can be referenced by a URL most commonly, Web pages.

Shortcuts are used in a number of places within the Windows shell, particularly within menus and toolbars. The Start menu, the Programs menu, the Quick Launch bar, and the SendTo menu, for example, consist of a group of shortcuts located in special folders. The shortcuts that appear on the Quick Launch bar are located in the following folder:

C:\Documents and Settings\{user profile name}\Application Data\Microsoft\Internet Explorer\Quick
 Launch

Your scripts can make use of shortcuts to customize the menus and the desktops of your users; for example, you can create a script that provides different menu options to different groups of users within your organization.

The properties of the WSH Shortcut object are shown in Table 3.10.

Table 3.10 WSH Shortcut Properties

Property

Description

Arguments

Additional command-line arguments that can be used when starting the application.

Description

Description given to the shortcut.

FullName

Read-only property that returns the complete path to the target application.

HotKey

Keyboard shortcut: a combination of keys that, when held down together, will start the application. Keyboard shortcuts typically consist of one of the following keys plus a letter (az), number (09), or function key (F1F12):

  • ALT

  • CTRL

  • SHIFT

For example, to set the keyboard shortcut to the CTRL key and the 9 key, use this value:

CTRL + 9

If the key combination you select is already in use, it will be overwritten and will be applied to the new shortcut created by your script.

IconLocation

Allows you to specify an icon and an icon index for the shortcut. If no location is specified, the default icon for the application is used.

TargetPath

Complete path to the target application. You must specify the full path, including the drive letter or UNC path.

When setting a TargetPath, WSH will accept the value entered. It will not check to ensure that the path is correct.

WindowStyle

Specifies the initial window type for the application. Valid styles are the same as those shown for the Run method and are listed in Table 3.9.

WorkingDirectory

Specifies the working directory for the application.

Creating Standard Shortcuts

Creating a standard shortcut involves three steps:

  1. Create an instance of the WshShortcut object by calling CreateShortcut(), passing as the sole parameter the path for the new shortcut file. Although shortcuts can be created anywhere within the file system, they are typically created within special folders such as AllUsersDesktop and StartMenu. Special folders are discussed later in this chapter.

  2. Set the properties of the WshShortcut object.

  3. Call the WshShortcut Save method. If you do not call the Save method, the shortcut will not actually be created.

The script in Listing 3.17 creates a shortcut to the Internet Information Services (IIS) manager on the desktop. The shortcut is visible to all users of the computer and can be opened either by double-clicking it or by using the key combination CTRL+SHIFT+I.

Listing 3.17 Creating a Desktop Shortcut

  
1
2
3
4
5
6
7
8
Set objShell = WScript.CreateObject("WScript.Shell")
strDesktopFolder = objShell.SpecialFolders("AllUsersDesktop")
Set objShortCut = objShell.CreateShortcut(strDesktopFolder & _
 "\IIS Manager.lnk")
objShortCut.TargetPath = "%SystemRoot%\System32\Inetsrv\iis.msc"
objShortCut.Description = "Run the Internet Information Services Manager."
objShortCut.HotKey = "Ctrl+Shift+I"
objShortCut.Save

In line 2 of Listing 3.17, the WshShell SpecialFolders property retrieves the directory path to the Desktop special folder. This path is stored in the strDesktopFolder variable.

In lines 34, the CreateShortcut method creates a shortcut file named IISManager.lnk in the Desktop folder.

In lines 57, the script sets the TargetPath, Description, and HotKey properties of the WshShortcut object.

In line 8, the script creates the actual shortcut by calling the WshShortcut Save method.

Note

  • An icon will be created on the desktop even if you do not set any properties, but the icon will not be a functional shortcut; if you double-click it, nothing will happen. (You will not even receive an error message.) To create a functional shortcut, you must set the TargetPath property. If the TargetPath property is not set, double-clicking the shortcut will not do anything.

Creating URL Shortcuts

Creating a URL Shortcut involves three similar steps:

  1. Create an instance of the WshUrlShortcut object by calling CreateShortcut, passing as the sole parameter the URL for the new shortcut file.

  2. Set the properties of the WshUrlShortcut object.

  3. Call the WshUrlShortcut Save method.

The script in Listing 3.18 creates a shortcut to the MSDN® Web site on the desktop. The shortcut is visible only to users who run the script. This is because the shortcut is created in the Desktop folder for the current user and not in the AllUsersDesktop folder.

Listing 3.18 Creating a Desktop URL Shortcut

  
1
2
3
4
5
Set objShell = WScript.CreateObject("WScript.Shell")
strDesktopFld = objShell.SpecialFolders("Desktop")
Set objURLShortcut = objShell.CreateShortcut(strDesktopFld & "\MSDN.url")
objURLShortcut.TargetPath = "https://msdn.microsoft.com"
objURLShortcut.Save

Adding an Item to the Quick Launch Bar

The script in Listing 3.19 uses a URL shortcut to create a Quick Launch button that opens the Microsoft® TechNet Web site. The Quick Launch button is visible only to users who run the script because the shortcut is created in the personal Quick Launch bar for the user.

Listing 3.19 Creating a Quick Launch Button to Open the TechNet Online Web Site

  
1
2
3
4
5
6
7
8
9
Set objShell = WScript.CreateObject("WScript.Shell")
Set colEnvironmentVariables = objShell.Environment("Volatile")
strQLFolder = colEnvironmentVariables.Item("APPDATA") & _
 "\Microsoft\Internet Explorer\Quick Launch"
 
Set objURLShortcut = objShell.CreateShortcut(strQLFolder & "\TechNet.url")
objURLShortcut.TargetPath = "https://www.microsoft.com/technet"
objURLShortcut.Save

Deleting Shortcuts

Shortcuts are files that can be deleted in the same way you delete any other file. For example, the following script deletes the shortcut created in Listing 3.19:

Set objShell = WScript.CreateObject("WScript.Shell")
Set objShell = WScript.CreateObject("WScript.Shell")
Set colEnvironmentVariables = objShell.Environment("Volatile")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strQLFolder = colEnvironmentVariables.Item("APPDATA") & _
"\Microsoft\Internet Explorer\Quick Launch\TechNet.URL"
objFSO.DeleteFile(strQLFolder)

For more information about working with files in WSH scripts, see "Script Runtime Primer" in this book.