Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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.

User uploaded file

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

Posted on May 25, 2017 12:49 AM

Reply
7 replies

Jun 1, 2017 12:14 PM in response to some☺︎user☺︎name

Here we go: This script will take the selected albums and create two albums, one with all photos taken at a lower altitude than a given threshold, and one with photos taken at a higher altitude.

An additional album "Skipped" will contain items without an altitude tag.


User uploaded file

-- copy from here to the script editor

(*

This script will take a set of photos selected in Photos and then search for photos taken at an altitude higher than a given altitude threshold


How to use this script:


Select some photos to be searched for photos taken at a high altitude


This script will split the selection and add the photos to two albums:

- HighAltitudeAlbum: an album with photos taken at a higher altitude than the threshold

- LowAltitudeAlbum: an album with photos taken at a lower altitude than the threshold


Open this script in Script Editor. Launch Photos.

Select the Photos you want to distribute between the albums.


When all all photo are selected, press the "Run" button in Script Editor.


Author: léonie

*)


set defaultAltitudeThreshold to 1000 -- (1000 m) -- high altitude starts at 1000 m

set dialogResult to display dialog ¬

"Enter the minimum altitude in meters : " buttons {"Cancel", "OK"} ¬


default answer (defaultAltitudeThreshold as text)

set AltitudeThreshold to ((text returned of dialogResult) as real)


--return AltitudeThreshold


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 splitPhotosByAltitude(minAltitude, selectedPhotos)


-- the altitude, real number


-- altiAlbumName: the name of the album to collect the photos, string


-- selectedPhotos: a list of media items


-- DistanceThreshold: the threshold for the distance from the circle, in km, real number

set lowAltiAlbumName to "Altitude lower than " & minAltitude & " m"

set highAltiAlbumName to "Altitude higher than " & minAltitude & " m"


set lowAltis to {} -- the list of altitude photos

set highAltis to {} -- the list of altitude photos


set skipped to {} -- not yet used, just for debugging

if selectedPhotos is {} then

error "Please select some images."

end if


tell application "Photos"


activate


-- Ensure that the albums do exist


try

if not (exists container highAltiAlbumName) then


makenewalbumnamedhighAltiAlbumName

end if

set theHighAltiAlbum to containerhighAltiAlbumName


if not (exists container lowAltiAlbumName) then


makenewalbumnamedlowAltiAlbumName

end if

set theLowAltiAlbum to containerlowAltiAlbumName


if not (exists container "SkippedAltitudes") then

make new album named "SkippedAltitudes"

end if

set theSkippedAltiAlbum to container "SkippedAltitudes"



on error errTexttwonumbererrNumtwo


display dialog "Cannot open albums: " & lowAltiAlbumName & " " & highAltiAlbumName & 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 altitude

set alti to get its altitude--retrieve the altitude

end tell

on error errText number errNum

display dialog "Error: " & errNum & return & errText & "Trying again"

try

delay 2

tell im

set alti to get its altitude --retrieve the altitude

end tell

on error errTexttwonumbererrNumtwo

display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo

end try


end try

set noAltitude to (alti is missing value)


if noAltitude then

set skipped to {im} & skipped -- for testing

else

set alti to alti as number


if (alti < minAltitude) then

set lowAltis to {im} & lowAltis

else

set highAltis to {im} & highAltis


end if


end if


end repeat



addlowAltistotheLowAltiAlbum


addhighAltistotheHighAltiAlbum


addskippedtotheSkippedAltiAlbum

return "low altitude photos: " & (length of lowAltis) & ", high altitude photos: " & (length of highAltis) & ", skipped photos: " & (length of skipped)

end if


end tell


end splitPhotosByAltitude


-- call the script for the circles you want


set altisFound to splitPhotosByAltitude(AltitudeThreshold, imageSel)

-- Published as a user Tip here: Script: Split a set of selected photos by the altitude EXIF tag into two albums

May 31, 2017 5:04 AM in response to some☺︎user☺︎name

Thank you! I'll have to think about the altitude search.

You can read the altitude already, using this script: Script: Extract the Numerical GPS Values Using an Apple Script


It will copy the latitude, longitude, and the altitude in meter to the clip board.


If I find a solution to search for photos taken at a certain altitude, I'll append it to this User Tip:

Major circles of Latitude: Searching for GPS coordinates in Photos using an Apple Script

May 25, 2017 12:57 AM in response to léonie

I had to post this as question to be able to include the AppleScript code formatted. For some weird reason it has not been possible to paste the code directly into the user tip and format it by applying the paragraph style. But posting it as a question and then converting the question to a user tip preserves the editing of the script.

Does anybody know a work-around to format code directly in a new user tip, now that the paragraph style does no longer work?

Major circles of Latitude: Searching for GPS coordinates in Photos using an Apple Script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.