August 29, 2007 Tools

Introducing Shell Glue

Shell Glue is aiStock_000001915932XSmall Windows Explorer Shell Extension, more specifically to add context menu items to files and folders.  What menu items and sub items are added to the context menu is controlled through a configuration file.  For a simple example lets create a configuration that will give a menu item that will build solution and or project files with MSBuild when selected.

 SimpleBuild

<?xml version="1.0" encoding="utf-8" ?>
<Settings>
  <Actions>
    <ActionItem>
      <Name>Build</Name>
      <Verb>Build</Verb>
      <Help>Build</Help>
      <TargetListMode>File</TargetListMode>
      <IconFilePath>C:\Program Files\Shell Glue\.Net\msbuild.ico</IconFilePath>
      <ExtentionFilter>
        <string>^.*\.sln$</string>
        <string>^.*\..*proj$</string>
      </ExtentionFilter>
      <ProgramPath>C:\Program Files\Shell Glue\.Net\Build.bat</ProgramPath>
    </ActionItem>
  </Actions>
</Settings>

Here is the batch file contents:

@echo off
set PATH=%PATH%;%windir%\Microsoft.NET\Framework\v2.0.50727
@echo on

FOR /F “tokens=1 delims=” %%i in (%~fs1) do msbuild “%%i”

@echo off
SET /P variable=”Hit Enter to exit.”

And here is the result of selecting the menu item:

CropperCapture[7]

So lets take a look at what config options there are and what they do.  Here is a bogas example of all the config options:

<?xml version="1.0" encoding="utf-16"?>
<ActionItem>
  <Name>Name</Name><!--Menu Item Caption-->
  <Verb>Verb</Verb><!--Optional - Cruft-->
  <Help>Help</Help><!--Optional - HelpString for the Menu Item-->
  <IconFilePath>Path</IconFilePath><!--Optional - Full path to ico file 16x16-->
  <Actions><!--Optional - List of sub menu items-->
    <ActionItem>
      <Name>Child</Name>
      <TargetListMode>File</TargetListMode><!--Optional - Default is File-->
      <ProgramPath>path</ProgramPath><!--Program to be executed-->
      <ExtentionFilter><!--Optional-->
        <string>Regex Pattern</string>
        <string>^.*\.txt$</string>
      </ExtentionFilter>
    </ActionItem>
    <ActionItem>
      <Name>Child2</Name>
      <TargetListMode>CommandLine</TargetListMode>
      <ArgumentsFormat>something {0} /something</ArgumentsFormat><!--Optional-->
      <ProgramPath>path</ProgramPath>
    </ActionItem>
  </Actions>
</ActionItem>

Each menu item is tracked with a key that is a combination of the Name, Verb, and Help strings.  So you can have multiple context menu items with the same Name/Caption.  Each menu item may have an icon, it looks best with a 16X16 icon.  The ProgramPath is the full path to file to be executed.  The TargetListMode controls how the selected items are passed to the program to be executed.  The File mode will write each target file and folder to a temp file and pass the temp file path to the program to be executed.  If you look back at the batch file used in the MSBuild example the FOR loop is iterating over the contents of the temp file.  The only other option for the TargetListMode is CommandLine.  This will pass each target file and folder as a separate argument.  This is a risky choice as you might exceed the length limit for the commandline; still it can be use full.  The ArgumentFormat gives you more control of what the commandline for the program to be executed is.  This is particularly useful if you want to call a console application directly.  The list of targets or the temp file are stored in {0}.  Lastly there are ExtensionFilters.  These allow you to provide regular expressions to narrow what files this menu item will be displayed for.

This config file is located: C:\Program Files\Shell Glue\Settings.xml.

 

I originally start on this to create a poor mans Tortoise for Team Foundation Server(TFS).  Here is an example entry for a menu item to add files and folders to TFS:

<ActionItem>
  <Name>Add</Name>
  <TargetListMode>CommandLine</TargetListMode>
  <ArgumentsFormat>add {0} /recursive</ArgumentsFormat>
  <ProgramPath>C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\TF.exe</ProgramPath>
</ActionItem>

So as this is no where near as nice as Tortoise products are I went with a crab (the theme being shells..).

TFSCrab

 

I have been warned that managed extensions can be problematic. From MSDN Magazine:

[ Editor's Update - 6/23/2006: Because shell extensions are loaded into arbitrary processes and because managed code built against one version of the runtime may not run in a process running an earlier version of the runtime, Microsoft recommends against writing managed shell extensions and does not consider them a supported scenario.]

I really made this for myself.  It did not take long to write.  The Wix installer took significantly longer than the extension.  So I am not worried about the warning for myself, you will have to judged for yourself.

Here is the download link:

ShellGlue-1.0.msi

 

The project is hosted on Google Code so you can play with the code yourself:

http://code.google.com/p/shellglue/source

Please know that it is using a commercial product to make extending the shell easy.  So if you want to play with the code you will need to download Sky Software’s EZShellExtension.Net.

4,229 Total Views

6 to “Introducing Shell Glue”

Trackbacks/Pingbacks

  1. [...] Jay Flowers has written an application, Shell Glue, that is a Windows Explorer Shell Extension.  What it gives you is the ability to add context menu items to files and folders easily.  [...]

  2. [...] Shell Glue – Windows Explorer shell extension that lets you extend context menus based on an XML configuration file. [...]

  1. Customize Windows Explorer Shell - The Easy Way says...

    [...] Jay Flowers has written an application, Shell Glue, that is a Windows Explorer Shell Extension.  What it gives you is the ability to add context menu items to files and folders easily.  [...]

  2. Eugene says...

    Hi Jay,
    Are you sure your site’s favicon.ico of 365Kb size is really necessary? ;) It’s bigger than most of your pages with images ;)

  3. jflowers says...

    Thanks Eugen,
    That is more that a little out of control. :-)

  4. Eugene says...

    Jay, I forgot to tell a word about the program.
    It’s really useful!
    The greatest thing for me that it’s able to work with folders.
    Many thanks!

  5. The Daily Grind 1220 at aoortic! dot com says...

    [...] Shell Glue – Windows Explorer shell extension that lets you extend context menus based on an XML configuration file. [...]

Leave a comment

*

here