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
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' \

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


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
State/province: Stockholm County
Postal code: 168 52
Country: Sweden
Map: ... 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,
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.
Site Admin
Posts: 8623
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


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.

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.
Site Admin
Posts: 8623
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!

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



output1=$(exiftool -gpslatitude -n -c %.8f ${filename})
output2=$(exiftool -gpslongitude -n -c %.8f ${filename})

varGeo=$(Geocode $output1b $output2b)

set $varGeo
typeset -i i
while [ i -lt $# ]; do
    eval varGeo$i=\$$(echo $i)
    eval echo varGeo$i=\$varGeo$i

Echo $varGeo4","$varGeo5

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

for fullpath in $filename
    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

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

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

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' \

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"

Sort into subfolder with pattern Date Created (1999)

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

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

Return to Support