Files
osr-mono/packages/osr-code-bot/kbot-extensions/docs/shell-ext/windows-js.md
T
2025-02-01 14:25:34 +01:00

3.2 KiB

Registering custom context menus (like "Open With" options) in Windows using Node.js involves modifying the Windows Registry, as that's where Windows stores configuration data for the context menu.

Below are options and example code snippets that show how to achieve this using Node.js. Remember, modifying the Windows Registry can have significant effects on your system, so proceed with caution and back up the registry before making changes.

Prerequisites

You need to have Node.js installed on your machine and optionally, you might want to use a package that makes interacting with the Windows Registry easier, such as node-winreg.

Option 1: Using the node-winreg package

  1. Install the package:

    npm install winreg
    
  2. Example Code to Add a Context Menu Entry:

    const Registry = require('winreg');
    
    // Define new registry entry
    const regKey = new Registry({
      hive: Registry.HKCR, // HKEY_CLASSES_ROOT
      key: '\\*\\shell\\MyCustomAction' // Use '\\Folder' for folder context
    });
    
    // Set the description of your context menu item
    regKey.set('', Registry.REG_SZ, 'Open with MyApp', (err) => {
      if (err) throw err;
      console.log('Context menu description set.');
    
      // Define the command key to execute
      const commandKey = new Registry({
        hive: Registry.HKCR,
        key: '\\*\\shell\\MyCustomAction\\command'
      });
    
      // Set the command that the context menu item executes
      commandKey.set('', Registry.REG_SZ, '"C:\\path\\to\\your\\app.exe" "%1"', (err) => {
        if (err) throw err;
        console.log('Command set for context menu.');
      });
    });
    

Option 2: Using Windows Registry Command Line (reg.exe)

This method involves executing Windows' built-in registry manipulation tool using Node.js's child_process module.

  1. Example Code to Add a Context Menu Entry:

    const { exec } = require('child_process');
    
    // Define the registry command
    const regAddCommand = `
      reg add HKCR\\*\\shell\\MyCustomAction /ve /d "Open with MyApp" /f &&
      reg add HKCR\\*\\shell\\MyCustomAction\\command /ve /d "\\"C:\\\\path\\\\to\\\\your\\\\app.exe\\" \\"%1\\"" /f
    `;
    
    // Execute the command
    exec(regAddCommand, (error, stdout, stderr) => {
      if (error) {
        console.error(`Error: ${error.message}`);
        return;
      }
      if (stderr) {
        console.error(`Stderr: ${stderr}`);
        return;
      }
      console.log('Context menu entry added successfully.');
    });
    

Important Considerations

  • Permissions: You will need administrative privileges to modify certain parts of the Windows Registry. Running Node.js or Command Prompt as an administrator may be necessary.

  • System Impact: Incorrect changes to the registry can cause system instability. Always back up the registry before making modifications.

  • Uninstalling: When distributing applications, consider providing a way to remove the context menu entries created, perhaps by running a script with similar logic but using reg delete or registry.remove.

These examples provide a basic implementation. Depending on your requirements, you might need to adjust paths and keys or add error handling as needed.