Major circles of Latitude: Searching for GPS coordinates in Photos using an Apple Script
Have you ever tried to find in Photos all photos taken while standing on the Equator or at the Arctic circle or similar?
It is easy to search for photo in Photos by the locations, if we see the places markers on the map or we can guess the name of the location that Photos assigned automatically. But typing the GPS coordinates into the search field simply does not work. We cannot use the GPS coordinates in smart albums either.
But we can search for GPS coordinates with the help of an Apple Script. The Photos dictionary gives access to these properties of the media items:
altitude (real) : The GPS altitude in meters.
location (list of real) : The GPS latitude and longitude, in an ordered list of 2 numbers. Latitude in range -90.0 to 90.0, longitude in range -180.0 to 180.0.
The location property can be read and written since macOS 10.12 Sierra, the altitude seems still to be read-only (at least I am getting continually error messages when I try to assign an altitude tag to a photo).
Just for the fun of it and as an example for scripting the GPS properties I wrote this little script to search for photos taken along the five major circles of latitude: the equator, the Arctic Circle, the Antarctic Circle, the Tropic of Capricorn, the Tropic of Cancer.
It will find all photos taken within a given distance of one of the five major circles of latitude and create five albums - one for each circle. The script is ignoring that the location of these circles is depending on the date and the circles are moving slowly due to the nutation. So expect the results to be off a few meters for older photos. These albums will be created when the script is running. It may take a few minutes for a large library. I select the photos in a smart album with the rule "Photos is tagged with GPS" to avoid unnecessary checks.
-- snip : copy from below this line to the end
(*
This script will take a set of photos selected in Photos and then search for photos taken close to the five major circles of latitude - the equator, the Arctic Circles, the tropics
*)
(* How to use this script:
Select some photos to be searched for photos that have been taken close to one of the five major circles of latitude
This script will split the selection and add the photos to six albums:
- EquatorPhotosAlbum: an album with photos taken close to the equator
- TropicOfCapricornAlbum: an album with photos taken close to the southern tropic - the tropic of capricorn
- TropicOfCancerAlbum: an album with photos taken close to the norther tropic - the tropic of cancer
- ArcticCircleAlbum: an album with photos taken close to the northern Arctic circle
- AntarcticCircleAlbum: an album with photos taken close to the nsouthern Arctic circle
Open this script in Script Editor. Launch Photos.
Select the Photos you want to distribute between the albums.
A photo is considered to be taken close to one of the circles, if the distance of the location is less than the distance threshold in km.
When all all photo are selected, press the "Run" button in Script Editor.
Author: léonie
*)
-- The latitudes of the major five circles of latitude - the script does not take into account, that these circles are shifting because of the motion of the axis of the earth
set TropicOfCapricornLatitude to -23.43703 --latitude of the southern tropic.
set TropicOfCancerLatitude to 23.43703 --latitude of the northern tropic.
set ArcticCircleLatitude to 66 + 33 / 60 + 46.7 / 3600 -- -66°33′46.7″
set AntarcticCircleLatitude to -ArcticCircleLatitude
set EquatorLatitude to 0.0
set defaultDistanceThreshold to 10.0 -- (10 km) allow 10 km distance from the circle, when searching for photos taken there.
set km2degreeLatitude to 1.0 / 111.32
set dialogResult to display dialog ¬
"Enter the distance threshold for the distance from the circle in km : " buttons {"Cancel", "OK"} ¬
default answer (defaultDistanceThreshold as text)
set DistanceThreshold to ((text returned of dialogResult) as real) * km2degreeLatitude
--return DistanceThreshold
tell application "Photos"
activate
try
set imageSel to (get selection)
on error errTexttwonumbererrNumtwo
display dialog "Cannot get the selection: " & errNumtwo & return & errTexttwo
end try
end tell
on findPhotosInLatitudeRange(latitudeOfCircle, latiAlbumName, selectedPhotos, DistanceThreshold)
set thisLatis to {} -- the list of portrait photos
set skipped to {}
set lowerBound to latitudeOfCircle - DistanceThreshold
set upperBound to latitudeOfCircle + DistanceThreshold
set latiValues to {}
tell application "Photos"
activate
-- Ensure that the albums do exist
try
if not (exists container latiAlbumName) then
makenewalbumnamedlatiAlbumName
end if
set theLatiAlbum to containerlatiAlbumName
on error errTexttwonumbererrNumtwo
display dialog "Cannot open albums: " & latiAlbumName & errNumtwo & return & errTexttwo
end try
-- check, if the album or the selected photos do contain images
if selectedPhotos is {} then
error "Please select some images."
else
repeat with im in selectedPhotos
try
tell im--get the pixel size
set loc to get the location--retrieve longitude and latitude as list
set lati to (the first item of loc) -- as string
-- set longi to (the second item of loc) as string
end tell
on error errText number errNum
display dialog "Error: " & errNum & return & errText & "Trying again"
try
delay 2
tell im
set loc to get the location--retrieve longitude and latitude as list
set lati to (the first item of loc) -- as string
-- set longi to (the second item of loc) as string
end tell
on error errTexttwonumbererrNumtwo
display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo
end try
end try
set noLocation to (lati is missing value)
if noLocation then
set skipped to {im} & skipped -- for testing
else
set lati to lati as number
if (lati < upperBound) and (lati > lowerBound) then
set thisLatis to {im} & thisLatis
-- set latiValues to {lati} & latiValues
end if
end if
end repeat
addthisLatistotheLatiAlbum
-- return latiValues & {lowerBound, upperBound}
end if
end tell
end findPhotosInLatitudeRange
findPhotosInLatitudeRange(EquatorLatitude, "EquatorPhotosAlbum", imageSel, DistanceThreshold)
findPhotosInLatitudeRange(TropicOfCapricornLatitude, "TropicOfCapricornAlbum", imageSel, DistanceThreshold)
findPhotosInLatitudeRange(TropicOfCancerLatitude, "TropicOfCancerAlbum", imageSel, DistanceThreshold)
findPhotosInLatitudeRange(ArcticCircleLatitude, "ArcticCircleAlbum", imageSel, DistanceThreshold)
findPhotosInLatitudeRange(AntarcticCircleLatitude, "AntarcticCircleAlbum", imageSel, DistanceThreshold)
MacBook Pro (Retina, 15-inch, Mid 2015), macOS Sierra (10.12.5), 2,8 GHz Intel Core i7, 16 GB, 1TB