Shortcuts, Menus, and Startup Folders

As you know in Windows operating systems, menus, desktops, and startup folders are all configured with shortcuts. Basically, all we need to know is how to manipulate shortcuts in special folders and how to get access to these folders.

To get an access to all these special folders we can either use method GetFolder() of the FileSystemObject as discussed before or use special property SpecialFolders of the WshShell object. This property is a collection of all special folders. To access a particular folder inside the collection we need to provide the name of the folder. The list of all available names with short description is given in the table.
Folder Description
AllUsersDesktop Desktop shortcuts for all users
AllUsersStartMenu   Start menu options for all users
AllUsersPrograms Programs menu for all users
AllUsersStartup Startup applications for all users
Desktop Desktop shortcuts for the current user
Favorites Favorites menu shortcuts for the current user
Fonts Fonts folder shortcuts for the current user
MyDocuments My Documents menu for the current user
NetHood Network neighborhood shortcuts for the current user
PrintHood Printers folder shortcuts for the current user
Programs Programs menu options for the current user
Recent Recently used documents for the current user
SendTo SentTo menu shortcuts for the current user
StartMenu Start menu shortcuts for the current user
Startup Startup applications for the current user
Templates Templates folder shortcuts for the current user
Thus, to get an access to the desktop of the current user we need to do this

var shell = WScript.CreateObject("WScript.Shell");
var desktop = shell.SpecialFolders("Desktop");

Creating a shortcut

To create a shortcut we should use CreateShortcut() method of the WshShell object. This method takes only one argument - a string value indicating the path name of the shortcut to create, and creates new or opens already existing shortcut. As a result this method returns the WshShortcut object.

The WshShortcut object has a set of properties and only one method Save(). We need to use this method when all modifications to an open shortcut are performed.
Property Description
Arguments Arguments to pass to an application started through the shortcut
Description Sets a description for the shortcut
FullName Returns the fully qualified path of the shortcut object's target.
Hotkey Sets a hot key sequence that activates the shortcut. Can only be used with desktop shortcuts and Start menu options.
IconLocation Sets the location of an icon for the shortcut. If not set, a default icon is used. The number after the file name indicates the position of the icon.
TargetPath Sets the path of the file to execute.
WindowStyle Sets the window style of the application started by the shortcut. The default value is 1. The available styles are the same as options 0-6 discussed before.
WorkingDirectory Sets the working directory of the application started by the shortcut
The following example creates a shortcut that starts the Notepad with the argument mynotes.txt. This shortcut has the hotkey sequence "CTRL-ATL-N" and an icon number 5 located in explorer.exe file:

var shell = WScript.CreateObject("WScript.Shell");
var desktop = shell.SpecialFolders("Desktop");
var scut = shell.CreateShortcut(desktop+"\\notes.lnk");
scut.TargetPath = "%windir%\\notepad.exe";
scut.Description = "Things to do today";
scut.Arguments = "c:\\temp\\mynotes.txt";
scut.Hotkey = "CTRL+ALT+N";
scut.IconLocation = "C:\\WINDOWS\\explorer.exe,5";
scut.Save();
In the next example we will create a URL shortcut.
var shell = WScript.CreateObject("WScript.Shell");
var desktop = shell.SpecialFolders("Desktop");
var scut = shell.CreateShortcut(desktop+"\\IST.url");
scut.TargetPath = "http://ist.marshall.edu";
scut.Save();
The same properties can be used to obtain information about a particular shortcut. To retrieve all information about a shortcut, we need to use the same method CreateShortcut() to open it and then the same properties to read all information.
Please write a script that takes a shortcut name as an argument and prints all information about this shortcut.

Moving and deleting shortcuts

While working with shortcuts we need to remember that we are actually working with files with the extension "lnk" located in one of the special folders or in a usual folder. Thus, to move or delete shortcuts we need to use usual file methods of the FileSystemObject or File object to do that.


References: