While preparing the installer for the Web 2.0 Starter Toolkit for IBM DB2, I had to set up Start Menu shortcuts. The way to do that is to work through the Windows Scripting Host (WSH).
The WSH supports two built-in languages – VBScript and Jscript – and a theoretical number of third-party alternatives. VBScript is the better documented of the two in terms of examples, but I find its syntax ugly and constrained. Fortunately, Jscript can do anything VBScript can.
So here’s how we can create Start Menu shortcuts with Javascript.
Locate the Start Menu Programs folder
Most interesting Windows folders can be found by working with special folders
var shell = new ActiveXObject("WScript.Shell"); var startmenu = shell.SpecialFolders("Programs");
The shell object will be reused in code below, but you can redeclare it every time if you like.
Create a Folder
Scripting Guy has more details
var folder = "My App"; var group = startmenu + "\" + name; var fso = new ActiveXObject("Scripting.FileSystemObject"); if (!fso.FolderExists(folder)) fso.CreateFolder(folder);
startmenu
is defined above.
Create an LNK Shortcut
The very hidden file extension of standard Windows shortcuts is LNK. This distinguishes them from website shortcuts, which have the equally hidden extension of URL.
If you are interested in something closer to the symbolic links of Unix, the NTFS equivalent is called junctions. You may find Junction Link Magic of interest.
var name = "My Shortcut"; var file = "myfile.txt"; var path = "C:\Program Files"; var shortcut = shell.CreateShortcut(group + "\" + name + ".lnk"); shortcut.TargetPath = path + "\" + file; shortcut.WorkingDirectory = path; shortcut.Save();
See the full list of properties. I recommend always setting the working directory for application links. If you don’t, your program won’t be able to load resources from it’s installation folder.
shell
and group
are defined above.
Create a URL Shortcut
This is very similar. The main difference is that you set the extension to URL.
var name2 = "My Other Shortcut"; var address = "http://example.org/"; var shortcut = shell.CreateShortcut(group + "\" + name2 +".url"); shortcut.TargetPath = address; shortcut.Save();
See the full list of properties.
shell
and group
are defined above.
Locate Program Files
When creating shortcuts, it is often useful to know where a user’s Program Files directory is located. It is called different things in different versions of Windows, and some advanced users like to move it or rename it.
var REG_PF = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir"; var progFiles = null; var process = shell.Environment("PROCESS"); if (process) progFiles = process("ProgramFiles"); if (!progFiles) progFiles = shell.RegRead(REG_PF);
shell
is defined in the first code excerpt.