Customizing File Associations
File associations control the following aspects of how Windows treats files:
Which icon Windows displays for a file
Which application launches when users double-click files
How Windows Explorer displays particular types of files
Which commands appear on files' shortcut menus
Other features, such as what displays in InfoTips
Appendix A, “File Associations,” describes file associations in detail. In that chapter, you also learn how to customize file associations using methods that only programmers know–until now. Because Appendix A gives a full discussion of file associations and the root key that contains them, HKCR, 'm not going to duplicate that material here. I thought you'd have more fun with some specific file association customizations that I like, such as adding Tweak UI to the My Computer shortcut menu or opening a command prompt window at a particular folder.
File Associations in the Registry
Appendix A, “File Associations,” is the place to go to learn about file associations in HKCR, but this section will help you use the hacks you see in this chapter. Look at Figure 4-4, which shows how Windows chooses what to display on a file's shortcut menu.
Figure 4-4 A file extension key's default value indicates the program class with which it's associated. The program class's shell subkey contains commands you see on the shortcut menu.
In Figure 4-4, you see the keys that Windows consults when you right-click a text file and then click Open. First, the operating system looks up the file extension in HKCR. The default value, shown in Figure 4-4, indicates that the program class associated with the .txt file extension is txtfile. So the operating system looks in HKCR\txtfile for the subkey shell to find the commands that it adds to the shortcut menu. For example, as shown in Figure 4-4, Windows adds Open to the shortcut menu, and when users choose Open, it runs the command in the command subkey.
The command in the command subkey is usually “program” options “%1”, where program is the path and file name of the program you want to run. If you're using a script and you change the default value of command to REG_EXPAND_SZ, you can use environment variables like %SystemRoot% in it. Otherwise, use an explicit path. You use the %1 as a placeholder for the target file. Windows will add the path and name of the target file to the end of the command by default, but you don't want to leave this up to chance. Also, always enclose %1 in quotation marks in case the target path and file name include spaces.
You often see this same shell subkey in class registrations, too. For example, the class registration for My Computer contains a command for managing the computer. The class registration for Recycle Bin contains commands for emptying and exploring its contents.
Running Programs from My Computer
I like any customization that makes things easier. There are some programs that I use over and over again, and I want an easy place from which to run them. The Quick Launch toolbar is a good place, as is the list of frequently used programs on the Start menu. I also want a place where I can put system-oriented commands, though, so I put those on My Computer's shortcut menu. Then I can display the My Computer icon on the desktop and they're one mouse-click away. You learned how to show the icon in the section “Hiding Desktop Icons.”
To add commands to My Computer's shortcut menu, edit its class registration, which is in HKCR\CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}. Add the command to this key's shell subkey. For example, after installing Microsoft Tweak UI, which you learn about in Chapter 5, “Mapping Tweak UI,” I like to add to My Computer's shortcut menu a command that opens Tweak UI. So I add the branch tweak\command to My Computer's class registration. I set the default value of tweak to Tweak UI, the menu item text, and the default value of command to C:\Windows\System32\Tweakui.exe, the path and file name of Tweak UI. After customizing the class registration for My Computer, starting Tweak UI is fast: right-click My Computer, and then click Tweak UI.
The following INF file automates this setting. First, install Tweak UI. Then save this script to the file Tweakui.inf, right-click the file, and then click Install. (Again, you can download these sample scripts from http://www.honeycutt.com.) See Chapter 11, “Scripting Registry Changes,” for other ways to script this hack. You can uninstall these settings in Add Or Remove Programs.
Listing 4-2 Tweakui.inf
[Version] Signature=$CHICAGO$ [DefaultInstall] AddReg=Reg.Settings AddReg=Reg.Uninstall CopyFiles=Inf.Copy [DefaultUninstall] DelReg=Reg.Settings DelReg=Reg.Uninstall DelFiles=Inf.Copy [Reg.Settings] HKCR,CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}\shell\tweak HKCR,CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}\shell\tweak,,,"%MENUITEM%" HKCR,CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}\shell\tweak\command\ ,,0x20000,"%SYSTEMROOT%\System32\Tweakui.exe" [Reg.Uninstall] HKLM,Software\Microsoft\Windows\CurrentVersion\Uninstall\%NAME% HKLM,Software\Microsoft\Windows\CurrentVersion\Uninstall\%NAME%,DisplayName\ ,,"%NAME%" HKLM,Software\Microsoft\Windows\CurrentVersion\Uninstall\%NAME%,UninstallString\ ,,"Rundll32.exe setupapi.dll,InstallHinfSection DefaultUninstall 132"\ " %17%\Tweakui.inf" [Inf.Copy] Tweakui.inf [DestinationDirs] Inf.Copy=17 [SourceDisksNames] 55=%DISKNAME% [SourceDisksFiles] Tweakui.inf=55 [Strings] NAME = "Jerry's Tweak UI Shortcut" MENUITEM = "Tweak UI" DISKNAME = "Setup Files"
You can add any command to any shortcut menu, and that command doesn't have to edit, print, or do anything at all with the menu's target. My Computer is a good place to put system-oriented commands like Tweak UI, but you could also put them on the shortcut menu of another object, such as Recycle Bin, if you don't display the My Computer icon on the desktop.
Opening Command Prompts at Targeted Folders
Another favorite customization, and the one I probably use the most, enables me to quickly open a command prompt window with the targeted folder set as the current working directory. I add the command C:\WINDOWS\System32\cmd.exe /k cd "%1" to the Directory and Drive program classes. Then I right-click a folder and click CMD Prompt Here to open a command prompt with that folder set as the current working directory. This saves a lot of time. Here are the settings to add to HKCR\Directory and HKCR\Drive.
In HKCR\Directory\shell, create the subkey cmdhere.
In HKCR\Directory\shell\cmdhere, set the default value to CMD Prompt Here. This is the text you'll see on the shortcut menu.
In HKCR\Directory\shell\cmdhere, create the subkey command.
In HKCR\Directory\shell\cmdhere\command, set the default value to C:\Windows\System32\cmd.exe /k cd "%1".
The following script automatically adds this command to the Directory and Drive program classes. Save it to the text file Cmdhere.inf, right-click it, and then click Install. To understand how this script works, see Chapter 11, “Scripting Registry Changes.” Remove these settings using Add Or Remove Programs.
Listing 4-3 Cmdhere.inf
[Version] Signature=$CHICAGO$ [DefaultInstall] AddReg=Reg.Settings AddReg=Reg.Uninstall CopyFiles=Inf.Copy [DefaultUninstall] DelReg=Reg.Settings DelReg=Reg.Uninstall DelFiles=Inf.Copy [Reg.Settings] HKCR,Directory\Shell\Cmdhere HKCR,Directory\Shell\Cmdhere,,,"%MENUITEM%" HKCR,Directory\Shell\Cmdhere\command,,,"%11%\cmd.exe /k cd ""%1""" HKCR,Drive\Shell\Cmdhere HKCR,Drive\Shell\Cmdhere,,,"%MENUITEM%" HKCR,Drive\Shell\Cmdhere\command,,,"%11%\cmd.exe /k cd ""%1""" [Reg.Uninstall] HKLM,Software\Microsoft\Windows\CurrentVersion\Uninstall\%NAME% HKLM,Software\Microsoft\Windows\CurrentVersion\Uninstall\%NAME%,DisplayName\ ,,"%NAME%" HKLM,Software\Microsoft\Windows\CurrentVersion\Uninstall\%NAME%,UninstallString\ ,,"Rundll32.exe setupapi.dll,InstallHinfSection DefaultUninstall 132 "\ "%17%\Cmdhere.inf" [Inf.Copy] Cmdhere.inf [DestinationDirs] Inf.Copy=17 [SourceDisksNames] 55=%DISKNAME% [SourceDisksFiles] Cmdhere.inf=55 [Strings] NAME = "Jerry's CMD Prompt Here" MENUITEM = "CMD &Prompt Here" DISKNAME = "Setup Files"
Rooting Windows Explorer at a Targeted Folder
This customization opens Windows Explorer without all the usual clutter, so you can focus on a single folder. Add the command Explorer.exe /e,/root,/idlist,%I to the Folder program class's shell subkey. Then right-click any folder, choose the command you added, and another Windows Explorer window opens with that folder rooted at the top of the left pane. Here are the settings you add to the Folder program class to target Windows Explorer at a particular folder:
In HKCR\Folder\shell, create the subkey fromhere.
In HKCR\Folder\shell\fromhere, set the default value to Explore from Here. This is the text you'll see on the shortcut menu.
In HKCR\Folder\shell\fromhere, create the subkey command.
In HKCR\Folder\shell\fromhere\command, set the default value to Explorer.exe /e,/root,/idlist,%I.
The following script automatically adds this command to the Folder program class. Save it to the text file Fromhere.inf, right-click it, and then click Install. To understand how this script works, see Chapter 11, “Scripting Registry Changes.” Remove these settings using Add Or Remove Programs.
Listing 4-4 Fromhere.inf
[Version] Signature=$CHICAGO$ [DefaultInstall] AddReg=Reg.Settings AddReg=Reg.Uninstall CopyFiles=Inf.Copy [DefaultUninstall] DelReg=Reg.Settings DelReg=Reg.Uninstall DelFiles=Inf.Copy [Reg.Settings] HKCR,Folder\shell\fromhere HKCR,Folder\shell\fromhere,,,"%MENUITEM%" HKCR,Folder\shell\fromhere\command,,,"explorer.exe /e,/root,/idlist,%I" [Reg.Uninstall] HKLM,Software\Microsoft\Windows\CurrentVersion\Uninstall\%NAME% HKLM,Software\Microsoft\Windows\CurrentVersion\Uninstall\%NAME%\ ,DisplayName,,"%NAME%" HKLM,Software\Microsoft\Windows\CurrentVersion\Uninstall\%NAME%\ ,UninstallString,,"Rundll32.exe setupapi.dll,InstallHinfSection DefaultUninstall 132 "\ "%17%\Fromhere.inf" [Inf.Copy] Fromhere.inf [DestinationDirs] Inf.Copy=17 [SourceDisksNames] 55=%DISKNAME% [SourceDisksFiles] Fromhere.inf=55 [Strings] NAME = "Jerry's Explore from Here" MENUITEM = "Explore from &Here" DISKNAME = "Setup Files"
Adding InfoTips to Program Classes
InfoTips are the balloons that users see when the mouse pointer hovers over an object. I like InfoTips; you might not. Position the mouse pointer over an object in the user interface, and Windows displays the InfoTip associated with it in a small yellow box. For documents, the InfoTip typically includes the type of document, the date of its last modification, its size, and more. You can customize InfoTips to display further information, though.
Windows uses the REG_SZ value InfoTip to display InfoTips. The operating system uses this value, which it finds in the class registration or the program class to which the object belongs. For example, if you position the mouse pointer over a file with the .doc file extension, Windows looks in the associated program class Wordpad.Document.1 for the value InfoTip. If it doesn't find the value there, it uses the value InfoTip that it finds in HKCR\*. The default value of that is prop:Type;DocAuthor;DocTitle; DocSubject;DocComments;Write;Size.
Thus, you can customize individual classes or create an InfoTip that applies to all classes. If you want a specific object or file type, add the REG_SZ value InfoTip to that specific class registration or program class. Otherwise, customize the value InfoTip in HKCR\* to see that tip for all file classes.
So what does all that mean? The notation prop:name indicates to Windows that it should use the document property name in the InfoTip. Thus, the value you just saw means that Windows should display the document properties Type, DocAuthor, DocTitle, DocSubject, DocComments, Write, and Size in the InfoTip. You can also set InfoTip to the exact string that you want Windows to display when users position the mouse pointer over objects of that particular class. For example, you can set InfoTip for the txtfile program class to This is a text file, and Windows displays that text when users position the mouse pointer over text files. Windows ignores any property in the InfoTip that the document doesn't define, and InfoTips can be up to six lines long. Available properties depend on each individual program class. The following list shows just some of the document properties that you can add to an InfoTip:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NOTE
A related value is TileInfo. The contents of this value are the same as the contents of InfoTip. Windows displays TileInfo next to icons in Tile view. You're limited to two lines, however, so make good use of the space you have. I usually prefer to display more useful information in the default value of TileInfo, such as a file's attributes. Thus, I like to set the value HKCR\*\TileInfo to prop:Type;Attributes, because it is more useful to me. Although you don't have to log off and back on to Windows to see changes you make to InfoTip, you do have to log off of Windows to see changes you make to TileInfo.
Customizing Folders with Desktop.ini
This chapter shows you how to customize files and other objects you see in the user interface, but customizing individual folders is useful, too. For example, there might be a folder in My Documents that you want to stand out from others.
You do that by creating the file Desktop.ini in the folder. There are numerous ways to customize folders by using this file, but the two most interesting are setting unique icons for folders and displaying InfoTips when users position the mouse pointer over them. In this sample Desktop.ini file, the value IconFile points to the file containing the icon that I want Windows Explorer to display for the folder. The value IconIndex is the index number of the icon, starting with 0, which is the first icon in the file. If you use an icon file instead of a DLL or EXE file, put the path of the file in IconFile, and set IconIndex to 0. InfoTip is the text that I want Windows Explorer to display when I position the mouse pointer over the folder.
[.ShellClassInfo] IconFile=C:\Windows\Regedit.exe IconIndex=0 InfoTip="Manuscripts for my latest registry book."
Set the Desktop.ini file's Hidden And System attribute by typing the command attrib +s +h filename in the Run dialog box. You set the folder's System attribute by typing the command attrib +s foldername in the Run dialog box. Figure 4-5 shows what the folder Registry Book looks like after you add this Desktop.ini file to it and set the file and folder's attributes. Now, whenever I position the mouse pointer over the folder, I am reminded of the important task at hand.
Figure 4-5 When I hold the mouse pointer over the Registry Book folder, I see the text “Manuscripts for my latest registry book.”