[Previous] [Table of Contents] [Next]

Shortcut Basics

In Windows, shortcuts provide a way to link to an application, a file, or a folder. Usually, the user creates a shortcut in Windows by right-clicking on a file or folder and dragging it to another folder, the Desktop, or the Start menu. A shortcut menu appears with the Create Shortcut(s) Here command. Alternatively, you can right-click on the file or folder and choose Create Shortcut from the shortcut menu.

These techniques create a shortcut file with the .lnk extension. This file contains properties such as the shortcut icon and paths to the target directory and the working directory. When you right-click on a shortcut icon and choose Properties, a property sheet appears. Figure 10-1 shows the Shortcut property page for a shortcut file.

In Windows, you use API functions to manipulate shortcut files and their properties. In Microsoft Windows Script Host (WSH), you use methods of the WshShell object to create shortcuts.

Figure 10-1 Shortcut properties

Using the CreateShortcut Method

To create a new shortcut, you use the CreateShortcut method of the WshShell object in WSH, which has the following syntax:

Set WshShell = WScript.CreateObject("WScript.Shell")
Set object = WshShell.CreateShortcut("shortcut_file.lnk")

The first VBScript statement creates an object instance of the WshShell object and stores the reference in the object variable WshShell. The second statement applies the CreateShortcut method to the WshShell object.

The CreateShortcut method requires a parameter for the filename of the shortcut file to be created. This method creates a WshShortcut object but doesn't create an actual shortcut file. It also assigns an object instance to an object variable. In subsequent statements, you can set the properties of this shortcut object. When you create a new shortcut file or update an existing file, you must apply the Save method of the shortcut object to finalize any changes.

NOTE
If you apply the CreateShortcut method with the name of a new shortcut, no .lnk file is created until you call the Save method. If the .lnk file specified in the CreateShortcut parameter already exists, the properties of this file are changed to the corresponding properties of the WshShortcut object.

Creating a shortcut to the current script file

For simplicity, our sample will create a shortcut to its own source code file. The .lnk file will be stored in the same folder as the script file. After the script starts, the program will ask the user whether to create the shortcut file. If the user clicks the Yes button, the script determines the path to the current script file by using the following two lines of VBScript code:

path = WScript.ScriptFullName
path = Left(path, InStrRev(path, "\"))

These two statements read the name of the script file, including the path, and strip off the filename.

You also need the name of the script file without the filename extension:

Lnk_Title = WScript.ScriptName
Lnk_Title = Left(Lnk_Title, InStrRev(Lnk_Title, ".") - 1)

The first statement reads the script filename. In the second statement, the string is searched in reverse order for the period separating the filename from the extension. The extension is then removed from the string using the VBScript Left function.

To create the shortcut, you need the WshShell object:

Set WshShell = WScript.CreateObject("WScript.Shell")

This object is a subobject of WScript (and has nothing to do with the Windows Shell object, which we'll discuss in later chapters). Now you can apply the CreateShortcut method to the WshShell object:

Set Shortcut = WshShell.CreateShortcut(path & Lnk_Title & ".lnk")

The parameter of the CreateShortcut method of the WshShell object defines the path and the name of the new .lnk file. This information is located in the variables path and Lnk_Title. When the CreateShortcut method is executed, a reference to the new WshShortcut object is assigned to the Shortcut object variable.

NOTE
At this point, you don't have a shortcut file on the hard disk. All you have is a WshShortcut object instance that's specified in an object variable. You can then set the properties of the WshShortcut object. To create the actual shortcut file, you must apply the Save method.

After creating a WshShortcut object instance, you can set the WshShortcut properties:

Shortcut.TargetPath = WScript.ScriptFullName 

The TargetPath property defines the path to the target file, which is specified in the shortcut file. Our sample specifies the current script file by assigning the ScriptFullName property of the WScript object to TargetPath.

In a second statement, you assign the working directory for this shortcut:

Shortcut.WorkingDirectory = path

Any properties not set in the script (such as window style or hot keys) are automatically set to default values.

Now you use the Save method to create the new .lnk file for the shortcut object:

Shortcut.Save

This method requires no further parameters. Listing 10-1 shows the full implementation details. (Like the other samples in this chapter, this file is in the \WSHDevGuide\Chapter10 folder on the book's companion CD.)

Listing 10-1 Shortcut.vbs

'************************************************
' File:    Shortcut.vbs (WSH sample in VBScript) 
' Author:  (c) G. Born
'
' Creating a shortcut to the current file in
' the script file's folder
'************************************************
Option Explicit 

Dim Text, Title, Lnk_Title, Shortcut
Dim WshShell    ' Object variable
Dim status, Ready_txt, path

' Define user messages.
Ready_txt = "Shortcut created"
Text = "Creates a shortcut to this script file in the current folder"
Title = "WSH sample - by G. Born"

status =  MsgBox(Text, _
                 vbOKCancel + vbInformation, _
                 Title)

If (status <> vbOK) Then
    WScript.Quit 1         ' Cancel selected.
End if

' *** Get script file path.
path = WScript.ScriptFullName
path = Left(path, InstrRev(path, "\"))

' Get script filename without extension.
Lnk_Title = WScript.ScriptName
Lnk_Title = Left(Lnk_Title, InStrRev(Lnk_Title, ".") - 1)

' Create new WshShell object, whose CreateShortcut method we need.
Set WshShell = WScript.CreateObject("WScript.Shell")

' Use the CreateShortcut method to create a shortcut.
Set Shortcut = WshShell.CreateShortcut(path & Lnk_Title & ".lnk")

' Set the shortcut properties. First set the target folder.
Shortcut.TargetPath = WScript.ScriptFullName

' Set working directory.
Shortcut.WorkingDirectory = path

Shortcut.Save             ' Create shortcut.

WScript.Echo Ready_txt    ' Ready message

'*** End

The JScript implementation

The JScript version, shown in Listing 10-2, uses the same objects, methods, and properties; only the syntax is different.

Listing 10-2 Shortcut.js

//************************************************
// File:    Shortcut.js (WSH sample in JScript) 
// Author:  (c) G. Born
// 
// Creating a shortcut to the current file in
// the script file's folder
//************************************************

var vbOKCancel = 1;       // Create variables.
var vbInformation = 64;
var vbCancel = 2;

var L_Welcome_MsgBox_Message_Text =
    "Creates a shortcut to this script file in the current folder";
var L_Welcome_MsgBox_Title_Text = "WSH sample - by G. Born";

Welcome();    // Welcome dialog box

// Get script path.
var path = WScript.ScriptFullName;
path = path.substr(0, path.lastIndexOf("\\") + 1);

// Get script filename and strip off extension.
var Lnk_Title = WScript.ScriptName
Lnk_Title = Lnk_Title.substr(0, Lnk_Title.lastIndexOf("."));

// Get WshShell object.
var WshShell = WScript.CreateObject("WScript.Shell");

// Create shortcut object.
var Shortcut = WshShell.CreateShortcut(path + Lnk_Title + ".lnk");

// Set shortcut properties.
Shortcut.TargetPath = WScript.ScriptFullName;
Shortcut.WorkingDirectory = path;
Shortcut.Save();     // Store shortcut file.

WScript.Echo("Shortcut created");

/////////////////////////////////////////////////////////////
//
// Welcome
//
function Welcome() 
{
    var WshShell = WScript.CreateObject("WScript.Shell");
    var intDoIt;

    intDoIt =  WshShell.Popup(L_Welcome_MsgBox_Message_Text,
                              0,
                              L_Welcome_MsgBox_Title_Text,
                              vbOKCancel + vbInformation);
    if (intDoIt == vbCancel) 
    {
        WScript.Quit(1);   // Cancel selected.
    }
}

//*** End