Rename by Creation Date, Area and City? (Exiftool, Geocoder)

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hi everyone, first post here! I’m noob to this, so I hope I’m posting in the right place.

I’m trying to write a shell script within Hazel to rename a lot of pictures and movies by Creation Date and Area & City. For example:
Filename input: IMG_0057.jpg
Intended Filename output: 2020-03-09 IMG_0057 (Studio City, Los Angeles).jpg

I can extract the GPS coordinates with Exiftool, but I can’t figure out how to pass the coordinates to Geocoder automatically to reverse geocode and then back to rename? (For Geocoder I'm using http://www.rubygeocoder.com/)
Here are the commands I have found so far:

Extract Creation Date with Exiftool and rename to start of file
exiftool -P -d %Y-%m-%d" "%%f\
'-filename<${CreateDate;}.%e' \
"$1"

Outputs: ”2020-03-09 IMG_0057.jpg”


Extract GPS Position with Exiftool
exiftool -gpsposition -n -c %.8f ”$1”

Outputs: ”59.3428694444444 17.8776916666667”


Reverse geocode GPS Position with Geocode
Geocode 59.3428694444444 17.8776916666667

Outputs:

Latitude: 59.3428923
Longitude: 17.8774578
Full address: Blackebergsbacken, Blackebergs gård, Blackeberg, Bromma stadsdelsområde, Stockholms kommun, Stockholm County, Region Svealand, 168 52, Sweden
City:
State/province: Stockholm County
Postal code: 168 52
Country: Sweden
Map: https://www.openstreetmap.org/?lat=59.3 ... 5&layers=M


Do any of you know how to extract the area and city from here? Would like to have the area ”Blackeberg” and the city ”Stockholm”. If City is empty (like it is here), I'd like to have the State/County instead, so it's more likely to be renamed.


How do I combine these lines in Hazel so they can work together?

I know there probably are a couple of blanks that need to be filled here, I would be super appreciative for any help on how to write this to make this work!!

Thank you so much in advance for your help, time and knowledge!

All the best,
Emanuel
email
 
Posts: 2
Joined: Mon Mar 09, 2020 7:28 pm

I think you'd have to do that in a script. While you might be able to get that text back into Hazel and do a "matches" on it, I think having fallbacks when certain fields are empty is extra logic that can't be done in Hazel so easily.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Hi,

I would also wished to know.
Totally new here, and not sure how to use ExifTool and GeoTag.

May I have an idea how on how to incorporate that into Hazel?
And how to rename image or movie files taken by creation date, follow by geographic location.

Thanks,
Js
jayisjayz
 
Posts: 1
Joined: Tue Jun 09, 2020 9:37 pm

"Date created" is a standard attribute so that part should be straightforward. For location, unless there's some metadata for it in the file already, you would need to use a script. In that case, search the forums to see if someone has already done this.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

OK Guys, I think I did it!! :shock: :lol:
I can't really code scripts so it probably looks very messy, BUT IT WORKS!
Hope this helps! Let me know if you have any questions!

INPUT FILE: IMG_1287.jpg
OUTPUT FILE: 2019-06-05 IMG_1287 (Studio City, Los Angeles).jpg

(Handles all extensions incl HEIC and should handle multiple dots in the filename) Enjoy!
/Email


Hazel workflow:
If ANY of the following conditions are met
Kind / is / Image
Kind / is / Movie


Do the following to the matched file or folder:
1. Run shell script / embedded script: (ATTENTION: /bin/ksh in the very top field)
#This calls RUBY GEOCODER and appends (Area, City) to the filename.
Code: Select all
/bin/ksh

PATH=/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin


filename=$1


output1=$(exiftool -gpslatitude -n -c %.8f ${filename})
output2=$(exiftool -gpslongitude -n -c %.8f ${filename})
output1a=${output1:13}
output2a=${output2:13}
output1b=${output1a//:}
output2b=${output2a//:}

varGeo=$(Geocode $output1b $output2b)


IFSsave=$IFS
IFS=,
set $varGeo
typeset -i i
i=1
while [ i -lt $# ]; do
    eval varGeo$i=\$$(echo $i)
    eval echo varGeo$i=\$varGeo$i
    i=i+1
done
IFS=$IFSsav

Echo $varGeo4","$varGeo5

GpsName=" (${varGeo4:1}","$varGeo5)"

for fullpath in $filename
do
    filename="${fullpath##*/}"                      # Strip longest match of */ from start
    dir="${fullpath:0:${#fullpath} - ${#filename}}" # Substring from 0 thru pos of filename
    base="${filename%.[^.]*}"                       # Strip shortest match of . plus at least one non-dot char from end
    ext="${filename:${#base} + 1}"                  # Substring from len of base thru end
    if [[ -z "$base" && -n "$ext" ]]; then          # If we have an extension and no base, it's really the base
        base=".$ext"
        ext=""
    fi

    echo -e "$fullpath:\n\tdir  = \"$dir\"\n\tbase = \"$base\"\n\text  = \"$ext\""
done

mv $dir$base"."$ext $dir$base$GpsName"."$ext



2. Run shell script / embedded script: (ATTENTION: /bin/bash in the very top field)

#This prepends the creation date to the filename.
Code: Select all
/bin/bash

exiftool -P -d '%Y-%m-%d '%%f \
   '-filename<${CreateDate;}.%e' \
    '-DateTimeOriginal<${CreateDate;}.%e' \
    '-FileModifyDate<${CreateDate;}.%e' \
    '-filename<${FileModifyDate;}.%e' \
    '-filename<${GPSDateTime;}.%e' \
    '-filename<${MediaCreateDate;}.%e' \
    '-filename<${ModifyDate;}.%e' \
    '-filename<${DateTimeOriginal;}.%e' \
    "$1"


3. These following steps are optional. I chose to move the files into folders by YEAR/MONTH for example: 2020/2020 01 January/file.jpg
Move to folder "YOUR DONE FOLDER"

4.
Sort into subfolder with pattern Date Created (1999)

5.
Sort into subfolder with pattern Date Created (1999-12 december)

Image
email
 
Posts: 2
Joined: Mon Mar 09, 2020 7:28 pm

Thanks for this! When testing it I noticed that:
1/ it would note rename the file but duplicate it twice (once with GPS, then once with GPS and date)
2/ because of the above, the rule would loop
3/ the folder structure would depend on the creation date of the file instead of the exif date (or the file name).

Does anybody know how to update the scripts so that the the file would only be renamed instead of duplicated?

Thanks.
Bruxced
 
Posts: 3
Joined: Tue Jun 26, 2007 5:59 pm

I was planning more or less the same:

viewtopic.php?f=4&t=14897&p=38417#p38417

Maybe the author of memberlist.php?mode=viewprofile&u=22755 or anyone else is willing to comment on this old posting and can help me to get rubygeocoder with hazel to run such a job?
mabebi
 
Posts: 9
Joined: Sat Jan 15, 2022 6:14 am


Return to Support