Previous Section Table of Contents Next Section

The Shortcut Object

Shortcut objects are created by using the Shell object's CreateShortcut method. This method only specifies the final location for the shortcut; it doesn't allow you to specify the shortcut's own properties. To do that, you modify the properties of the Shortcut object, and then call the Shortcut object's Save method to save your changes.

Methods and Properties

The Shortcut object offers the following properties.

  • Arguments. These are any command-line arguments that should be passed when the shortcut is launched.

  • Description. A description of the shortcut.

  • FullName. This is a read-only property and returns the full name of the target application.

  • HotKey. The hot key that can be used to launch the shortcut from the keyboard. You can use any letter, number, or function key (F1 to F12). You can also specify Control, Alt, or Shift keys, such as Alt+F9.

  • IconLocation. The name of an icon file, along with an index to a specific icon, that should be used for the shortcut.

  • TargetPath. The complete path and filename to the target application. UNCs are acceptable.

  • WindowStyle. Specifies the starting window style for the shortcut when launched.

  • WorkingDirectory. Sets the working directory for the application launched by the shortcut.

You can create two types of shortcuts:

  1. Standard shortcuts have an LNK filename extension and generally point to applications on the local computer or network.

  2. Internet shortcuts have a URL filename extension and point to Web sites.

You'll see examples of both in Listing 11.2.

Practical Application

Listing 11.2 shows an example script that creates both a normal application shortcut and a URL shortcut.

Listing 11.2. Shortcuts.vbs. Creates shortcuts on the user's desktop.

' this sample creates two shortcuts on the current user's desktop

' shows how to use the Shell interface from within Script.



'first, we need to create an instance of the shell object

dim objShell

set objShell = WScript.CreateObject("WScript.Shell")



'next, we need to get the path to the special Desktop folder

dim strDesktop

strDesktop = objShell.SpecialFolders("Desktop")



'now, we can create shortcuts on the desktop



'let's do Internet Explorer

dim objShortcut

set objShortcut= objShell.CreateShortcut(strDesktop & "\IE.lnk")

with objShortcut

     .TargetPath = "iexplore.exe"

     .WindowStyle = 1

     .Hotkey = "CTRL+SHIFT+I"

     .Description = "Launch Internet Explorer"

     .WorkingDirectory = strDesktop

     .Save

end with



'let's create a link to my home page

dim objURL

set objURL = objShell.CreateShortcut(strDesktop & _

 "\BrainCore Website.url")

objURL.TargetPath = "http://www.braincore.net"

objURL.Save

I briefly introduced you to the With…End With construct earlier. Here, it's used so that I don't have to keep retyping objShortcut over and over. Each of the lines following the With statement begins with a period, and so VBScript assumes I'm talking about objShortcut, the object mentioned in the With statement.

    Previous Section Table of Contents Next Section