Skip to main content

start fleshing out the Swift scripts

ID
4e37d29
date
2023-05-07 20:59:54+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
001cb09
message
start fleshing out the Swift scripts
changed files
2 files, 101 additions

Changed files

get_albums_for_photo.swift (0) → get_albums_for_photo.swift (1023)

diff --git a/get_albums_for_photo.swift b/get_albums_for_photo.swift
new file mode 100644
index 0000000..f833996
--- /dev/null
+++ b/get_albums_for_photo.swift
@@ -0,0 +1,46 @@
+#!/usr/bin/env swift
+// Print a list of album names that contain this photo.
+//
+// This takes one arguments: the UUID of the photo.
+//
+// == Usage ==
+//
+//    $ swift get_albums_for_photo.swift 9D28ABBE-79F6-402F-8750-8674840EDA3D
+//    ["Flagged"]
+//
+
+import Photos
+
+func getPhotoWith(uuid: String) -> PHAsset {
+  let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [uuid], options: nil)
+
+  if fetchResult.count == 1 {
+    return fetchResult.firstObject!
+  } else {
+    fputs("Unable to find photo with ID: \(uuid).\n", stderr)
+    exit(1)
+  }
+}
+
+let arguments = CommandLine.arguments
+
+if arguments.count != 1 {
+  fputs("Usage: \(arguments[0]) PHOTO_ID\n", stderr)
+  exit(1)
+}
+
+let photo = getPhotoWith(uuid: arguments[1])
+
+let collections = PHAssetCollection.fetchAssetCollectionsContaining(
+  photo, with: .album, options: nil
+)
+
+var titles: [String] = []
+
+collections.enumerateObjects({ (album, index, stop) in
+  if (album.localizedTitle != nil) {
+    titles.append(album.localizedTitle!)
+  }
+})
+
+print(titles)

get_photos.swift (0) → get_photos.swift (1351)

diff --git a/get_photos.swift b/get_photos.swift
new file mode 100644
index 0000000..cf9d51a
--- /dev/null
+++ b/get_photos.swift
@@ -0,0 +1,55 @@
+#!/usr/bin/env swift
+// Print a list of album names that contain this photo.
+//
+// This takes one arguments: the UUID of the photo.
+//
+// == Usage ==
+//
+//    $ swift get_albums_for_photo.swift 9D28ABBE-79F6-402F-8750-8674840EDA3D
+//    ["Flagged"]
+//
+
+import Photos
+
+let options = PHFetchOptions()
+options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
+options.fetchLimit = 10
+
+let assets = PHAsset.fetchAssets(with: options)
+print(assets.count)
+
+print(assets.firstObject!)
+
+// func getPhotoWith(uuid: String) -> PHAsset {
+//   let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [uuid], options: nil)
+//
+//   if fetchResult.count == 1 {
+//     return fetchResult.firstObject!
+//   } else {
+//     fputs("Unable to find photo with ID: \(uuid).\n", stderr)
+//     exit(1)
+//   }
+// }
+//
+// let arguments = CommandLine.arguments
+//
+// if arguments.count != 1 {
+//   fputs("Usage: \(arguments[0]) PHOTO_ID\n", stderr)
+//   exit(1)
+// }
+//
+// let photo = getPhotoWith(uuid: arguments[1])
+//
+// let collections = PHAssetCollection.fetchAssetCollectionsContaining(
+//   photo, with: .album, options: nil
+// )
+//
+// var titles: [String] = []
+//
+// collections.enumerateObjects({ (album, index, stop) in
+//   if (album.localizedTitle != nil) {
+//     titles.append(album.localizedTitle!)
+//   }
+// })
+//
+// print(titles)