One of Tom's desktop scripting projects at ABC, Inc. is to standardize the look and feel of the Windows desktop on all new computers that he deploys. One of the ways that he plans to accomplish this is by creating a folder called the Standard Applications folder and adding a number of shortcuts to it. These shortcuts will include:
This chapter will explain in detail how Windows shortcuts work and how to use the methods and properties belonging to the WSH WshShortcut object in order to accomplish this task.
In addition, this chapter will show you how to add menus to the Start menu and how to add menu items to existing menus. Specifically, you will see how to create a submenu under the All Programs menu by adding the Standard Applications folder. The shortcuts in this folder will then function as menu items.
You will also learn how to use shortcuts to manage the configuration of the Quick Launch toolbar. This will include adding shortcuts to the Windows Calculator and WinZip applications. The chapter will conclude by demonstrating how to create two of the scripts required to automate the configuration of computers at ABC, Inc.
Shortcuts provide a tool for organizing access to applications, files, drives, printers and many other Windows resources. Placing shortcuts on the Windows desktop provides quick access to resources that are constantly accessed. However, placing too many shortcuts on the Windows desktop will clutter things up and can be distracting. To prevent this, shortcuts can be added to other convenient locations, such as the Start menu and the Quick Launch toolbar.
Note |
By default, the Quick Launch toolbar resides just to the right of the Start menu. It provides single-click access to any shortcut that is added to it. |
A shortcut represents a link to another Windows resource. By consistently creating and placing shortcuts in the same place on each new Windows XP computer, Tom hopes to begin introducing the idea of desktop standardization to the employees of ABC, Inc.
Figure 12.1 shows a shortcut for the Windows Notepad application.
Figure 12.1: A shortcut to the Notepad application
You can tell a shortcut apart from the resource that it represents by the presence of a small curved black arrow in the lower left-hand corner of the shortcut's icon. Shortcuts have a number of properties that control their operation. For example, the Target property specifies the location and name of the resource that the shortcut represents. To view all the properties associated with a shortcut, right-click on the shortcut and select Properties from the menu that appears. This will open the Properties dialog box for the shortcut and display its Shortcut property sheet. For example, Figure 12.2 shows the properties associated with a shortcut to the Notepad application.
Figure 12.2: Examining the properties of a shortcut to the Notepad application
Table 12.1 lists and describes all of the properties associated with shortcuts.
Property |
Description |
---|---|
Target |
Specifies the complete path and file name of the Windows object |
Start in |
Specifies the application's default working directory |
Shortcut key |
Specifies a keyboard keystroke sequence that can be used to open the shortcut |
Run |
Specifies whether the application will be opened in a normal window or in one that is maximized or minimized |
Comment |
Specifies an optional shortcut description |
Icon filename |
Identifies the icon used to represent the shortcut |
To create and manage shortcuts, you will need to learn how to work with the WshShortcut object. In addition, you will need to establish a reference to the location where you want to save each shortcut. In order to work with the WshShortcut object, you must first instantiate its parent object, the WshShell, as shown below.
Set WshShl = WScript.CreateObject("WScript.Shell")
Before you can proceed with the instantiation of the WshShortcut object, you must set up a reference to the location where the shortcut will be saved. For example, to set up a reference to the Windows desktop, you must use the Windows Desktop special folder.
DesktopFolder = WshShl.SpecialFolders("Desktop")
You can now define the shortcut by using the WshShell object's CreateShortcut() method to set up an instance of the WshShortcut object, as demonstrated below.
Set NotepadShortcut = WshShl.CreateShortcut(DesktopFolder & "\Notepad.lnk")
As you can see, the shortcut is defined by concatenating its destination folder to its name (\Notepad.lnk).
Note |
In the previous chapter, you learned how to work with the WshUrlShortcut object. A URL shortcut is created in the same way as a standard shortcut, the only difference being that you specify .url instead of .lnk at the end of the shortcut definition. Specifying .url results in the instantiation of the WshUrlShortcut object, and specifying .lnk results in the specification of the WshShortcut object. |
The next step involved in setting up the shortcut is to specify the Windows resource that the shortcut is to represent. This is done using the WshShortcut object's TargetPath property, as demonstrated below.
NotepadShortcut.TargetPath = "%windir%Notepad.exe"
The final step in the creation of the shortcut is to save the shortcut. This is done using the WshShortcut object's Save() method:
NotepadShortcut.Save()
Note |
The Notepad application resides by default in the C:Windows folder on computers running Windows 98, Me, and XP. However, on Windows 2000 it resides by default in C:Winnt. In addition, it is possible to modify the location of the folder where Windows system files are stored during the installation of the operating system, in which case the Notepad application could reside in an entirely different folder. One way to avoid any confusion and to facilitate the development of a single script that will work on any of these Microsoft operating systems is to take advantage of the %windir% environment variable. This variable is automatically created by the operating system. It specifies the location of the Windows system folder, wherever it may reside. |
By using the above statements as a template, it's easy to assemble a VBScript that creates a shortcut to the Notepad application.
'************************************************************************* 'Script Name: Script 12.1.vbs 'Author: Jerry Ford 'Created: 02/22/03 'Description: This script creates a desktop for the Windows Notepad 'application '************************************************************************* 'Initialization Section Option Explicit Dim WshShl, DesktopFolder, NotepadShortcut Set WshShl = WScript.CreateObject("WScript.Shell") 'Main Processing Section CreateShortcut() WScript.Quit() 'Procedure Section 'Create the desktop Shortcut Sub CreateShortcut() DesktopFolder = WshShl.SpecialFolders("Desktop") Set NotepadShortcut = WshShl.CreateShortcut(DesktopFolder & _ "\Notepad.lnk") NotepadShortcut.TargetPath = "%windir%Notepad.exe" NotepadShortcut.Save() End Sub
Note |
You can also use VBScript to delete Shortcuts. To do so, you need to use the WshShortcut object's Delete() method as demonstrated below. Set WshShl = WScript.CreateObject("WScript.Shell") DesktopFolder = WshShl.SpecialFolders("Desktop") Set FsoObject = CreateObject("Scripting.FileSystemObject") Set NotepadShortcut = FsoObject.GetFile(DesktopFolder & "\notepad.lnk") NotepadShortcut.Delete |
The shortcut defined by the previous VBScript was defined using a single WshShortcut object property, the TargetPath property. As a result, the rest of the shortcut properties either were left undefined or were set using defaults, as shown in Figure 12.3.
Figure 12.3: Examining the properties assigned to the shortcut for the Notepad application
As Table 12.2 shows, the WshShortcut object provides access to all the properties associated with shortcuts.
Property |
Description |
---|---|
Arguments |
Specifies arguments to be passed to the application |
Description |
Specifies a comment |
Hotkey |
Specifies a keystroke sequence that can be used to open the shortcut |
IconLocation |
Specifies the icon to be displayed |
TargetPath |
Specifies the complete path and file name of the object represented by the shortcut |
WindowStyle |
Specifies the window style to be used when the application is started from the shortcut (normal, minimized, or maximized) |
WorkingDirectory |
Specifies the application's default working directory as well as the default location where any output will be saved |
Most of these properties are strings that specify a particular piece of information. However, three of these properties require further explanation.
The WshShortcut object's Hotkey must include a minimum of one modifier key and one key designator. A modifier key can be any of the following:
A key designator can be any of the following:
The WshShortcut object's IconLocation is used to specify the index position of an icon to be used to represent a shortcut. Many times a Windows object, such as an application's executable file, contains an indexed list of icons, which can be used to represent the application. For example, Figure 12.4 shows the icons available for the Windows WordPad application. These icons can be viewed by right-clicking on the WordPad application executable's icon, selecting Properties, and then clicking on the Change Icon button on the Shortcut Properties sheet.
Figure 12.4: Many Windows applications provide an optional indexed list of icons that can be selected
The WshShortcut object's WindowStyle is used to specify the type of window that the shortcut should use. Table 12.3 lists the three Windows style types that are supported by the WshShortcut object.
WindowStyle |
Description |
---|---|
1 |
Displays a window by restoring it to its location and size |
2 |
Displays a maximized window |
7 |
Minimizes the window |
By setting additional shortcut properties, you can further refine the definition of your shortcuts. For example, the following VBScript is a modified version of the previous example. In addition to creating the Notepad shortcut, this new script sets or modifies a number of additional properties. For example, the value of the Arguments property is set to "D:DskTpError.log". This property specifies a file that is to be opened by the Notepad application whenever the shortcut is used to open it. In addition, the Description property is set equal to Desktop Error Log. As a result, this comment will be displayed any time the user moves the pointer over the shortcut. Finally, the Hotkey property is set equal to Ctrl+Alt+D. This allows the shortcut to be opened by pressing the Ctrl, Alt, and D keys at the same time.
'************************************************************************* 'Script Name: Script 12.2.vbs 'Author: Jerry Ford 'Created: 02/22/03 'Description: This script creates a desktop for the Windows Notepad 'application '************************************************************************* 'Initialization Section Option Explicit Dim WshShl, DesktopFolder, NotepadShortcut Set WshShl = WScript.CreateObject("WScript.Shell") 'Main Processing Section CreateShortcut() WScript.Quit() 'Procedure Section 'Create the desktop Shortcut Sub CreateShortcut() DesktopFolder = WshShl.SpecialFolders("Desktop") Set NotepadShortcut = WshShl.CreateShortcut(DesktopFolder & "\Notepad.lnk") NotepadShortcut.TargetPath = "%windir%Notepad.exe" NotepadShortcut.Description = "Desktop Error Log" NotepadShortcut.Arguments = "D:DskTpError.log" NotepadShortcut.Hotkey = "CTRL+Alt+D" NotepadShortcut.Save() End Sub
Figure 12.5 shows how the properties are set for the shortcut created by the previous script.
Figure 12.5: Validating property settings for the new shortcut
One of Tom's scripting projects is the development of a Standard Applications folder. Tom plans to create this folder and then populate it with a number of application shortcuts in order to provide quick access to a collection of applications used by everyone in the company.
The first step in writing the script that will perform this task is to create the Standard Applications folder. This can be accomplished using properties and methods associated with the VBScript run-time FileSystemObject object.
The first step in working with the FileSystemObject is to instantiate it, as demonstrated below.
Set FsoObject = CreateObject("Scripting.FileSystemObject")
Once this is done, you can access all of its properties and methods. You can then use the FileSystemObject object's CreateFolder() method to create the Standard Applications folder. However, before doing so, it is always a good idea to first verify that the folder does not already exist. You can do this using the FileSystemObject object's FolderExists() method. The following VBScript statements demonstrate how to test for the existence of the Standard Applications folder and how to create it if it does not already exist.
Set FsoObject = CreateObject("Scripting.FileSystemObject") If (fsoObject.FolderExists("D:Standard Applications") = false) Then Set StndAppsFolder = fsoObject.CreateFolder("D:Standard Applications") End If
Once the Standard Applications folder is created, you may add shortcuts to it. The following VBScript statements can be added to the end of the previous example to add a shortcut for the Notepad application to the Standard Applications folder.
Set WshShl = WScript.CreateObject("WScript.Shell") Set NotepadShortcut = WshShl.CreateShortcut(StndAppsFolder & "\Notepad.lnk") NotepadShortcut.TargetPath = "%windir% otepad.exe" NotepadShortcut.Save()
The chapter has now covered all of the building blocks required to create the VBScript that creates and populates the Standard Applications folder for ABC, Inc., which is shown below.
'************************************************************************* 'Script Name: Script 12.3.vbs 'Author: Jerry Ford 'Created: 02/22/03 'Description: This script creates a Standard applications folder and 'populates it with a collection of application shortcuts '************************************************************************* 'Initialization Section Option Explicit Dim FsoObject, WshShl, StndAppsFolder, ModelShortcut Set FsoObject = CreateObject("Scripting.FileSystemObject") Set WshShl = WScript.CreateObject("WScript.Shell") 'Main Processing Section CreateStndAppsFolder() PopulateWinZipShortcut() PopulatePaintShopProShortcut() PopulateAdobeAcrobatReaderShortcut() PopulateWS_FTPShortcut() PopulateMSWordShortcut() WScript.Quit() 'Procedure Section 'Look for and if necessary create the Standard Applications folder Sub CreateStndAppsFolder() If (fsoObject.FolderExists("C:Standard Applications") = false) Then Set StndAppsFolder = fsoObject.CreateFolder("C:Standard Applications") Else fsoObject.DeleteFolder("C:Standard Applications") Set StndAppsFolder = fsoObject.CreateFolder("C:Standard Applications") End If End Sub 'Create and add a WinZip shortcut Sub PopulateWinZipShortcut() Set ModelShortcut = WshShl.CreateShortcut(StndAppsFolder & _ "\WinZip.lnk") ModelShortcut.TargetPath = "C:Program FilesWinZipWinZip32.exe" ModelShortcut.Save() End Sub 'Create and add a Paint Shop Pro shortcut Sub PopulatePaintShopProShortcut() Set ModelShortcut = WshShl.CreateShortcut(StndAppsFolder & _ "\Paint Shop Pro.lnk") ModelShortcut.TargetPath = "C:Program FilesPaint Shop ProPSP.exe" ModelShortcut.Save() End Sub 'Create and add a Adobe Acrobat Reader shortcut Sub PopulateAdobeAcrobatReaderShortcut() Set ModelShortcut = WshShl.CreateShortcut(StndAppsFolder & _ "\Adobe Acrobat Reader.lnk") ModelShortcut.TargetPath = _ "C:Program FilesAdobeAcrobat 5.0ReaderAcroRd32.exe" ModelShortcut.Save() End Sub 'Create and add a WS_FTP LE shortcut Sub PopulateWS_FTPShortcut() Set ModelShortcut = WshShl.CreateShortcut(StndAppsFolder & _ "\WS_FTP LE.lnk") ModelShortcut.TargetPath = "C:Program FilesWS_FTPWS_FTP95.exe" ModelShortcut.Save() End Sub 'Create and add a MS Word shortcut Sub PopulateMSWordShortcut() Set ModelShortcut = WshShl.CreateShortcut(StndAppsFolder & _ "\Microsoft Word.lnk") ModelShortcut.TargetPath = _ "C:Program FilesMicrosoft OfficeOfficeWinword.exe" ModelShortcut.Save() End Sub
The script's Initialization Section defines the variables and objects that it will use. The Main Processing Section executes a series of procedure calls before finally terminating the script execution using the WScript object's Quit() method.
The first procedure executed in the Main Processing Section is the CreateStndAppsFolder() subroutine. It uses the FileSystemObject object's FolderExists() method to determine whether or not the Standard Applications folder already exists. If it does not, then it is created using the CreateFolder() method. If the folder does already exist, then it is deleted using the DeleteFolder() method and then recreated, effectively replacing the contents of the folder. The rest of the VBScript's procedures create and add various application shortcuts to the Standard Applications folder.
The Start menu is organized as a series of folders with the Start menu as the top of the menu hierarchy. Underneath it is the All Programs menu, which by default contains the Programs folder and shortcuts to several Windows applications, as shown in Figure 12.6.
Figure 12.6: Examining the folders and shortcuts stored in the Programs special folder
Each folder located under the Programs folder serves as a menu when viewed from the Start menu, and each shortcut stored on one of these folders serves a menu option. By adding a shortcut to the Standard Applications folder from the Programs menu, you can present users with easy access to their most commonly used applications.
In order to programmatically add menus and menu items to the Start menu's All Programs menu, you first need to know how to access it. The following VBScript statements demonstrate how to add a menu entry for the Notepad application on the All Programs menu located on the Start menu.
Set WshShl = WScript.CreateObject("WScript.Shell") StartMenuFolder = WshShl.SpecialFolders("StartMenu") Set NotepadShortcut = WshShl.CreateShortcut(StartMenuFolder & _ "\notepad.lnk") NotepadShortcut.TargetPath = "%windir% otepad.exe" NotepadShortcut.Save
In similar fashion, the following VBScript statements demonstrate how to create a Standard Applications folder, add it to the All Programs menu as a submenu, and then add a Notepad shortcut to it.
Set WshShl = WScript.CreateObject("WScript.Shell") StartMenuFolder = WshShl.SpecialFolders("StartMenu") Set FsoObject = CreateObject("Scripting.FileSystemObject") Set StndAppsFolder = fsoObject.CreateFolder(StartMenuFolder & _ "Standard Applications") Set NotepadShortcut = WshShl.CreateShortcut(StartMenuFolder & _ "Standard ApplicationsNotepad.lnk") NotepadShortcut.TargetPath = "%windir% otepad.exe" NotepadShortcut.Save
Using the information and examples presented in this chapter, you now have everything that you require to develop a VBScript for ABC, Inc. This VBScript takes the Standard Applications folder developed earlier in the chapter and adds it to the All Programs menu under the Start menu, as shown below.
'************************************************************************* 'Script Name: Script 12.4.vbs 'Author: Jerry Ford 'Created: 02/23/03 'Description: This script adds a shortcut to the Standard applications 'folder on the Windows XP All Programs menu '************************************************************************* 'Initialization Section Option Explicit Dim FsoObject, WshShl, StartMenuFolder, StndAppsFolder, StdAppsShortcut Set FsoObject = CreateObject("Scripting.FileSystemObject") Set WshShl = WScript.CreateObject("WScript.Shell") 'Main Processing Section ModifyAllProgramsMenu() WScript.Quit() 'Procedure Section 'Add the Standard Applications folders to the All Programs menu Sub ModifyAllProgramsMenu() If (FsoObject.FolderExists("C:Standard Applications") = false) Then MsgBox "Unable to modify All Programs menu - Standard " & _ "Applications folder not found" Else StartMenuFolder = WshShl.SpecialFolders("StartMenu") Set StdAppsShortcut = WshShl.CreateShortcut(StartMenuFolder & _ "\Standard Applications.lnk") StdAppsShortcut.TargetPath = "C:Standard Applications" StdAppsShortcut.Save End If End Sub
The core logic in this script resides in the ModifyAllProgramsMenu() subroutine. It uses the FileSystemObject object's FolderExists() method to verify that the Standard Applications folder exists. If it does not exist, an error message is displayed. Otherwise a shortcut for the folder is added to the Start menu folder, making it appear under the All Programs menu.
Figure 12.7 demonstrates how the Standard Applications folder will appear once added to the Programs folder belonging to the Start menu.
Figure 12.7: Using the Standard Applications folder to add a new menu under the All Programs menu
Another way to provide users with quick access to applications is to add shortcuts to them on the Quick Launch toolbar. The Quick Launch toolbar resides on the Windows taskbar. It provides single-click access to any application that is added to it. By default, Windows XP enables the Quick Launch toolbar and places several icons on it. These icons include those representing Internet Explorer, Outlook Express, and the Windows desktop.
In order to programmatically administer the Quick Launch toolbar, you need to work with the AppData special folder. The process of adding shortcuts to the Quick Launch toolbar is a little different than the process of adding them to the Start menu. To add a shortcut to the Quick Launch toolbar, you must specify a reference to the Quick Launch toolbar, which is located within the AppData special folder. The following VBScript statements demonstrate how to add a shortcut for the Notepad application to the Quick Launch toolbar.
Set WshShl = WScript.CreateObject("WScript.Shell") QuickLaunchToolbar = WshShl.SpecialFolders("AppData") ApplicationPath = _ QuickLaunchToolbar + "MicrosoftInternet ExplorerQuick Launch" Set QuickLaunchShortcut = _ WshShl.CreateShortcut(ApplicationPath + "\notepad.lnk") QuickLaunchShortcut.TargetPath = "%windir% otepad.exe " QuickLaunchShortcut.Save
By expanding on the previous example, you can create the VBScript needed to populate the Quick Launch toolbar for the new computers at ABC, Inc.
'************************************************************************* 'Script Name: Script 12.5.vbs 'Author: Jerry Ford 'Created: 02/23/03 'Description: This script adds shortcuts to the Quick Launch Toolbar '************************************************************************* 'Initialization Section Option Explicit Dim WshShl, QuickLaunchToolbar, ApplicationPath, QuickLaunchShortcut Set WshShl = WScript.CreateObject("WScript.Shell") 'Main Processing Section ModifyQuickLaunchToolbar() WScript.Quit() 'Procedure Section 'Add application shortcuts to the Quick Launch Toolbar Sub ModifyQuickLaunchToolbar() QuickLaunchToolbar = WshShl.SpecialFolders("AppData") ApplicationPath = _ QuickLaunchToolbar + "MicrosoftInternet ExplorerQuick Launch" Set QuickLaunchShortcut = _ WshShl.CreateShortcut(ApplicationPath + "\WinZip.lnk") QuickLaunchShortcut.TargetPath = "d:Program FilesWinZipWinzip32.exe " QuickLaunchShortcut.Save Set QuickLaunchShortcut = _ WshShl.CreateShortcut(ApplicationPath + "\Calculator.lnk") QuickLaunchShortcut.TargetPath = "%SystemRoot%System32calc.exe" QuickLaunchShortcut.Save End Sub
Figure 12.8 shows how the Quick Launch toolbar looks after the two shortcuts have been added to it.
Figure 12.8: Adding shortcuts to the Windows Quick Launch toolbar
This chapter showed you how to use the WshShortcut object and the VBScript run-time FileSystemObject to modify menus and menu options on the Start menu. You learned how to configure a variety of shortcut properties. This chapter also showed you how to manage the configuration of the Quick Launch toolbar. Tom will be able to use the information presented in this chapter to write VBScripts that he'll use to customize the new Windows XP computers being deployed at ABC, Inc.
Part I - Introducing Microsoft VBScriptBasics
Part II - Professional Project 1 Desktop Administration Using VBScript and the WSH
Part III - Professional Project 2 Analyzing Application Logs
Part IV - Professional Project 3 Creating a Centralized Report Management Station
Part V - Professional Project 4 Reporting Application Summary Data via the Web
Part VI - Introducing Microsoft VBScriptBasics