February 7th, 2013

How can I register my context menu command for all file types *except* one, or other complex conditionals?

We saw that you can register your context menu under * to make it apply to all files. But what if you want it to apply to all files except one? For example, your command might be “Convert to Widget” but you don’t want it to appear for .widget files because that would be redundant.

I mentioned how to do this as an afterthought in an earlier discussion of advanced context menu registration, but I’m going to elevate to its own topic because it’s probably even more useful than the base article!

Starting in Windows 7, you can conditionalize your context menu declaratively. This is explained in the documentation for context menus in the section Getting Dynamic Behavior for Static Verbs by Using Advanced Query Syntax. Let’s try it out. Of course, I don’t actually have a “Widgetizer” program, so I’ll just use Calc.

[HKEY_CLASSES_ROOT\*\shell\Widgetize]
"AppliesTo"="NOT System.FileExtension:=.widget"

[HKEY_CLASSES_ROOT\*\shell\Widgetize\Command]
@="calc.exe"

The Widgetize command appears only for files whose extension is not .widget.

You have access to the shell property system and Advanced Query Syntax here, so you can create more complex conditionals. Here’s how you can Widgetize only files that are smaller than 32KB, and ignore zero-byte files.

"AppliesTo"="System.Size:1..32kb AND NOT System.FileExtension:=.widget"
Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.