Skip to content

Instantly share code, notes, and snippets.

@jmada
Last active April 13, 2026 12:41
Show Gist options
  • Select an option

  • Save jmada/389b1c56901208f7ff64e15a3c7c6c7b to your computer and use it in GitHub Desktop.

Select an option

Save jmada/389b1c56901208f7ff64e15a3c7c6c7b to your computer and use it in GitHub Desktop.
muCommander "Open With..." Custom Command

muCommander "Open With..." Custom Command

A robust solution to add an "Open With..." context menu option in muCommander that shows a graphical application selector dialog for any file type.

Problem Statement

muCommander's Java environment has limitations when executing external commands:

  • Cannot properly resolve mimetype and mimeopen commands from the Java PATH
  • Has issues with file path permissions and accessibility
  • Cannot display terminal-based application selectors
  • Requires careful handling of command arguments and quoting

Solution Overview

This solution creates a bash script that:

  1. Properly handles file paths passed from muCommander
  2. Detects the MIME type of the selected file
  3. Searches for all applications that can handle that MIME type
  4. Displays a graphical selection dialog using zenity
  5. Opens the file with the selected application

Files

1. Bash Script: mu-openwith.sh

#!/bin/bash
FILE_PATH="$1"

# Remover comillas simples
if [[ "$FILE_PATH" == \'* ]]; then
    FILE_PATH="${FILE_PATH:1}"
fi
if [[ "$FILE_PATH" == *\' ]]; then
    FILE_PATH="${FILE_PATH%?}"
fi

if [ ! -f "$FILE_PATH" ]; then
    exit 1
fi

export DISPLAY=:0.0
export XAUTHORITY="$HOME/.Xauthority"

# Obtener MIME type
MIME_TYPE="$(/usr/bin/mimetype -b "$FILE_PATH")"

# Mapear MIME de WPS a los MIME estándar usados por LibreOffice
case "$MIME_TYPE" in
    application/wps-office.docx)
        MIME_TYPE="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        ;;
    application/wps-office.pptx)
        MIME_TYPE="application/vnd.openxmlformats-officedocument.presentationml.presentation"
        ;;
    application/wps-office.xlsx)
        MIME_TYPE="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        ;;
esac

APPS_FILE="/tmp/mu-apps-$$.txt"

for desktop_file in /usr/share/applications/*.desktop ~/.local/share/applications/*.desktop; do
    if [ -f "$desktop_file" ]; then
        if grep -q "MimeType=.*$MIME_TYPE" "$desktop_file" 2>/dev/null; then
            APP_NAME="$(grep "^Name=" "$desktop_file" | head -1 | cut -d= -f2)"
            APP_EXEC="$(grep "^Exec=" "$desktop_file" | head -1 | cut -d= -f2)"
            if [ -n "$APP_NAME" ] && [ -n "$APP_EXEC" ]; then
                echo "$APP_NAME|$APP_EXEC" >> "$APPS_FILE"
            fi
        fi
    fi
done

if [ -f "$APPS_FILE" ] && [ -s "$APPS_FILE" ]; then
    if command -v zenity >/dev/null 2>&1; then
        SELECTED="$(cut -d'|' -f1 "$APPS_FILE" | zenity --list \
            --title="Abrir con..." \
            --column="Aplicación" \
            --height=400 \
            --width=500)"

        if [ -n "$SELECTED" ]; then
            APP_CMD="$(grep "^$SELECTED|" "$APPS_FILE" | cut -d'|' -f2 | head -1)"
            APP_CMD="$(echo "$APP_CMD" | sed 's/%[fFuU]//g' | sed 's/^"//' | sed 's/"$//')"
            eval "$APP_CMD '$FILE_PATH'" &
        fi
    else
        xfce4-terminal -e "/usr/bin/mimeopen -a '$FILE_PATH'"
    fi
else
    xfce4-terminal -e "/usr/bin/mimeopen -a '$FILE_PATH'"
fi

rm -f "$APPS_FILE"

2. muCommander Configuration: commands.xml

Add the following to your muCommander commands.xml file (typically located in ~/.mucommander/):

<command alias="openWith" value="/path/to/mu-openwith.sh '$f'" display="Open with..." />

Replace /path/to/ with the actual path where you saved the script.

Installation Steps

  1. Save the script:

    # Save the script to your home directory
    nano ~/mu-openwith.sh
    # Copy and paste the script content
  2. Make it executable:

    chmod +x ~/mu-openwith.sh
  3. Edit muCommander commands.xml:

    # Navigate to muCommander configuration directory
    cd ~/.mucommander/
    # Edit commands.xml (create if it doesn't exist)
    nano commands.xml

    Add the command configuration inside the <commands> section:

    <commands>
        <!-- Other commands -->
        <command alias="openWith" value="/home/yourusername/mu-openwith.sh '$f'" display="Open with..." />
    </commands>
  4. Restart muCommander for the changes to take effect.

Dependencies

The script requires the following packages to be installed:

  • zenity: For graphical dialog (recommended)

    sudo apt install zenity  # Debian/Ubuntu/MX Linux
    sudo pacman -S zenity    # Arch Linux
    sudo dnf install zenity  # Fedora
  • mimeopen and mimetype: For MIME type detection

    sudo apt install shared-mime-info xdg-utils  # Debian/Ubuntu/MX Linux
  • Terminal emulator (optional, for fallback):

    • xfce4-terminal (XFCE)
    • xterm (minimal fallback)
    • gnome-terminal (GNOME)
    • konsole (KDE)

How It Works

  1. File Path Handling: The script properly handles file paths passed from muCommander, removing any extra quotes that might be added during argument passing.

  2. MIME Type Detection: Uses mimetype to determine the file's MIME type (e.g., text/plain, image/png).

  3. Application Discovery: Searches through .desktop files in standard locations to find applications registered to handle the detected MIME type.

  4. Graphical Selection: Presents a clean zenity dialog listing all available applications.

  5. Application Execution: Launches the selected application with the file as an argument, running it in the background.

Troubleshooting

Common Issues

  1. "File not found" error in muCommander:

    • Ensure the script path in commands.xml is correct
    • Check that the script has execute permissions
    • Verify file permissions on the target file
  2. No applications appear in the dialog:

    • Check if mimetype is installed: which mimetype
    • Verify the MIME type detection works: mimetype yourfile.txt
    • Ensure .desktop files exist for your applications
  3. Zenity not found:

    • Install zenity: sudo apt install zenity
    • Or use the terminal fallback by removing the zenity check
  4. Dialog doesn't appear:

    • Check DISPLAY environment: echo $DISPLAY
    • Ensure X11 forwarding is enabled if using remote connections

Testing

Test the script manually before using it in muCommander:

# Test with a sample file
~/mu-openwith.sh "/path/to/test.txt"

# Check for errors
echo $?  # Should return 0 if successful

Alternative Approaches

If zenity is not available or preferred, consider these alternatives:

  1. Terminal-based selection: Use mimeopen directly in a terminal
  2. Simple default opener: Use xdg-open to open with default application
  3. Other dialog tools: Replace zenity with kdialog (KDE) or yad (advanced dialogs)

Compatibility

  • Tested on: MX Linux 23 (XFCE)
  • muCommander version: 1.3.0+
  • Shell: Bash 4.4+
  • Desktop Environments: XFCE, GNOME, KDE (with zenity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment