9.5 Creating Windows Shortcuts and Internet Shortcuts in Scripts

9.5 Creating Windows Shortcuts and Internet Shortcuts in Scripts

Include the following subroutine in your script to allow easy creation of Internet Shortcuts (*.url ) and Windows Shortcuts (*.lnk):

Sub Shortcut(LinkFile, CommandLine)   Set WshShell = WScript.CreateObject("WScript.Shell")   If LCase(Right(LinkFile, 4)) <> ".lnk" And _           LCase(Right(LinkFile, 4)) <>".url" Then _          LinkFile = LinkFile & ".LNK"   Set ShortcutHandle = WshShell.CreateShortcut(LinkFile)   ShortcutHandle.TargetPath = CommandLine   ShortcutHandle.Save End Sub

To create a shortcut to a program or file, use the following statement:

Call Shortcut("C:\Documents and Settings\All Users\SendTo\Notepad.lnk", _               "Notepad.exe")

To create a shortcut to an Internet address:

Call Shortcut("D:\Prjects\Important\Annoyances.url", _               "http://www.annoyances.org/")

If the first parameter, LinkFile, ends in .lnk (case doesn't matter), the Shortcut subroutine will automatically create a standard Windows shortcut; if LinkFile ends in .url, however, an Internet Shortcut file will be created. Note the If...Then structure in the routine, which automatically adds the .lnk filename extension if no proper extension is found.

 

The LCase function, which transforms the contents of any variable to lower-case, is vital here, and completely compensates for .URL, .url, .Url, and any other case mismatch in the specified filename.

 

If you specify a nonexistent folder in the path for the new shortcut file, an "Unspecified Error" will occur. You may want to use the FolderExists function, detailed in the Section 9.4 topic earlier in this chapter, to supplement this routine and eliminate the possibility of this error.



Windows XP Annoyances
Fixing Windows XP Annoyances
ISBN: 0596100531
EAN: 2147483647
Year: 2005
Pages: 78
Authors: David A. Karp

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net