Last active
August 14, 2020 05:15
-
-
Save RhetTbull/586f754cf4f1a90270a8d9c62239e193 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Find all photos in Apple photos with a detected face and | |
add them to a new album named """ | |
import osxphotos | |
# there's more than one applescript package on pypi | |
# the one you need is py-applescript: | |
# pip install py-applescript | |
from applescript import AppleScript | |
def create_album(album_name): | |
scpt = AppleScript( | |
""" | |
to create_album(albumName) | |
-- creates album named albumName | |
-- does not check for duplicate album | |
-- returns UUID of newly created album | |
tell application "Photos" | |
-- In Photos 5, uuid is in form DB7DED61-C0CC-4FC7-952C-CEA9E01AB106/L0/001 | |
-- but we need only the part before the "/" | |
set theVersion to version of application "Photos" | |
if theVersion ≥ 5 then | |
-- Set the new delimiter | |
set AppleScript's text item delimiters to "/" | |
end if | |
set theAlbum to make new album named albumName | |
set theID to ((id of theAlbum) as text) | |
if theVersion ≥ 5 then | |
set theID to text item 1 of theID | |
end if | |
return theID | |
end tell | |
end create_album | |
""" | |
) | |
return scpt.call("create_album", album_name) | |
def add_photos_to_album(album_uuid, photo_uuid_list): | |
scpt = AppleScript( | |
""" | |
on add_photos_to_album(albumUUID, photoUUIDList) | |
set photoUUIDList to photoUUIDList as list | |
tell application "Photos" | |
set theAlbum to album id (albumUUID) | |
set photoList to {} | |
repeat with thePhotoID in photoUUIDList | |
set thePhoto to media item id thePhotoID | |
copy thePhoto to the end of photoList | |
end repeat | |
add photoList to theAlbum | |
end tell | |
end add_photos_to_album | |
""" | |
) | |
scpt.call("add_photos_to_album", album_uuid, photo_uuid_list) | |
def main(): | |
photosdb = osxphotos.PhotosDB() | |
face_photos = [p.uuid for p in photosdb.photos() if p.face_info] | |
album_name = "My Face Album" | |
album_uuid = create_album(album_name) | |
print(f"created new album {album_name} with uuid {album_uuid}") | |
add_photos_to_album(album_uuid, face_photos) | |
print(f"Added {len(face_photos)} photos to album {album_name}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment