#!/bin/bash # A TMSU (Tag My Shit Up) handler using YAD/Zenity for clickable GTK tagging and easy new tag creation. TMSU_PROMPT_PROG="yad" APPLIED_TAGS="" TEMP_DIR=$(mktemp --directory /tmp/tmsu.XXXX) if [ ! -d "/tmp/tmsu-prompt" ]; then mkdir /tmp/tmsu-prompt fi LOGFILE=$(mktemp /tmp/tmsu-prompt/XXXX.log) MOVE_DIR() { # Get images directory and move to tmsu base. WDIR=$(dirname $(readlink -f $1)) cd $WDIR #OP_DIR=$(tmsu info | sed -n 's/Root.*: \(.*\)/\1/p') } SET_TAG() { echo "" > $TEMP_DIR/tags # Make arrays for master tags and images. declare -A tagArray # Initial loop to get full list of tags, setting all to false. for t in $(tmsu tags -n "never" -1) do tagArray[$t]=FALSE done # Loop again to set the tags of the image to TRUE. for t in $(tmsu tags -n "never" -1 $1) do tagArray[$t]=TRUE done # Sort the array so we can print them correctly every time. SORTED=$(printf '%s\n' ${!tagArray[@]} | sort) # Build the table for zenity or yad. for key in $SORTED do echo ${tagArray[$key]} $key >> $TEMP_DIR/tags done # Unset to clear for next loop. unset tagArray } CALL_PROMPT() { # Prompt files. APPLIED_TAGS=$($TMSU_PROMPT_PROG --list \ --text "Tagging image $1" \ --title "Tagging image $1" \ --multiple \ --width=600 --height=600 \ --checklist \ --column="" \ --column "Select" \ --editable \ --image tag \ --separator=" " \ $(cat $TEMP_DIR/tags) | \ sed 's/TRUE //' | tr -d '\n' ) if [ -n "$APPLIED_TAGS" ]; then return 0 else return 1 fi } APPLY_TAG() { tmsu untag --all $1 >> $LOGFILE 2>> $LOGFILE tmsu tag $1 $2 >> $LOGFILE 2>> $LOGFILE } for x in $@ do MOVE_DIR $x SET_TAG $x if CALL_PROMPT $x ; then APPLY_TAG $x "$APPLIED_TAGS" fi done # Cleanup rm $TEMP_DIR/*tags rmdir $TEMP_DIR