Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
find photos with an id and move it to a Photos.app album. JXA.
#!/usr/bin/osascript -l JavaScript
// -*- JavaScript -*-
var idJson = {
"errors": [],
"ids": {
"48.jpg": [
"fjjIN4YbTDuca24C2%AseQ"
],
}
}
function findAlbum(photos, name) {
for (var i = 0; i < photos.albums.length; i++) {
var album = photos.albums[i];
console.log("finding album ", name, "... looking at:", album.name())
if (album.name() == name) {
return album;
}
}
return null;
}
function findItemAndAddToAlbum(photos, filename, ids, dupesAlbum) {
var mediaItems = photos.search({for: filename})
if (mediaItems.length) {
var toAdd = []
for (var i = 0; i < mediaItems.length; i++) {
var mediaItem = mediaItems[i]
if (ids.indexOf(mediaItem.id()) != -1) {
toAdd.push(mediaItem)
}
}
if (toAdd.length > 0) {
console.log("adding ", toAdd.length, " for ", filename)
photos.add(toAdd, {to: dupesAlbum})
} else {
console.log("no matching ids found for ", filename)
}
} else {
console.log("not found", filename)
}
}
function handleIds(photos, ids, dupes) {
var filenames = Object.keys(ids)
for (var i = 0; i < filenames.length; i++) {
var filename = filenames[i]
findItemAndAddToAlbum(photos, filename, ids[filename], dupes)
}
}
var photos = Application("Photos") // eslint-disable-line
if (photos.running()) {
var dupes = findAlbum(photos, "DUPES")
handleIds(photos, idJson.ids, dupes)
} else {
console.log("Photos is not running!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment