Windows脚本编程核心技术精解Chapter09.pdf
《Windows脚本编程核心技术精解Chapter09.pdf》由会员分享,可在线阅读,更多相关《Windows脚本编程核心技术精解Chapter09.pdf(32页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 9Mastering LinksIn This Chapter?Learn more about the power of shortcut files?Createand modify shortcut files by script?Search for invalid links and duplicating keyboard shortcuts?Change shortcut icons dynamically?Preselect a shortcuts window size?Use the hidden Shell Linkobject to resolve br
2、oken links?Discover the undocumented link resolving mechanism and the new Windows 2000 Distributed Link Tracking Service?Open a shortcut targets Properties page?Mark a shortcut target file in the ExplorerNow that you know how to handle your file system,take a look atlink files.In this chapter,you le
3、arn what link files are and how touse them to customize menus.You also learn how to create new linkfiles from scratch and how to check existing link files for errors.Usingsecret API functions,you can even automatically repair broken link files.Why Do I Need Links Anyway?Links are small but extremely
4、 powerful files.They represent files,folders,and system objects located somewhere else.Links build the foundation ofcustomizing Windows.Links make the Programs menu work.To see howimportant links really are,start a search for*.LNK.Scripts can use links to place themselves in important places.You can
5、 add your script to the Start menu,place it into the Send To folder,or insert a reference into the Startup folder.In addition,scripts can use their link functions for maintenance.Scripts can search for links,checkwhether their destinations are still valid,and sort out any broken links.4684-8 ch09.f.
6、qc 3/3/00 9:35 AM Page 289More importantly,scripts can check the internal keyboard shortcuts and makesure there are no duplicate entries.Windows doesnt check for this,allowingyou to assign multiple keyboard shortcuts to more than one link file,or even to override important built-in Windows shortcuts
7、resulting in erroneousbehavior.By using some undocumented Windows magic,you can even resolve brokenlinks and mark files and folders.Creating a new link fileThe WScript.Shellobject contains all the methods needed for managinglinks.The following example asks for a name and places a shortcut to theWind
8、ows editor right on your desktop:9-1.VBSset wshshell=CreateObject(“WScript.Shell”)desktop=wshshell.SpecialFolders(“desktop”)linkname=InputBox(“What do you want to call your new link?”,_,”Windows Editor”)target=“notepad.exe”linkname=desktop&“”&linkname&“.lnk”create new shortcut objectset scut=wshshel
9、l.CreateShortcut(linkname)fill in informationscut.TargetPath=target save shortcut objectscut.SaveMsgBox“Done-new shortcut on your desktop!”Take a look at your desktop.The script adds a new shortcut and,when youopen it,launches the editor.Examining the shortcut objectAll this magic was done with the
10、help of a shortcut object.A shortcut objectrepresents the internal properties of a link file.CreateShortcutis yoursecret key to the shortcut object.It always returns a shortcut object.If youspecified a link file that doesnt yet exist,the new shortcut object is empty.However,you can easily access the
11、 hidden properties of existing shortcuts.Just specify an existing shortcut file path.290Part II:Conquering the File SystemII4684-8 ch09.f.qc 3/3/00 9:35 AM Page 290CreateShortcutwill fail if the specified file name doesnt carry the.linkfile extension.The resulting shortcut object is not the actual s
12、hortcut.Itsjust an information object stored somewhere in memory.To write theproperties of the shortcut object back to the actual shortcut file,you mustcall the objects Savemethod.Table 9-1 shows how shortcut objects are organized.Table 9-1The Shortcut ObjectProperty/MethodDescriptionProperty Argume
13、nts As StringAdditional command-line argumentsyou want to pass to the target fileProperty Description As StringOptional descriptive informationabout what this shortcut is used forProperty FullName As StringFull path name of the link file(undocumented)Property Hotkey As StringKeyboard shortcutPropert
14、y IconLocation Display icon of the shortcutAs StringProperty RelativePath Not fully supported(undocumented)As StringSub SaveSaves the information to theshortcutProperty TargetPath As StringPath name of the shortcut target fileProperty WindowStyle As LongType of window the shortcut should openPropert
15、y WorkingDirectory Initial directory settingAs StringAccessing Existing Link FilesCreateShortcutis somewhat misleading,as it doesnt create a shortcut file.It creates a shortcut object.You can easily manage existing link files usingCreateShortcutand reveal their internal properties.Searching for inva
16、lid linksThe next script checks your entire Programs menu and reports any invalidlinks.Figure 9-1 shows the result.Chapter 9:Mastering Links291II4684-8 ch09.f.qc 3/3/00 9:35 AM Page 291For your convenience,the script offers to delete invalid links.Before youdelete something,always think twice.Shortc
17、ut targets on network drives ortargets to disk drives may not currently be available but may still be useful.Check the message dialog box carefully before you decide to clean up yourPrograms menu!9-2.VBSset wshshell=CreateObject(“WScript.Shell”)set fs=CreateObject(“Scripting.FileSystemObject”)global
18、 variablesnum_checked=0num_error=0num_deleted=0programs=wshshell.SpecialFolders(“programs”)set folderobj=fs.GetFolder(programs)CheckFolder folderobjmsg=“I have checked“&num_checked&“links.”&vbCrmsg=msg&“I found“&num_error&“invalid links.”&vbCrmsg=msg&“You asked me to delete“&num_deleted&“links”MsgBo
19、x msg,vbInformationsub CheckFolder(folderobj)for each file in folderobj.filesext=lcase(fs.GetExtensionName(file.name)if ext=“lnk”thennum_checked=num_checked+1set scut=wshshell.CreateShortcut(file.path)target=scut.TargetPathif not fs.FileExists(target)thennum_error=num_error+1msg=“Link“”&file.path&“”
20、points to“”_&target&“”&vbCrmsg=msg&“The target no longer exists!Do“_&“you want to delete the link?”response=MsgBox(msg,vbQuestion+vbYesNo _+vbDefaultButton2)if response=vbYes thennum_deleted=num_deleted+1file.deleteend ifend ifend ifnextfor each subfolder in folderobj.subfoldersCheckFolder subfolder
21、nextend sub292Part II:Conquering the File SystemII4684-8 ch09.f.qc 3/3/00 9:35 AM Page 292Figure 9-1:Check for invalid and outdated links in your Programs menu.Finding(and eliminating)duplicate keyboard shortcutsKeyboard shortcuts are extremely usefulafter all,any shortcut file with akeyboard shortc
22、ut can be invoked just by pressing the keys you specify.Justright-click on a link file and choose Properties.Then enter your keyboardshortcut in the shortcut text field and click OK.But,this doesnt always work!Keyboard shortcuts can launch files only if the shortcut is located either onyour desktop
23、or inside the Programs menu.These are the only places Windowslooks for keyboard shortcuts.It would just take much too long to check all linkfiles anywhere in the file system.One of the most annoying reasons for failure is duplicate shortcut keys.Windowsdoesnt make any effort to prevent you from assi
24、gning new keyboard shortcutsthat are already in use by some other link file.Once you have multiple link filesusing the same key combination,its up to Windows which file will be launched.Even worse,its possible to override Windows internal shortcuts.If youassign F1 to a link file,this key will no lon
25、ger invoke Windows help.Its very hard to resolve duplicate keyboard shortcuts because Windows cantlist the shortcuts in use.You would have to open the Properties page of eachand every link file manually to look for shortcuts.Thats too much work.Thefollowing script creates a shortcut list automatical
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows 脚本 编程 核心技术 Chapter09
限制150内