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

3.3 KiB
Raw Blame History

To create a custom context menu ("Open With") in Ubuntu using Node.js, you can modify desktop entry files and integrate your application into the GNOME or other Linux desktop environments. Here's how you can do it:

Step-by-Step Guide

  1. Create a Desktop Entry File: This file registers your application with the system so you can define how it should appear in the context menu.

  2. Use Node.js to Automate the Setup: Create a Node.js script that writes the necessary desktop entry.

Heres a simple example to register a custom application that opens files with a specific extension using your Node.js script:

Example Code

  1. Create the Script:
const fs = require('fs');
const path = require('path');

// Define your application details
const appName = 'My Custom App';
const execPath = '/usr/bin/my-custom-app'; // Adjust this to your actual executable path
const mimeType = 'text/plain'; // Specify the MIME type your app can handle

// Define the path for the desktop entry
const desktopEntryPath = path.join(process.env.HOME, '.local', 'share', 'applications', 'my-custom-app.desktop');

// Craft the content of the .desktop file
const desktopEntryContent = `
[Desktop Entry]
Name=${appName}
Comment=Open with My Custom App
Exec=${execPath} %U
Terminal=false
Type=Application
MimeType=${mimeType};
`;

function createDesktopEntry() {
  // Ensure the target directory exists
  const dirPath = path.dirname(desktopEntryPath);
  if (!fs.existsSync(dirPath)) {
    fs.mkdirSync(dirPath, { recursive: true });
  }

  // Write the desktop entry file
  fs.writeFileSync(desktopEntryPath, desktopEntryContent.trim(), { mode: 0o644 });
  console.log(`Desktop entry created at: ${desktopEntryPath}`);
}

// Execute the function
createDesktopEntry();
  1. Register MIME Type:
  • Ensure the MIME type is recognized by your system. If your application handles a unique file type, you might need to create a custom MIME type in your system as below.

  • Create a MIME Type XML file (e.g., my-custom-app.xml):

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
    <mime-type type="application/x-my-custom-file">
        <comment>My Custom File</comment>
        <glob pattern="*.myext"/>
    </mime-type>
</mime-info>
  • Install the MIME Type:
xdg-mime install my-custom-app.xml
  • Associate your application with this MIME type:
xdg-mime default my-custom-app.desktop application/x-my-custom-file
  1. Refresh the System Database:

Update the system database using update-desktop-database. Ensure this command is available on your system:

update-desktop-database ~/.local/share/applications

Considerations

  • Permissions: Ensure that the user has sufficient permissions to create and modify files in ~/.local/share/applications.

  • Executable Path: The executable path (/usr/bin/my-custom-app) must point to an actual executable or script you want to run.

  • MimeType: Adjust the mimeType field to match the file types your app should handle.

With these steps, your custom application should appear in the "Open With" context menu for the specified MIME type files. You can further test, enhance, and refine the script according to your needs and environment.