Sometimes, when editing photos, it’s useful to be able to copy the GPS location of one photo, and assign it to another. This was supported in iPhoto using the ‘Paste Location’ menu option, but not in Photos.

Using JavaScript—which is now a first-class citizen in macOS Automation—it’s easy to use JSON to transfer this data via the clipboard.

First, copy the details of the selected photo to the clipboard (date and location in this example):

photos = Application('Photos');
photos.includeStandardAdditions = true;
photos.activate();
item = photos.selection()[0];
details = {"location": item.location(), "date": item.date()};
photos.setTheClipboardTo(JSON.stringify(details));

Next, retrieve these same details from the clipboard, and update the newly-selected photo:

photos = Application('Photos');
photos.includeStandardAdditions = true;
photos.activate();
item = photos.selection()[0];
details = JSON.parse(photos.theClipboard());
item.location = details["location"];
item.date = new Date(details["date"]);

While JavaScript makes it wonderfully easy to implement this functionality, it may not be immediately apparent how to create a git-friendly plain text script file which macOS automation knows to treat as JavaScript. I’ve found the easiest solution is to create an executable script and specify the interpreter as  osascript, passing the language flag to explicitly specify the language as follows:

#!/usr/bin/osascript -l JavaScript

photos = Application('Photos');
...