Revisited: Taxonomies in Thunar (Color labelling/tagging)

Get help with XFCE specific problems
User avatar
M. Eerie
Moderator
Moderator
Posts: 711
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

Revisited: Taxonomies in Thunar (Color labelling/tagging)

Post#1 by M. Eerie » 10 Feb 2025, 13:12

With some quite a bit... “co-piloting” ;) I've been redesigning this method that I created some time ago and somehow felt was incomplete.

It now works completely and in a much more optimized way. :)

INSTRUCTIONS:

1.- Launch the gen-thunar-emblems-colors-actions script (below) to generate the needed framework
2.- Copy the switch-emblem-color script (below) to the folder YOU DECLARED in the gen-thunar-emblems-colors-actions script.
3.- Test with the test-emblems script (below). Preferably copy the test script to the desktop, give it run permissions (chmod +x test-emblems) and leave only a thunar window open showing the blue, green or whatever subfolder is used in the script.

gen-thunar-emblems-colors-actions follows:

Code: Select all

#!/bin/bash
# gen-thunar-emblems-colors-actions
# 2025-03 M. Eerie --> forum.porteus.org
script_path=""
paths=("${HOME}/.local/bin" "${HOME}/bin") # Lookup for user scripts

for path in "${paths[@]}"; do
    if echo "$PATH" | grep -qE "(^|:)$path(:|$)" || [ -d "$path" ]; then
        script_path="${path%/}/switch-emblem-color"
        break
    fi
done

if [ -z "$script_path" ]; then
    read -p "Enter path to your script: (e.g., /home/guest/bin/): " -i "${HOME}/bin/" user_path
    ! [ -d "$user_path" ] && mkdir -p "$user_path"
    script_path="${user_path%/}/switch-emblem-color"
fi

# Emblems Colors
colors=("blue" "green" "magenta" "orange" "purple" "red" "skyblue" "yellow")
gencommand==$(command -v magick || command -v convert)
PS3="Give a name to your Labels folder: "
options=("Spotlight" "Colormarks" "Scattergories" "Favorites" "ColorLabels" "Other")
select labels in "${options[@]}"; do
    [[ $labels == "Other" ]] && read -p "Enter the desired name for your Labels folder: " labels
    if [ -n "$labels" ]; then
        for color in "${colors[@]}"; do 
            bookmark=$HOME/$labels/$color
            mkdir -p "$bookmark"
            echo "file://$bookmark" >> "$HOME/.config/gtk-3.0/bookmarks"
            if [ -n "$gencommand" ]; then
                "$gencommand" -size 48x48 xc:none -fill "$color" -draw "circle 24,24 24,46" "$bookmark/folder.png"
            else
                echo "Switching emblem color: $color for bookmark: $bookmark"
                switch-emblem-color "$color" "$bookmark"
            fi
        done
        break
    else
        echo "Invalid option. Try again."
    fi
done
echo "Thunar bookmarks created"

# Path to Thunar Sendto .desktop files
th_actions_path="$HOME/.local/share/Thunar/sendto"
mkdir -p "$th_actions_path" # Create if necessary

# Generate .desktop files for each color defined above
for color in "${colors[@]}"; do
  cat <<EOL > "$th_actions_path/tag-$color.desktop"
[Desktop Entry]
Version=1.0
Name=Tag $color
Comment=Tag $color
Exec=sh -c '$script_path $color %F'
Terminal=false
Icon=emblem-color-$color
Type=Application
Path=
StartupNotify=false
EOL
done
echo "Thunar .desktop files created in $th_actions_path"

# Create emblem icons
PS3="Please select your preferred emblem: "
select choice in circles labels
do
    case $choice in
        circles) draw="<circle r='8' cx='8' cy='8' style='fill:\${color}' stroke='#000' stroke-width='.4'/>"
            break ;;
        labels)  draw="<path d='m2 2.997s-1 .003-1 1.003v8s0 1 1 1h9l4-5-4-5z' style='fill:\${color}' stroke='#000' stroke-width='.4'/>"
            break ;;
        *) echo "Invalid option. Exiting." && break ;;
    esac
done

[ -d "$HOME/.icons/hicolor/scalable/emblems" ] || mkdir -p "$HOME/.icons/hicolor/scalable/emblems"
cd "$HOME/.icons/hicolor/scalable/emblems" || exit

colors=("blue" "green" "magenta" "orange" "purple" "red" "skyblue" "yellow")
for color in "${colors[@]}"; do
cat > emblem-color-$color.svg <<EOF
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="16" height="16">
 ${draw//\$\{color\}/$color}
</svg>
EOF
done
echo "Emblem icons created in $HOME/.icons/hicolor/scalable/emblems"

exit 0
switch-emblem-color follows:

Code: Select all

#!/bin/bash
# switch-emblem-color
# v. 2025-03-04 by M. Eerie --> forum.porteus.org
# This Bash script is designed to manage file emblems and symbolic links based on user-specified options.
# It allows the user to:
# - Tag files with emblems: Assigns a color-based emblem to a file and creates a symbolic link in a designated directory.
# - Untag files: Removes all emblems from a file and deletes any associated symbolic links.
# - List emblems: Displays the current emblems assigned to a file.
# The script ensures that symbolic links are correctly created or removed according to the presence of the corresponding emblems,
# effectively organizing files based on their tags.

# Usage: $0 <color|untag|list> <file1> [file2 ... fileN]

option="$1"
shift # Ignore 1st argument ("option"). Now $@ becomes the files
[ -z "$option" ] && exit 1

manage_symlink() {
    local action="$1" file="$2" emblem="$3"
    local destination_dir="$HOME/Scattergories/$emblem"
    local symlink="$destination_dir/${file##*/}"

    case "$action" in
        create) mkdir -p "$destination_dir" && ln -sf "$(realpath "$file")" "$symlink" ;;
        delete) [ -L "$symlink" ] && [ "$(readlink "$symlink")" = "$(realpath "$file")" ] && rm "$symlink" ;;
    esac
}

for file in "$@"; do
    emblems=$(gio info -a metadata::emblems "$file" | sed -n 's/metadata::emblems: \[\(.*\)\]/\1/p')

    case "$option" in
        untag)
            gio set -t unset "$file" metadata::emblems
            find "$HOME/Scattergories/" -type l -samefile "$(realpath "$file")" -delete ;;

        list) echo "$file: $emblems" ;;

        *)
            emblem="emblem-color-$option"

            # Create comma-separated array from emblems list
            IFS=',' read -ra arr <<< "$emblems"
            for i in "${!arr[@]}"; do
                arr[i]=$(echo "${arr[i]}" | xargs) # Remove spaces
            done

            arr=($(printf "%s\n" "${arr[@]}" | awk NF)) # Remove empty elements from array

            found=0
            for i in "${!arr[@]}"; do
                if [ "${arr[i]}" = "$emblem" ]; then
                    found=1
                    break
                fi
            done

            if [ "$found" -eq 0 ]; then
                arr+=("$emblem")
                manage_symlink create "$file" "$option"
            else
                arr=("${arr[@]/$emblem}")
                manage_symlink delete "$file" "$option"
            fi

            emblems=$(IFS=','; echo "${arr[*]}") # Properly rebuild the emblems string

            # Update metadata::emblems key
            [ -z "$emblems" ] && gio set -t unset "$file" metadata::emblems || \
            gio set -t stringv "$file" metadata::emblems "${arr[@]}"
            touch -cr "$file" "$file" # Keep time mark from original file
            ;;
    esac
done


test-emblems script:

Code: Select all

#!/bin/bash
desktop_dir=$(xdg-user-dir DESKTOP)
cd "$desktop_dir" || ( echo "Desktop directory not found." && exit )
file="kk kk"
[ -f "$file" ] && toggle-emblem-color untag "$file" || touch "$file"
colors=("blue" "green" "yellow" "red" "orange")
for i in {1..15}
do
    random_color=${colors[$RANDOM % ${#colors[@]}]} # Select random color
    command=" toggle-emblem-color $random_color '$file'"
    eval "$command" && sleep 1
done

toggle-emblem-color untag "$file"
toggle-emblem-color list "$file"
rm "$file"
create-thunar-submenu

Code: Select all

#!/bin/bash

xml_file="$HOME/.config/Thunar/uca.xml"
### Create a backup first
cp "$xml_file"{,.bak}

# Color names
names=("Blue" "Green" "Magenta" "Orange" "Purple" "Red" "Skyblue" "Yellow")

write_record=""
for i in {0..7}; do
    unique_id="$(date +%s%N)-$((i+1))"
    record="<action>
        <icon>emblem-color-${names[i],,}</icon>
        <name>${names[i]}</name>
        <submenu>Etiquetas</submenu>
        <unique-id>${unique_id}</unique-id>
        <command>sh -c &quot;~/.local/bin/toggle-emblem-color ${names[i]} %F&quot;</command>
        <description>Poner/Quitar etiqueta ${names[i],,}</description>
        <range>*</range>
        <patterns>*</patterns>
        <directories/>
	    <audio-files/>
	    <image-files/>
	    <other-files/>
	    <text-files/>
	    <video-files/>
    </action>"
    write_record="${write_record}${record}\n"
done

temp_file=$(mktemp)
{ head -n -1 "$xml_file"; printf "%b" "$write_record"; tail -n 1 "$xml_file"; } > "$temp_file"
mv "$temp_file" "$xml_file"

Installation Demo:
Image

Testing Demo:
Image

Feedback much appreciated. :)

EDIT:
* Removed trailing slash preventing correct operation with filenames containing spaces

Code: Select all

--- local destination_dir="$HOME/Scattergories/$emblem/"
+++ local destination_dir="$HOME/Scattergories/$emblem"
* Changed test-emblems to show an example of usage with a filename containing spaces

* Fixed a bug in switch-emblem-color that deleted the existing symlink when adding a next emblem. Now as many symlinks are created (and deleted) as emblems.

* Added option to create folder.png icons for each generated color folder
* Added a script to generate a Tags menu in Thunar
Last edited by M. Eerie on 07 Mar 2025, 00:52, edited 7 times in total.
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=102066#p102066
https://forum.porteus.org/viewtopic.php?p=102306#p102306
https://forum.porteus.org/viewtopic.php?p=72741#p72741

User avatar
M. Eerie
Moderator
Moderator
Posts: 711
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

Revisited: Taxonomies in Thunar (Color labelling/tagging)

Post#2 by M. Eerie » 18 Feb 2025, 09:11

Fixed a stupid but important bug :oops:
M. Eerie wrote:
10 Feb 2025, 13:12
* Removed trailing slash preventing correct operation with filenames containing spaces
Added a new test script illustrating the aforementioned
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=102066#p102066
https://forum.porteus.org/viewtopic.php?p=102306#p102306
https://forum.porteus.org/viewtopic.php?p=72741#p72741

vinnie
Shogun
Shogun
Posts: 210
Joined: 13 Jun 2024, 08:25
Distribution: alpine

Revisited: Taxonomies in Thunar (Color labelling/tagging)

Post#3 by vinnie » 18 Feb 2025, 20:47

I'm a little curious to understand how it works, please tell me if I understand correctly.
Can you add a color to a file/directory and then based on the color these are moved to a specific place in the file system?
Like a kind of preset for “send to” ?

User avatar
M. Eerie
Moderator
Moderator
Posts: 711
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

Revisited: Taxonomies in Thunar (Color labelling/tagging)

Post#4 by M. Eerie » 18 Feb 2025, 22:24

Hi Vinnie. It's a way to organize/classify different materials by colors, although you can use any other method, such as ranges, priorities, etc. It is commonly used on Macs to have more versatility when it comes to finding files according to their categories. If I'm not mistaken, Macs allows you to search for files based on their metadata, something I haven't found in Thunar yet.

Take a look at the Thunar's sidepanel in the demo to see how it works. There you will see some bookmarked folders with color names pointing to their respective folders within $HOME/Scattergories.
Then, I came up with a method by using Thunar's SendTo menu to assign a color emblem to a series of selected files and create a symlink at once to track them easily.

In this way, files that have been assigned a color emblem will appear linked in the (bookmarked) folder of that color. To remove that assignment, just do the same process, as the script acts like a toggle.

It works pretty well, as long as you don't use fancy names containing unusual characters like emojis and such...

Tip: To make the process a bit easier, I recommend adding a Thunar custom action:
Thunar menu --> Edit --> Configure custom actions --> Name: Open physical location, command:

Code: Select all

thunar "$(dirname "$(readlink -f %f)" -z)"
Appearance conditions: File pattern *, Rank * and check all options below.

Reminder: Once a color/category has been assigned, the original file should not be renamed/moved. Otherwise, there will be broken links in the color folders, and you'll have to delete them manually.

I hope I have clarified how this works :)
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=102066#p102066
https://forum.porteus.org/viewtopic.php?p=102306#p102306
https://forum.porteus.org/viewtopic.php?p=72741#p72741

User avatar
M. Eerie
Moderator
Moderator
Posts: 711
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

Revisited: Taxonomies in Thunar (Color labelling/tagging)

Post#5 by M. Eerie » 18 Feb 2025, 22:41

This is how my Thunar's sidepanel looks currently:
Image

For convenience (to make assignation process visually easier), I've renamed the color folders bookmarks in the side panel. You can do this by the right-click menu --> rename bookmark, or by editing the contents of the $HOME/.config/gtk-3.0/bookmarks file. This is mine:

Code: Select all

file:///home/guest/Documentos
file:///home/guest/Descargas
file:///home/guest/Im%C3%A1genes
file:///home/guest/V%C3%ADdeos
file:///home/guest/Scattergories     [ Scattergories ] 
file:///home/guest/Scattergories/magenta Apuntes - Notas 
file:///home/guest/Scattergories/orange Links 
file:///home/guest/Scattergories/purple Personal 
file:///home/guest/Scattergories/blue Finalizado 
file:///home/guest/Scattergories/green En progreso 
file:///home/guest/Scattergories/red Pendiente 
file:///home/guest/Scattergories/skyblue .dotfiles 
file:///home/guest/Scattergories/yellow scripts 
file:///home/guest/Applications Applications 
Last edited by M. Eerie on 07 Mar 2025, 00:31, edited 2 times in total.
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=102066#p102066
https://forum.porteus.org/viewtopic.php?p=102306#p102306
https://forum.porteus.org/viewtopic.php?p=72741#p72741

vinnie
Shogun
Shogun
Posts: 210
Joined: 13 Jun 2024, 08:25
Distribution: alpine

Revisited: Taxonomies in Thunar (Color labelling/tagging)

Post#6 by vinnie » 18 Feb 2025, 22:53

Yes, it is clear, I guess you can have as many categories as you like and add them whenever you want.
I don't know if that would be useful for me because I tend to live boot the distribution each time and put in a separate partition what I need to save, i.e., I'm forced to physically move things from the home
However, it seems like a great way to sort things because it helps you automate some of the work.
If I can I will try it out.

User avatar
M. Eerie
Moderator
Moderator
Posts: 711
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

Revisited: Taxonomies in Thunar (Color labelling/tagging)

Post#7 by M. Eerie » 07 Mar 2025, 00:50

Added a script to generate a Labels menu in Thunar as an alternative to the SendTo menu via custom actions (~/.config/Thunar/uca.xml)
See first post.

Changed the look of the color folders in the side panel. Now, in case the ImageMagick package is installed, “folder.png” icons will be added under each color folder to mimic the look of Mac's.
See below:

Image

Image
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=102066#p102066
https://forum.porteus.org/viewtopic.php?p=102306#p102306
https://forum.porteus.org/viewtopic.php?p=72741#p72741

Post Reply