New plugin - set file mtime to Exif.Image.DateTime
[geeqie.git] / plugins / exif-datetime-to-file / geeqie-exif-datetime-to-file
1 #!/bin/sh
2
3 ## @file
4 ## @brief Sets file mtime to the Exif.Image.DateTime of the file
5 ##
6 ## $@ List of files to be processed
7 ##
8 ## Requires exiv2 or exiftool
9 ##
10
11 if [ -z "$(command -v exiv2)" ]
12 then
13         if [ -z "$(command -v exiftool)" ]
14         then
15                 zenity --title="Geeqie exif datetime to file" --info --width=200 --text="Neither exiv2 nor exiftran is installed" --window-icon=/usr/local/share/pixmaps/geeqie.png 2> /dev/null
16                 exit 1
17         else
18                 use_exiv2=false
19         fi
20 else
21         use_exiv2=true
22 fi
23
24 if ! zenity --title="Geeqie - exif datetime to file" --question --text "Set file datetime (mtime) to  Exif.Image.DateTime\n\nContinue?" --width=300 --window-icon=/usr/local/share/pixmaps/geeqie.png
25 then
26         exit 0
27 fi
28
29 # exiv2 should be faster
30 if [ "$use_exiv2" = "true" ]
31 then
32         while [ $# -gt 0 ]
33         do
34                 if date_time=$(exiv2 -g Exif.Image.DateTime -Pv "$1")
35                 then
36                         y_m_d_h_m_s=$(echo "$date_time" | tr ' ' ':')
37                         y_m_d_h_m=$(echo "$y_m_d_h_m_s" | cut --delimiter ':' --fields '1 2 3 4 5' | tr --delete ':')
38                         s=$(echo "$y_m_d_h_m_s" | cut --delimiter ':' --fields '6')
39                         touch -m -t "$y_m_d_h_m.$s" "$1"
40                 else
41                         printf "Geeqie plugin exif-datetime-to-file -\nFile: %s does not have Exif.Image.DateTime\n" "$1"
42                 fi
43                 shift
44         done
45 else
46         while [ $# -gt 0 ]
47         do
48                 if date_time=$(exiftool -t -createdate "$1")
49                 then
50                         y_m_d_h_m_s=$(echo "$date_time" | cut --characters '13-31' | tr ' ' ':')
51                         y_m_d_h_m=$(echo "$y_m_d_h_m_s" | cut --delimiter ':' --fields '1 2 3 4 5' | tr --delete ':')
52                         s=$(echo "$y_m_d_h_m_s" | cut --delimiter ':' --fields '6')
53                         touch -m -t "$y_m_d_h_m.$s" "$1"
54                 else
55                         printf "Geeqie plugin exif-datetime-to-file -\nFile: %s does not have Exif.Image.DateTime\n" "$1"
56                 fi
57                 shift
58         done
59 fi