Add my script for getting photo sizes
- ID
30e394a- date
2024-01-09 20:27:17+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
ec4c856- message
Add my script for getting photo sizes- changed files
3 files, 76 additions, 1 deletion
Changed files
.gitattributes (164) → .gitattributes (204)
diff --git a/.gitattributes b/.gitattributes
index 80ccdb3..a3d8869 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1,4 +1,5 @@
close_tabs linguist-language=JavaScript
get_focus_mode linguist-language=JavaScript
get_live_text linguist-language=Swift
+get_photo_sizes linguist-language=Swift
set_accent_colour linguist-language=Swift
macos/README.md (4851) → macos/README.md (5190)
diff --git a/macos/README.md b/macos/README.md
index dd39276..851ae10 100644
--- a/macos/README.md
+++ b/macos/README.md
@@ -37,6 +37,10 @@ scripts = [
"description": "prints the current Focus mode"
},
{
+ "name": "get_photo_sizes",
+ "description": "print the size of every item in my Photos Library."
+ },
+ {
"name": "get_live_text [image]",
"description": "get OCR'd text for a single image using Live Text"
},
@@ -113,6 +117,15 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
</dd>
<dt>
+ <a href="https://github.com/alexwlchan/scripts/blob/main/macos/get_photo_sizes">
+ <code>get_photo_sizes</code>
+ </a>
+ </dt>
+ <dd>
+ print the size of every item in my Photos Library.
+ </dd>
+
+ <dt>
<a href="https://github.com/alexwlchan/scripts/blob/main/macos/get_live_text">
<code>get_live_text [image]</code>
</a>
@@ -160,4 +173,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
</p>
</dd>
</dl>
-<!-- [[[end]]] (checksum: b76affe4f83859dc60b4ef333666ce0f) -->
+<!-- [[[end]]] (checksum: 08c7ada61eb1b97fe419046dcccb8b1f) -->
macos/get_photo_sizes (0) → macos/get_photo_sizes (1544)
diff --git a/macos/get_photo_sizes b/macos/get_photo_sizes
new file mode 100755
index 0000000..d7afa0d
--- /dev/null
+++ b/macos/get_photo_sizes
@@ -0,0 +1,61 @@
+#!/usr/bin/env swift
+// Print the size of every item in my Photos Library.
+// See https://alexwlchan.net/2023/finding-big-photos/
+
+import Photos
+
+struct AssetData: Codable {
+ var localIdentifier: String
+ var originalFilename: String
+ var fileSize: Int64
+}
+
+/// Returns a list of assets in the Photos Library.
+///
+/// The list is sorted by file size, from largest to smallest.
+func getAssetsBySize() -> [AssetData] {
+ var allAssets: [AssetData] = []
+
+ let options: PHFetchOptions? = nil
+
+ PHAsset.fetchAssets(with: options)
+ .enumerateObjects({ (asset, _, _) in
+ let resource = PHAssetResource.assetResources(for: asset)[0]
+
+ let data = AssetData(
+ localIdentifier: asset.localIdentifier,
+ originalFilename: resource.originalFilename,
+ fileSize: resource.value(forKey: "fileSize") as! Int64
+ )
+
+ allAssets.append(data)
+ })
+
+ allAssets.sort { $0.fileSize > $1.fileSize }
+
+ return allAssets
+}
+
+/// Quick extension to allow left-padding a string in Swift
+///
+/// By user2878850 on Stack Overflow:
+/// https://stackoverflow.com/a/69859859/1558022
+extension String {
+ func leftPadding(toLength: Int, withPad: String) -> String {
+ String(
+ String(reversed())
+ .padding(toLength: toLength, withPad: withPad, startingAt: 0)
+ .reversed()
+ )
+ }
+}
+
+let bcf = ByteCountFormatter()
+
+for photo in getAssetsBySize() {
+ let size =
+ bcf
+ .string(fromByteCount: photo.fileSize)
+ .leftPadding(toLength: 8, withPad: " ")
+ print("\(size) \(photo.originalFilename)")
+}