Code Monkey home page Code Monkey logo

filetag's Introduction

## Time-stamp: <2013-05-17 12:22:24 vk> ## -*- coding: utf-8 -*- ## This file is best viewed with GNU Emacs Org-mode: http://orgmode.org/

filetag.py

This Python script adds or removes tags to file names in the following form:

2013-05-09 a file name with ISO date stamp in name -- tag1.jpg
different file without time stamp in name -- tag2.txt
2013-05-09T16.17 file name with time stamp -- tag3.csv
file name with several tags -- tag1 tag2.jpeg
another example file name with multiple example tags -- fun videos kids.mpeg

The script accepts an arbitrary number of files (see your shell for possible length limitations).

Why

Besides the fact that I am using ISO dates and times in file names (as shown in examples above), I am using tags with file names. To separate tags from the file name, I am using the separator ~ – ~ (space dash dash space).

For people familiar with Regular Expressions:

(<ISO date/time stamp>)? <descriptive file name> -- <list of tags separated by spaces>.extension

Tagging files this way requires a file renaming process. Adding (or removing) tag(s) to a set of file results in multiple renaming processes. Despite advanced renaming tools like vidir (from moreutils) it’s handy to have a tool that makes adding and removing tags as simple as possible.

You may like to add this tool to your image or file manager of choice. I added mine to geeqie which is my favorite image viewer on GNU/Linux.

Usage

filetag.py --tag foo a_file_name.txt

… adds tag “foo” such that it results in a_file_name -- foo.txt

filetag.py -i *.jpeg

… interactive mode: asking for list of tags (for the JPEG files) from the user

filetag.py --tag "foo bar" "file name 1.jpg" "file name 2 -- foo.txt" "file name 3 -- bar.csv"

… adds tag “foo” such that it results in …

"file name 1 -- foo bar.jpg"
"file name 2 -- foo bar.txt"
"file name 3 -- bar foo.csv"
filetag.py --remove --tag foo "foo a_file_name -- foo.txt"

… removes tag “foo” such that it results in foo a_file_name.txt

For a complete list of parameters, please try:

filetag.py --help

Bonus: integrating into Geeqie (or similar file browsers)

I am using geeqie for browsing/presenting image files. After I mark a set of images for adding one or more tags, I just have to press t and I get asked for the tags. After entering the tags and RETURN, the tags are added to the image files. With T I can remove tags accordingly.

Using GNU/Linux, this is quite easy accomplished. The only thing that is not straight forward is the need for a wrapper script. The wrapper script does provide a shell window for entering the tags.

vk-filetag-interactive-adding-wrapper-with-gnome-terminal.sh looks like:

#!/bin/sh

/usr/bin/gnome-terminal \
    --geometry=73x5+330+5  \
    --tab-with-profile=big \
    --hide-menubar \
    -x /home/vk/src/filetag/filetag.py --interactive "${@}"

#end

vk-filetag-interactive-removing-wrapper-with-gnome-terminal.sh looks like:

#!/bin/sh

/usr/bin/gnome-terminal \
    --geometry=73x5+330+5  \
    --tab-with-profile=big \
    --hide-menubar \
    -x /home/vk/src/filetag/filetag.py --interactive --remove "${@}"

#end

In $HOME/.config/geeqie/applications I wrote two desktop files such that geeqie shows the wrapper scripts as external editors to its image files:

$HOME/.config/geeqie/applications/add-tags.desktop looks like:

[Desktop Entry]
Name=filetag
GenericName=filetag
Comment=
Exec=/home/vk/src/misc/vk-filetag-interactive-adding-wrapper-with-gnome-terminal.sh %F
Icon=
Terminal=true
Type=Application
Categories=Application;Graphics;
hidden=false
MimeType=image/*;video/*;image/mpo;image/thm
Categories=X-Geeqie;

$HOME/.config/geeqie/applications/remove-tags.desktop looks like:

[Desktop Entry]
Name=filetag
GenericName=filetag
Comment=
Exec=/home/vk/src/misc/vk-filetag-interactive-removing-wrapper-with-gnome-terminal.sh %F
Icon=
Terminal=true
Type=Application
Categories=Application;Graphics;
hidden=false
MimeType=image/*;video/*;image/mpo;image/thm
Categories=X-Geeqie;

In order to be able to use the keyboard shortcuts t (adding tags) and T (removing tags), you can define them in geeqie:

  1. Edit > Preferences > Preferences … > Keyboard.
  2. Scroll to the bottom of the list.
  3. Double click in the KEY-column of filetag and filetag-remove and choose your desired keyboard shortcut accordingly.

I hope this method is as handy for you as it is for me :-)

Bonus: Using tags to specify a sub-set of photographs

You know the problem: got back from Paris and you can not show 937 image files to your friends. It’s just too much.

My solution: I tag to define selections. For example, I am using sel for the ultimate cool photographs using filetag, of course.

Within geeqie, I redefined S (usually mapped to “sort manager”) to an external shell script (below) which creates a temporary folder (within /tmp/), symbolic links to all photographs of the current folder that contain the tag sel, and start a new instance of geeqie.

In short: after returning from a trip, I mark all “cool” photographs within geeqie, choose t and tag them with sel (described in previous section). For showing only sel images, I just press S in geeqie and instead of 937 photographs, my friends just have to watch the best 50 or so. :-)

The script vksel.sh looks like this:

#!/bin/sh

TMPDIR="/tmp/imageselection"
IMAGEDIR="${1}"
IMAGEVIEWER="geeqie"
FILENAME=$(basename $0)

print_usage()
{
        echo
        echo "usage:   ${FILENAME} <directory>"
        echo 
        echo "... starts a image viewer containing files tagged with \"sel\" in the current" 
        echo "folder or the folder given as parameter 1."
        echo
}

STARTDIR=`pwd`

if [ "x${IMAGEDIR}" = "x-h" -o "x${IMAGEDIR}" = "x--help" ]; then
    print_usage
    exit 0
fi

if [ "x${IMAGEDIR}" = "x" ]; then
    IMAGEDIR="${STARTDIR}"
fi

if [ ! -d ${IMAGEIDIR} ]; then
    echo
    echo "  Please specify a folder containing the <directory>."
    echo
    print_usage
    exit 1
fi


## remove (old) TMPDIR if exists:
test -d "${TMPDIR}" && rm -rf "${TMPDIR}"

## create fresh TMPDIR
mkdir "${TMPDIR}"
cd "${TMPDIR}"


find "${IMAGEDIR}" -name '* -- *sel*' -print0 | xargs -0 -I {} ln -s {} . --
${IMAGEVIEWER}

cd "${STARTDIR}"

#end

Integration in geeqie is done with $HOME/.config/geeqie/applications/show-sel.desktop

[Desktop Entry]
Name=show-sel
GenericName=show-sel
Comment=
Exec=/home/vk/bin/vksel.sh
Icon=
Terminal=true
Type=Application
Categories=Application;Graphics;
hidden=false
MimeType=image/*;video/*;image/mpo;image/thm
Categories=X-Geeqie;

Contribute!

I am looking for your ideas!

If you want to contribute to this cool project, please fork and contribute!

Local Variables

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.