Script: Share Location Between Images in Photos Using an Apple Script
Returning from holiday this spring I wanted to write an Apple Script to improve the workflow of a task I tend to do every time I return from holiday: Copy location data from photos taken with my iPhone to photos taken with cameras without GPS capability. I found a solution presented by leonié in the thread Script: Extract the Numerical GPS Values Using an Apple Script that nearly did what I wanted.
The script below is a modification to that Apple Script by leonié. The Apple Script lets you select a group of photos in Photos. If one (and only one) photo got location data the location data is shared with the other photos in the selection. If none of the selected photos or more than one got location data an error message is shown.
--Andreas Severinsen, 2018
-- The script let you select a group of photos in Photos. If one (and only one) photo got location data this is shared with the other photos in the selection. If none of the selected photos or more than one got location data an error message is shown.
--This script is based on the script "Copy GPS tags from one Photo to Other Photos" by léonie posted on Apple Support Communities: https://discussions.apple.com/thread/7836908
tell application "Photos"
activate
set imageSel to (get selection)
if (imageSel is {}) or (the length of imageSel < 2) then
error "Please select at least two images."
else
set locationFound to false
set locationImage to {}
try --Find photo with location. Return error if there are none, multiple or if the location is invalid
repeat with i from 1 to count of imageSel
set next_image to item i of imageSel
tell next_image
set loc to get the location --retrieve longitude and latitude as list
if loc is not {missing value, missing value} then
if locationFound is true then
error "More than one image in selection got location."
else
set locationFound to true
set locationImageNum to i
end if
end if
end tell
end repeat
if locationFound is false then
error "No images in the selection got location."
end if
set locationImage to item locationImageNum of imageSel
tell locationImage
set loc to get the location
end tell
set lati to (the first item of loc) -- as number
set longi to (the second item of loc) -- as number
if (lati > 90.0) or (lati < -90.0) then
error "Latitude out of range " & lati
end if
if (longi > 180.0) or (longi < -180.0) then
error "Longitude out of range " & longi
end if
on error errTexttwo number errNumtwo
display dialog "Error: " & errNumtwo & return & errTexttwo
return
end try
repeat with i from 1 to count of imageSel
set next_image to item i of imageSel
if next_image is not locationImage then
tell next_image
set its location to {lati, longi}
end tell
end if
end repeat
end if
end tell
-- Code for error handling
-- return {loc, "Done"}