Skip to content

Instantly share code, notes, and snippets.

@javahippie
Created October 17, 2021 19:59
Show Gist options
  • Save javahippie/df5c0b982aecc645433f0fcdc63762d5 to your computer and use it in GitHub Desktop.
Save javahippie/df5c0b982aecc645433f0fcdc63762d5 to your computer and use it in GitHub Desktop.
Exports my 20 "most symmetric" pictures from Apple Photos as files. "Most symmetric", according to Apples internal machine learning results.
#!/usr/bin/env bb
(require '[babashka.pods :as pods])
(pods/load-pod 'org.babashka/go-sqlite3 "0.0.1")
(require '[pod.babashka.go-sqlite3 :as sqlite])
(require '[clojure.java.shell :refer [sh]])
(def sqlite-path "/users/zoeller/Pictures/Photos Library.photoslibrary/database/Photos.sqlite")
(def find-by-ml ["select asset.Z_PK, asset.ZUUID
from ZCOMPUTEDASSETATTRIBUTES attr
left join ZASSET asset
on attr.ZASSET = asset.Z_PK
order by ZPLEASANTSYMMETRYSCORE desc
limit 20"])
(defn as-applescript [script params]
(reduce #(str %1 " " %2)
(for [token script]
(cond
(keyword? token) (str "\"" (get params token) "\"")
(char? token) token
:else (name token)))))
(def applescript '[tell application :photos \newline
set theitem to media item id :uuid \newline
set thePath to POSIX path of :path \newline
export \{theitem\} to thePath \newline
end tell])
(defn export-photo [uuid path]
(sh "osascript" "-e"
(-> (as-applescript applescript
{:photos "Photos"
:uuid uuid
:path path}))))
(let [nicest-photos
(sqlite/query sqlite-path find-by-ml)]
(for [{:keys [ZUUID]} nicest-photos]
(if (= 0 (:exit (export-photo ZUUID "/Users/zoeller/Desktop/test")))
(str "Exported " ZUUID)
(str "Did not export " ZUUID))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment