Tip: Auto tag Images with address location via EXIF GPS data

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

I am particularly proud of this one given that i have very basic scripting knowledge (so welcome ways to clean up the script). The following applescript will extract the GPS data from an image using EXIFTOOL, look up the location via google's geocode API, and tag the image with the address taken (full address like 500 Washington Place, Baltimore, Maryland 12564, USA) so you can search for images by any location info.

Required tools:
Exiftool -- http://www.sno.phy.queensu.ca/~phil/exiftool/
JSON helper plugin for Applescript - https://itunes.apple.com/us/app/json-he ... 4608?mt=12

You will also need to sign up for an API key from google here: https://developers.google.com/maps/docu ... g/#api_key

The scrip is below. Note that 1) script assumes exiftool is installed in /usr/bin/ and 2) you will need to input your own API key in the middle of the script where it says to do so.

I have not tested this extensively but it has worked without error the last week.

Code: Select all
set ExifTool to "/usr/bin/exiftool"
set ExifGPSLat to "-GPSLatitude"
set ExifGPSLon to "-GPSLongitude"
set ExifToolOption to "-keywords+="

-- Get file path
try
   set pathList to quoted form of POSIX path of (theFile as alias)
end try

-- Get GPS Latitude and Longitude from EXIF data
do shell script ExifTool & space & ExifGPSLat & space & "-overwrite_original_in_place -P" & space & pathList
set gpsLat to result

do shell script ExifTool & space & ExifGPSLon & space & "-overwrite_original_in_place -P" & space & pathList
set gpsLon to result

if (gpsLat = "") or (gpsLon = "") then
   error number -128
else
   
   -- Set GPS variables separately
   set latDegree to word 3 of gpsLat
   set latMinute to word 5 of gpsLat
   set latSecond to word 6 of gpsLat
   set latDirection to word 7 of gpsLat
   set lonDegree to word 3 of gpsLon
   set lonMinute to word 5 of gpsLon
   set lonSecond to word 6 of gpsLon
   set lonDirection to word 7 of gpsLon
   
   -- Convert Deg,Sec,Min to Decimals
   set latDecimal to latDegree + latMinute / 60 + latSecond / 3600
   set lonDecimal to lonDegree + lonMinute / 60 + lonSecond / 3600
   
   -- Change Decimals based on Direction
   if (latDirection is equal to "S") then
      set latDecimal to latDecimal * -1 as text
   end if
   
   if (lonDirection is equal to "W") then
      set lonDecimal to lonDecimal * -1 as text
   end if
   
   set locateP to latDecimal & "," & lonDecimal as string
   
   -- Set the Google API variables
   set api_key to "ENTER_YOUR_OWN_KEY_HERE"
   set apiCommand to "https://maps.googleapis.com/maps/api/geocode/json?latlng=" & locateP & "&result_type=street_address&key=" & api_key
   
   -- Fetch location information from Google
   try
      tell application "JSON Helper"
         set geoL to apiCommand
         set jsonResult to fetch JSON from geoL with cleaning feed
         set kWord to formatted_address of item 1 of results of jsonResult
      end tell
   end try
   
   -- Add keywords for location for images
   do shell script ExifTool & space & ExifToolOption & quoted form of kWord & space & "-overwrite_original_in_place -P" & space & pathList
end if
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm

Neat. Looking at it, I'm guessing that its adding EXIF keywords and not OS X tags?
Mr_Noodle
Site Admin
 
Posts: 8235
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Right. Could be easily adapted to do so.

As an aside, there is probably a better way to parse the Lon and Lat from the EXIF data use pattern matching in applescript or shell script but I'm not very good with it so took the easy way out with "word of". But i think its cool to be able to convert GPS data into addresses as many times i have wanted to look for a picture i knew was taken in a certain location.

The nice thing is that by adding keywords using exiftool, those keywords are searchable (i.e. create a smart folder for all pics taken in Barcelona) on osx like a mac tag would be and also mesh well with programs like Flickr which let you sort/search by exif keyword.

I welcome your feedback on making the script better.
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm

Fantastic Idea!

Am not yet very comfortable with AppleScript to give an example or solution, but wouldnt an error handling of some sort be a nice addition?
vmax77
 
Posts: 7
Joined: Sun Oct 26, 2014 2:30 pm

Hello, first post here, and this is just the photo management setup I am looking for.

However I keep getting an error

2016-11-10 16:15:36.193 hazelworker[4864] 2016-11-06_21-27-15_000.jpeg: Rule Rename matched.
2016-11-10 16:15:36.195 hazelworker[4864] [Error] AppleScript failed: Error executing AppleScript on file /Volumes/[REDACTED]/2016-11-06_21-27-15_000.jpeg.
2016-11-10 16:15:36.195 hazelworker[4864] OSAScript error: {
OSAScriptErrorNumberKey = "-1708";


I did have to updated the exiftool location to /usr/local/bin I guess a OSX update needed this.

I have hazel just run this on images in the specified folder and run the script on "do the following.." I've tried it both as an embedded script and calling out an external.


I am new to Hazel and AppleScript. There is probably something simple that I am not getting.
Any help will be greatly appreciated
gflinch
 
Posts: 7
Joined: Thu Nov 10, 2016 5:12 pm

Did you paste that into an external script? If so, then you are missing the handler you have to set up. I suggest pasting it into Hazel's script editor instead to avoid that, especially if you are new to AppleScript.
Mr_Noodle
Site Admin
 
Posts: 8235
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Ok, not sure why only a few of my posts are showing up (confused) so I'm going to post this here, hopefully I'm not violating a forum rule.

@Mr Noodle, yes I figured out the script, and works great.

Anyhow, I really like this Rule/Script, I have been combing the internet to find out how to extract only a portion of the address from this script.

I don't need the street information, only CITY/STATE/Country
gflinch
 
Posts: 7
Joined: Thu Nov 10, 2016 5:12 pm

Adding -n to the exiftool command will allow you to skip the whole decimal calculation.
sanderdatema
 
Posts: 1
Joined: Fri Apr 02, 2010 7:31 am

I am trying to run this code but am getting the following error on every file it tries to execute.

2017-07-02 23:23:40.282 hazelworker[2379] [Error] AppleScript failed: Error executing AppleScript on file /Users/#######/############/Travel/2017-03-04-12.38.46.JPG.
2017-07-02 23:23:40.282 hazelworker[2379] OSAScript error: {
NSLocalizedDescription = "The variable kWord is not defined.";
NSLocalizedFailureReason = "The variable kWord is not defined.";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: null()>";
OSAScriptErrorBriefMessageKey = "The variable kWord is not defined.";
OSAScriptErrorMessageKey = "The variable kWord is not defined.";
OSAScriptErrorNumberKey = "-2753";
OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'utxt'(\"kWord\")>";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";

Exiftool is installed in usr/local/bin and I have updated "set ExifTool to "/usr/local/bin/exiftool" "

I have inserted my API key as well. The script was pasted as an embedded script into Hazel's AppleScript editor window.

Any ideas. I have not touched anything else inside the script.
jfisher
 
Posts: 37
Joined: Sat Feb 25, 2017 7:47 pm

If you are willing, you could run it as a stand alone to work out the bugs. Do you know AppleScript?
gflinch
 
Posts: 7
Joined: Thu Nov 10, 2016 5:12 pm

gflinch wrote:If you are willing, you could run it as a stand alone to work out the bugs. Do you know AppleScript?


Unfortunately no, I know some very basic stuff in AppleScript and have been trying to read up on it when I find time. I've been reading up on a few of the exif posts regarding GPS tagging but I can never seem to get them to work and this seemed like the most straightforward place to start.

Pasting the code into AppleScript I get the script error that pathList is not defined if that helps.
jfisher
 
Posts: 37
Joined: Sat Feb 25, 2017 7:47 pm

I will post a modified stand alone script later today. I have nearly completely rewritten the original script and added a lot of other features to it. I went from 0 experience to getting a fair (but newbie) of AppleScript.
gflinch
 
Posts: 7
Joined: Thu Nov 10, 2016 5:12 pm

gflinch wrote:I will post a modified stand alone script later today. I have nearly completely rewritten the original script and added a lot of other features to it. I went from 0 experience to getting a fair (but newbie) of AppleScript.


I'm excited to see how this works.
jfisher
 
Posts: 37
Joined: Sat Feb 25, 2017 7:47 pm

Code: Select all
set ExifTool to "/usr/local/bin/exiftool"
set ExifGPSLat to "-GPSLatitude"
set ExifGPSLon to "-GPSLongitude"
set ExifToolOption to "-keywords+="
set kWord to ""
-- Get file path
try
   --set pathList to quoted form of POSIX path of (theFile as alias)
   set pathList to quoted form of POSIX path of (choose file with prompt "Please choose a file:" default location (path to desktop))
end try

-- Get GPS Latitude and Longitude from EXIF data
do shell script ExifTool & space & ExifGPSLat & space & "-overwrite_original_in_place -P" & space & pathList
set gpsLat to result

do shell script ExifTool & space & ExifGPSLon & space & "-overwrite_original_in_place -P" & space & pathList
set gpsLon to result

if (gpsLat = "") or (gpsLon = "") then
   error number -128
else
   
   -- Set GPS variables separately
   set latDegree to word 3 of gpsLat
   set latMinute to word 5 of gpsLat
   set latSecond to word 6 of gpsLat
   set latDirection to word 7 of gpsLat
   set lonDegree to word 3 of gpsLon
   set lonMinute to word 5 of gpsLon
   set lonSecond to word 6 of gpsLon
   set lonDirection to word 7 of gpsLon
   
   -- Convert Deg,Sec,Min to Decimals
   set latDecimal to latDegree + latMinute / 60 + latSecond / 3600
   set lonDecimal to lonDegree + lonMinute / 60 + lonSecond / 3600
   
   -- Change Decimals based on Direction
   if (latDirection is equal to "S") then
      set latDecimal to latDecimal * -1 as text
   end if
   
   if (lonDirection is equal to "W") then
      set lonDecimal to lonDecimal * -1 as text
   end if
   
   set locateP to latDecimal & "," & lonDecimal as string
   
   -- Set the Google API variables
   set api_key to "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
   set apiCommand to "https://maps.googleapis.com/maps/api/geocode/json?latlng=" & locateP & "&result_type=street_address&key=" & api_key
   
   
   -- Fetch location information from Google
   try
      tell application "JSON Helper"
         set geoL to apiCommand
         set jsonResult to fetch JSON from geoL with cleaning feed
         set kWord to formatted_address of item 1 of results of jsonResult
      end tell
   end try
   
   
   
   display dialog ("Photo taken: " & kWord) buttons "OK"
end if




Here you go, this is a bare script not intended for hazel. This is for you to test to see if you are getting the correct information. You will need to make sure exiftool location is pointed to where yours is located, and also fill in your API key.

My new script breaks the GPS location down to a very granular level.

Sorry for the delay, been very busy lately, hope this helps.
gflinch
 
Posts: 7
Joined: Thu Nov 10, 2016 5:12 pm

This script seems to work inside AppleScript just fine. I combined it with your previous script and commented out the appropriate lines and pasted it into Hazel. It seems to be working nicely.

I had a question about skipping the decimal place calculation so that it only shows the City, State & Country like one member suggested. I don't understand where to add the -n to the Exiftool command to skip this.

Is that done inside your script?

I'd like to only show the City, State & Country so I can use that information to create folders and sort the images into it. Renaming the file with the physical address
jfisher
 
Posts: 37
Joined: Sat Feb 25, 2017 7:47 pm

Next

Return to Tips & Tricks - DO NOT POST QUESTIONS

cron