Nautilus/Thunar custom action "Duplicate a Minetest World"

Post Reply
User avatar
MetaNomad
Member
Posts: 33
Joined: Sat Apr 10, 2021 09:58
In-game: MetaNomad

Nautilus/Thunar custom action "Duplicate a Minetest World"

by MetaNomad » Post

I've written a script to help duplicate Minetest worlds using Thunar's (or Nautilus) custom actions. Useful for testing your mods under different conditions. Here’s how you can set it up:

1. Install Zenity (if not already installed):

Code: Select all

sudo apt-get install zenity
Adapt if non-debian based system

2. Create the bash script:

Code: Select all

cd ~/minetest_folder/util
nano duplicate_world.sh

Code: Select all

#!/bin/bash
original_folder="$1"
original_name=$(basename "$original_folder")
if [ ! -f "$original_folder/world.mt" ]; then
    zenity --error --text "The selected folder does not appear to be a Minetest world. Please select a valid Minetest world folder."
    exit 1
fi
while true; do
    new_name=$(zenity --entry --text "Enter the new name for the world:" --entry-text "$original_name")
    if [ -z "$new_name" ]; then
        exit 1
    fi
    if [ "$new_name" == "$original_name" ]; then
        zenity --error --text "The new name cannot be the same as the original name. Please choose a different name."
        continue
    fi
    new_folder=$(dirname "$original_folder")/"$new_name"
    if [ -d "$new_folder" ]; then
        zenity --error --text "A world with the name '$new_name' already exists. Please choose a different name."
        continue
    fi
    break
done
cp -r "$original_folder" "$new_folder"
sed -i "s/^world_name = .*/world_name = $new_name/" "$new_folder/world.mt"
zenity --info --text "The world has been successfully copied and renamed to '$new_name'."
3. Make the script executable:

Code: Select all

chmod +x duplicate_world.sh
4. Configure the custom action in Thunar:
  • Open Thunar.
  • Go to Edit > Configure custom actions...
  • Click on the + button to add a new action.
  • Fill in the fields as follows:
    • Name: Duplicate Minetest World
    • Description: Duplicate a Minetest world with a new name
    • Command: /path/to/script/duplicate_world.sh %f (replace /path/to/script/ with the actual path to the script)
  • In the Appearance Conditions tab:
    • File Pattern: *
    • Appears if selection contains: Directories
5. Save and test:
  • Save the custom action.
  • Right-click on a Minetest world folder in Thunar and select Duplicate Minetest World.
  • Enter the new name when the Zenity dialog appears.
  • Check that the folder has been copied and the world.mt file has been updated correctly.
And there you have it! Hope it will be useful for someone.



Version française :

Code: Select all

#!/bin/bash
original_folder="$1"
original_name=$(basename "$original_folder")
if [ ! -f "$original_folder/world.mt" ]; then
    zenity --error --text "Le dossier sélectionné ne semble pas être un monde Minetest. Veuillez sélectionner un dossier valide de monde Minetest."
    exit 1
fi
while true; do
    new_name=$(zenity --entry --text "Entrez le nouveau nom pour le monde:" --entry-text "$original_name")
    if [ -z "$new_name" ]; then
        exit 1
    fi
    if [ "$new_name" == "$original_name" ]; then
        zenity --error --text "Le nouveau nom ne peut pas être le même que le nom original. Veuillez choisir un autre nom."
        continue
    fi
    new_folder=$(dirname "$original_folder")/"$new_name"
    if [ -d "$new_folder" ]; then
        zenity --error --text "Un monde avec le nom '$new_name' existe déjà. Veuillez choisir un autre nom."
        continue
    fi
    break
done
cp -r "$original_folder" "$new_folder"
sed -i "s/^world_name = .*/world_name = $new_name/" "$new_folder/world.mt"
zenity --info --text "Le monde a été copié et renommé avec succès en '$new_name'."

Post Reply