Photos Sorted by Location using HAZEL & Google Maps API

How to sort your photo based on GPS location, metadata of the image? This script & tweak will use Google Maps API, Hazel and photos import from your iPhone/smartphone with GPS enabled on that device.

Requirements

  1. Hazel installed
  2. Import photos from iPhone
  3. Google Maps API

Steps

hazel script to sort photos by location

Hazel script to sort photos by location

  1. Put the iPhone photos into (Dropbox/IFTTT/TestGPS) folder.
  2. Create RULES in Hazel:
    • If Latitude is greater than 0
    • Run shell script EMBEDED Script

      #!/bin/bash
      #apiKey="AIzaSyCEzXfCAwZLcqz90bn_mRPsvutwOoyIChI"
      #apiKey="abcD12345efGhIJkLmn-abcD12345efGhIJkLmn_cO_8"
      #Set Variable
      filename=$(basename “$1″)
      filename=”${filename%.*}”
      directoryname=$(dirname “$1”)
      latitude=$(mdls “$1” | awk ‘/Latitude/ {print $3}’)
      longitude=$(mdls “$1″ | awk ‘/Longitude/ {print $3}’)

      #Download JSON to local
      curl -s https://maps.googleapis.com/maps/api/geocode/json?latlng=”$latitude”,”$longitude” > “$1.json”

      #Query local JSON – Short Name
      #local6=$(curl -s http://localhost:8088/json/”$1.json” | jq ‘.results[0].address_components[6].short_name’ | sed -e ‘s/^”//’ -e ‘s/”$//’)
      local6=$(cat “$1.json” | json ‘.results[0].address_components[6].short_name’ | sed -e ‘s/^”//’ -e ‘s/”$//’)
      local5=$(cat “$1.json” | json ‘.results[0].address_components[5].short_name’ | sed -e ‘s/^”//’ -e ‘s/”$//’)
      local4=$(cat “$1.json” | json ‘.results[0].address_components[4].short_name’ | sed -e ‘s/^”//’ -e ‘s/”$//’)
      local3=$(cat “$1.json” | json ‘.results[0].address_components[3].short_name’ | sed -e ‘s/^”//’ -e ‘s/”$//’)
      local2=$(cat “$1.json” | json ‘.results[0].address_components[2].short_name’ | sed -e ‘s/^”//’ -e ‘s/”$//’)
      local1=$(cat “$1.json” | json ‘.results[0].address_components[1].short_name’ | sed -e ‘s/^”//’ -e ‘s/”$//’)
      local0=$(cat “$1.json” | json ‘.results[0].address_components[0].short_name’ | sed -e ‘s/^”//’ -e ‘s/”$//’)

      if [ “$local6″ == MY ]
      then
      country=”$local6″
      state=”$local5″
      district=”$local4″
      route=”$local3”
      fi
      if [ “$local5″ == MY ]
      then
      country=”$local5″
      state=”$local4″
      district=”$local3″
      route=”$local2”
      fi
      if [ “$local4″ == MY ]
      then
      country=”$local4″
      state=”$local3″
      district=”$local2″
      route=”$local1”
      fi
      if [ “$local3″ == MY ]
      then
      country=”$local3″
      state=”$local2″
      district=”$local1″
      route=”${local0////_}”
      fi
      if [ “$local2″ == MY ]
      then
      country=”$local2″
      state=”$local1″
      district=”${local0////_}”
      fi

      directory_cs=$(“/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state”)
      directory_d=$(“/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/$district”)
      directory_r=$(“/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/$district””/$route”)

      if [ ! -d “$directory_cs” ]
      then
      mkdir “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state”
      fi
      if [ ! -d “$directory_d” ]
      then
      #mkdir “$directory_d”
      mkdir “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district”
      fi
      if [ “$local6” == MY ]
      then
      if [ ! -d “$directory_r” ]
      then
      mkdir “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/$route”
      fi
      if [ ! -f “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””.jpg” ]
      then
      mv “$1” “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””.jpg”
      else
      mv “$1” “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””_copy.jpg”
      fi
      elif [ “$local5” == MY ]
      then
      if [ ! -d “$directory_r” ]
      then
      mkdir “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/$route”
      fi
      if [ ! -f “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””.jpg” ]
      then
      mv “$1” “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””.jpg”
      else
      mv “$1” “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””_copy.jpg”
      fi
      elif [ “$local4” == MY ]
      then
      if [ ! -d “$directory_r” ]
      then
      mkdir “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/$route”
      fi
      if [ ! -f “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””.jpg” ]
      then
      mv “$1” “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””.jpg”
      else
      mv “$1” “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””_copy.jpg”
      fi
      elif [ “$local3” == MY ]
      then
      if [ ! -d “$directory_r” ]
      then
      mkdir “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/$route”
      fi
      if [ ! -f “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””.jpg” ]
      then
      mv “$1” “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””.jpg”
      else
      mv “$1” “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$route””/””$filename””_copy.jpg”
      fi
      else
      if [ ! -f “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$filename””.jpg” ]
      then
      mv “$1” “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$filename””.jpg”
      else
      mv “$1” “/Users/AmirMac/Pictures/DropboxLocationPictures/$country””_$state””/””$district””/””$filename””_copy.jpg”
      fi
      fi
      rm “$1.json”

  3. Done

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.