Applescript to copy Google Maps address to keywords

Discussion regarding all scripting related questions
Please DO NOT post to this thread anything that is not directly related to scripting of Capture One.

Applescript to copy Google Maps address to keywords

Postby Francesco Mariani » Wed Sep 19, 2018 5:01 pm

Hi,

I am looking for an AppleScript to copy Google Maps address components to Capture One keywords, e.g. Street Name, City, Country.

I used an AppleScript like this in Apple Aperture that used a JSON call to the Google Maps API.

Thanks,

Francis.
Olympus OM-D E-M5 Mark II
MacBook Pro - Retina, 15-inch, Mid 2014 - 2.5 GHz Intel Core i7 processor - 16 GB RAM
macOS Sierra - 10.12.6
Aperture 3.6 > Migrating to Capture One Pro 11 or 12
User avatar
Francesco Mariani
 
Posts: 39
Joined: Mon May 11, 2015 3:26 am
Location: Toronto, Ontario, Canada

Re: Applescript to copy Google Maps address to keywords

Postby Francesco Mariani » Thu Sep 20, 2018 1:39 am

This is the AppleScript for Aperture. I recently had to add a Google Maps API key to make this work, so I found the appropriate Google Maps API link to generate the key - you need to provide credit card info, but since I'll be doing this less than 25,000 times a day, it won't cost anything.

Code: Select all
tell application "Aperture"
   set imageSel to selection
   repeat with i from 1 to count of imageSel
      tell library 1
         tell item i of imageSel
            set lat to get latitude
            set long to get longitude
            
            if (long is missing value) or (lat is missing value) then
               display dialog "Please select a geotagged image" with title "GPS data missing" buttons {"Okay"}
            else
               set lat to my set_decimal_delimiter_to_dot(lat as string)
               set long to my set_decimal_delimiter_to_dot(long as string)
               
               tell application "JSON Helper"
                  --set reverse_geocode_request to fetch JSON from ("https://maps.googleapis.com/maps/api/geocode/json?latlng=" & lat & "," & long & "&sensor=false")
                  set reverse_geocode_request to fetch JSON from ("https://maps.googleapis.com/maps/api/geocode/json?latlng=" & lat & "," & long & "&key=my_google_maps_api_key")
                  set address_components to get address_components of item 1 of results of reverse_geocode_request
                  
                  -- making a list (Array) of address component names
                  set address_components_list to {}
                  repeat with j from 1 to count of address_components
                     if item 1 of |types| of item j of address_components is in {"route", "sublocality", "locality", "administrative_area_level_1", "country"} then
                        copy long_name of item j of address_components to beginning of address_components_list --adding them at the beginning, sorts locations from general to specific
                     end if
                  end repeat
               end tell
               
               -- making and assigning a keyword for each item in the address components list
               -- each keyword will have the previous item(s) as a parent keyword
               -- this will create a nested list of location keywords
               repeat with k from 1 to count of address_components_list
                  -- making a list (Array) of parent keywords from the previous item(s) in the address components list
                  set parents_list to {}
                  repeat with l from 1 to (k - 1)
                     copy item (k - l) of address_components_list to end of parents_list
                  end repeat
                  
                  -- setting delimiter to "\t": so when parents_list get converted to String, a tab is inserted as the separator
                  -- (the nested keywords for the 'parents' param expects a tab-separated list of parent keywords)
                  set AppleScript's text item delimiters to "   "
                  
                  make new keyword with properties {name:item k of address_components_list, parents:parents_list as string}
               end repeat
            end if
            
         end tell
      end tell
   end repeat
end tell

on set_decimal_delimiter_to_dot(this_text)
   set AppleScript's text item delimiters to ","
   set the text_items to every text item of this_text
   set AppleScript's text item delimiters to "."
   set this_text to the text_items as string
   set AppleScript's text item delimiters to ""
   return this_text
end set_decimal_delimiter_to_dot
Olympus OM-D E-M5 Mark II
MacBook Pro - Retina, 15-inch, Mid 2014 - 2.5 GHz Intel Core i7 processor - 16 GB RAM
macOS Sierra - 10.12.6
Aperture 3.6 > Migrating to Capture One Pro 11 or 12
User avatar
Francesco Mariani
 
Posts: 39
Joined: Mon May 11, 2015 3:26 am
Location: Toronto, Ontario, Canada

Re: Applescript to copy Google Maps address to keywords

Postby RobiWan » Sun Sep 23, 2018 8:28 pm

Francesco Mariani wrote:I am looking for an AppleScript to copy Google Maps address components to Capture One keywords, e.g. Street Name, City, Country.


I'm looking for something like that too.
RobiWan
 
Posts: 388
Joined: Fri Mar 30, 2012 1:42 pm

Re: Applescript to copy Google Maps address to keywords

Postby SeanMurp » Tue Sep 25, 2018 4:08 pm

Hi Francis,
Which portions of your previous script are you needing Capture One to do? I would assume you're shooting or importing images into Capture One and want to grab the lat/long data, then go out to Google Maps API to gather the address components and then save it back to the image in Capture One?
Sean
SeanMurp
 
Posts: 75
Joined: Wed Nov 02, 2016 4:30 am

Re: Applescript to copy Google Maps address to keywords

Postby Eric Nepean » Sun Nov 18, 2018 9:00 pm

Hi All
Here is Fancis's Applescript with the Aperture interface replaced by a Capture One interface.

I've copied the Google API code, but don't have the time o debug or check it. but if it was working before, it should be working now.

Code: Select all
tell application "Capture One 11"
   set imageSel to selected variants
   repeat with i from 1 to count of imageSel
      tell item i of imageSel
         set lat to get EXIF latitude of its parent image
         set long to get EXIF longitude of its parent image
         
         if (long is missing value) or (lat is missing value) then
            display dialog "Please select a geotagged image" with title "GPS data missing" buttons {"Okay"}
         else
            set lat to my set_decimal_delimiter_to_dot(lat as string)
            set long to my set_decimal_delimiter_to_dot(long as string)
            
               tell application "JSON Helper"
                  --set reverse_geocode_request to fetch JSON from ("https://maps.googleapis.com/maps/api/geocode/json?latlng=" & lat & "," & long & "&sensor=false")
                  set reverse_geocode_request to fetch JSON from ("https://maps.googleapis.com/maps/api/geocode/json?latlng=" & lat & "," & long & "&key=my_google_maps_api_key")
                  set address_components to get address_components of item 1 of results of reverse_geocode_request
                 
                  -- making a list (Array) of address component names
                  set address_components_list to {}
                  repeat with j from 1 to count of address_components
                     if item 1 of |types| of item j of address_components is in {"route", "sublocality", "locality", "administrative_area_level_1", "country"} then
                        copy long_name of item j of address_components to beginning of address_components_list --adding them at the beginning, sorts locations from general to specific
                     end if
                  end repeat
               end tell
            
            -- making and assigning a keyword for each item in the address components list
            -- each keyword will have the previous item(s) as a parent keyword
            -- this will create a nested list of location keywords
            repeat with k from 1 to count of address_components_list
               -- making a list (Array) of parent keywords from the previous item(s) in the address components list
               set parents_list to {}
               repeat with l from 1 to (k - 1)
                  copy item (k - l) of address_components_list to end of parents_list
               end repeat
               
               -- setting delimiter to "\t": so when parents_list get converted to String, a tab is inserted as the separator
               -- (the nested keywords for the 'parents' param expects a tab-separated list of parent keywords)
               set AppleScript's text item delimiters to "   "
               
               make new keyword with properties {name:item k of address_components_list, parents:parents_list as string}
            end repeat
         end if
         
      end tell
   end repeat
end tell

on set_decimal_delimiter_to_dot(this_text)
   set AppleScript's text item delimiters to ","
   set the text_items to every text item of this_text
   set AppleScript's text item delimiters to "."
   set this_text to the text_items as string
   set AppleScript's text item delimiters to ""
   return this_text
end set_decimal_delimiter_to_dot
Cheers, Eric
[late 2015 iMac, 4GHz i7, 24GB RAM, external SSDs. GX8, E-M1, GX7, GM5, GM1 ....]
Eric Nepean
 
Posts: 370
Joined: Sat Oct 25, 2014 8:02 am

Re: Applescript to copy Google Maps address to keywords

Postby Francesco Mariani » Wed Jan 02, 2019 9:33 pm

Eric, sorry for the very tardy reply. Thanks very much for offering the updated script. Happy New Year!
Olympus OM-D E-M5 Mark II
MacBook Pro - Retina, 15-inch, Mid 2014 - 2.5 GHz Intel Core i7 processor - 16 GB RAM
macOS Sierra - 10.12.6
Aperture 3.6 > Migrating to Capture One Pro 11 or 12
User avatar
Francesco Mariani
 
Posts: 39
Joined: Mon May 11, 2015 3:26 am
Location: Toronto, Ontario, Canada

Re: Applescript to copy Google Maps address to keywords

Postby Eric Nepean » Wed Jan 02, 2019 9:37 pm

Francesco Mariani wrote:Eric, sorry for the very tardy reply. Thanks very much for offering the updated script. Happy New Year!

No problem. Life sometimes changes your plans.

But - does it work now? - if it needed fixes, what did you change?
Cheers, Eric
[late 2015 iMac, 4GHz i7, 24GB RAM, external SSDs. GX8, E-M1, GX7, GM5, GM1 ....]
Eric Nepean
 
Posts: 370
Joined: Sat Oct 25, 2014 8:02 am


Return to Scripting



Who is online

Users browsing this forum: No registered users and 2 guests