102 lines
3.2 KiB
Bash
Executable File
102 lines
3.2 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# Thunar Copy Filename Auto-Installer
|
|
# Copyright (C) 2026 laki
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
#
|
|
# This script adds the "Copy Filename" custom action to ~/.config/Thunar/uca.xml
|
|
|
|
set -e
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
SOURCE_PYTHON_SCRIPT="$SCRIPT_DIR/copy_filename.py"
|
|
UCA_FILE="$HOME/.config/Thunar/uca.xml"
|
|
TARGET_PYTHON_SCRIPT="$HOME/.config/Thunar/copy_filename.py"
|
|
|
|
echo "--- Thunar Copy Filename Installer ---"
|
|
|
|
# 1. Check if the source Python script exists
|
|
if [ ! -f "$SOURCE_PYTHON_SCRIPT" ]; then
|
|
echo "Error: $SOURCE_PYTHON_SCRIPT not found!"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Copying script to: $TARGET_PYTHON_SCRIPT"
|
|
mkdir -p "$(dirname "$TARGET_PYTHON_SCRIPT")"
|
|
cp "$SOURCE_PYTHON_SCRIPT" "$TARGET_PYTHON_SCRIPT"
|
|
chmod +x "$TARGET_PYTHON_SCRIPT"
|
|
|
|
# 2. Check for dependencies
|
|
if ! python3 -c "import gi; gi.require_version('Gtk', '3.0')" 2>/dev/null; then
|
|
echo "Warning: Python3 GTK3 bindings not found."
|
|
echo "Please install: sudo apt install python3-gi gir1.2-gtk-3.0"
|
|
fi
|
|
|
|
# 3. Handle uca.xml
|
|
if [ ! -f "$UCA_FILE" ]; then
|
|
echo "Creating new Thunar configuration at $UCA_FILE"
|
|
mkdir -p "$(dirname "$UCA_FILE")"
|
|
cat <<EOF > "$UCA_FILE"
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<actions>
|
|
</actions>
|
|
EOF
|
|
fi
|
|
|
|
# 4. Check if already installed
|
|
if grep -q "copy-filename-extension" "$UCA_FILE"; then
|
|
echo "Action is already present in $UCA_FILE."
|
|
echo "Updating the path to ensure it's correct..."
|
|
# Update the existing command path
|
|
sed -i "s|<command>.*copy_filename.py %N</command>|<command>$TARGET_PYTHON_SCRIPT %N</command>|g" "$UCA_FILE"
|
|
else
|
|
echo "Adding 'Copy Filename' action to $UCA_FILE"
|
|
|
|
ACTION_BLOCK="<action>
|
|
<icon>edit-copy</icon>
|
|
<name>Copy Filename</name>
|
|
<submenu></submenu>
|
|
<unique-id>copy-filename-extension</unique-id>
|
|
<command>$TARGET_PYTHON_SCRIPT %N</command>
|
|
<description>Copy the filename and extension to clipboard</description>
|
|
<range></range>
|
|
<patterns>*</patterns>
|
|
<startup-notify/>
|
|
<audio-files/>
|
|
<image-files/>
|
|
<other-files/>
|
|
<text-files/>
|
|
<video-files/>
|
|
<directories/>
|
|
</action>"
|
|
|
|
# Use Python to safely handle the multi-line insertion
|
|
export ACTION_BLOCK UCA_FILE
|
|
python3 -c '
|
|
import os
|
|
action_block = os.environ["ACTION_BLOCK"]
|
|
uca_path = os.environ["UCA_FILE"]
|
|
with open(uca_path, "r") as f:
|
|
content = f.read()
|
|
if "<actions>" in content:
|
|
new_content = content.replace("<actions>", "<actions>\n" + action_block)
|
|
with open(uca_path, "w") as f:
|
|
f.write(new_content)
|
|
'
|
|
fi
|
|
|
|
echo "Success! Please restart Thunar (thunar -q) to see the changes."
|
|
echo "----------------------------------------"
|