If you want to compare two files with meld
from Nautilus, you can write some scripts that can be activated through the right-click menu on a file or folder.
The scripting language for Nautilus scripts may be bash, python, perl or ruby, just to list some examples. In this answer I'll show a solution based on bash.
The first thing to is to create the scripts folder with the terminal command:
mkdir -p ~/.local/share/nautilus/scripts
All scripts will go into this folder. Once a script is in the folder, it will not show up in the right-click context menu until you make it executable with the terminal command:
chmod +x <script_name>
Once you have at least one executable script, then it will appear under 'scripts' in the context menu, as shown below (the image is just a reference taken from a StackExchange post, currently I don't have acces to my own Ubuntu for a more "personal" screenshot).
Now, you can add to that folder the following two files, that I have named Meld (select first item)
and Meld (select second item)
. I have not put the .sh
just for an aestethic reason (the extension appears in the context menu, and I don't like it): the presence of the shebang line at the beginning of every file it's enough for the system to understand how to execute them.
Content of Meld (select first item)
file:
#!/bin/bash
#-------------------------------------------------------------------------------
# Script: Meld (select first item)
# Author: Lorenz Keel (AskUbuntu)
# Purpose: Define left-side compare operand for meld
# Note: Don't forget to run: chmod +x <script_name>
#------------------------------------------------------------------------------
# Exit values
STATUS_OK=0
STATUS_ERROR=1
# Check meld package installation
if [ -n "$(command -v meld)" ]; then
# Create temp file
TMP_FILE="/tmp/nautilus-compare.txt"
# Check if the Nautilus environment variable is empty
if [ -z "${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS}" ]; then
# If it's blank, exit with notification of error
exit "${STATUS_ERROR}"
else
# Remove the previous version of the temporary file
if [ -f "${TMP_FILE}" ]; then
rm -rf "${TMP_FILE}"
fi
# Put quotes around every full path
while read -r FULL_PATH; do
echo "'${FULL_PATH}'" | tee "${TMP_FILE}"
done < <(echo "${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS::-1}")
fi
else
# Get the name of the current icon theme
ICON_THEME="$(gsettings get org.gnome.desktop.interface icon-theme | tr -d "'")"
# Set the value of the notification icon
if [ -f "/usr/share/icons/${ICON_THEME}/scalable/status/dialog-error-symbolic.svg" ]; then
NOTIFY_ICON="/usr/share/icons/${ICON_THEME}/scalable/status/dialog-error-symbolic.svg"
else
NOTIFY_ICON="error"
fi
# Specifies the timeout in seconds after which the dialog is closed
NOTIFY_TIMEOUT=5000
TEXT_ERROR="Installare meld per usare questo script"
notify-send -t "${NOTIFY_TIMEOUT}" -i "${NOTIFY_ICON}" "${TEXT_ERROR}"
fi
# Exit with notification of success
exit "${STATUS_OK}"
The above files does not trigger yet meld
, but simply creates a temporary files that contains the list of the files that are selected in Nautilus when you press the Meld (select first item)
item in the context menu. The selected files are read from the special variable NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
that serves for this specific purpose.
The remaining part of the script triggers a notification with the proper icon in the Ubuntu top bar in case of errros.
Content of Meld (select first item)
file:
#!/bin/bash
#-------------------------------------------------------------------------------
# Script: Meld (select second item)
# Author: Lorenz Keel (AskUbuntu)
# Purpose: Define right-side compare operand for meld
# Note: Don't forget to run: chmod +x <script_name>
#------------------------------------------------------------------------------
# Exit values
STATUS_OK=0
STATUS_ERROR=1
# Check meld package installation
if [ -n "$(command -v meld)" ]; then
# Temp file location
TMP_FILE="/tmp/nautilus-compare.txt"
# Check if the Nautilus environment variable is empty
if [ -z "${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS}" ]; then
# If it's blank, exit with notification of error
exit "${STATUS_ERROR}"
else
if [ -f "${TMP_FILE}" ]; then
# Put quotes around every full path
while read -r FULL_PATH; do
echo "'${FULL_PATH}'" | tee -a "${TMP_FILE}"
done < <(echo "${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS::-1}")
else
# Temp file is absent, left item has not been selected
exit "${STATUS_ERROR}"
fi
fi
# Open meld
tail -2 "${TMP_FILE}" | xargs meld &
# Wait before deleting the temp file
sleep 1s && rm -rf "${TMP_FILE}"
else
# Get the name of the current icon theme
ICON_THEME="$(gsettings get org.gnome.desktop.interface icon-theme | tr -d "'")"
# Set the value of the notification icon
if [ -f "/usr/share/icons/${ICON_THEME}/scalable/status/dialog-error-symbolic.svg" ]; then
NOTIFY_ICON="/usr/share/icons/${ICON_THEME}/scalable/status/dialog-error-symbolic.svg"
else
NOTIFY_ICON="error"
fi
# Specifies the timeout in seconds after which the dialog is closed
NOTIFY_TIMEOUT=5000
TEXT_ERROR="Installare meld per usare questo script"
notify-send -t "${NOTIFY_TIMEOUT}" -i "${NOTIFY_ICON}" "${TEXT_ERROR}"
fi
# Exit with notification of success
exit "${STATUS_OK}"
This second script fills the temporary files with the second item of the comparison and then run meld.
You can create these files with you preferred text editor, such as nano
(from terminal) or gedit
and gnome-text-editor
(if you prefer a GUI). The important thing is to save them in the folder I mentioned at the beginning of this answer and enable execution rights on them.
These scripts have been written by myself for my personal usage, my minimal debugging showed me that they work for the simple tasks I ask them to do, I'm aware that probably they can be improved a lot.