I’m trying to remove all the photos from an album but I can’t seem to find “remove” in the dictionary. I see “delete” but that is only for albums and folders.
I tried:
set target_album to album named target_album_name of container target_folder_name
repeat with photo in media items in target_album
remove photo
end repeat
Any thoughts? For now I am deleting the album and recreated. However i would prefer just to remove the media items.
tell application "Photos"
activate
set thePhotos to media item of album "Alexandra"
repeat with thePhoto in thePhotos
spotlight thePhoto
delay 1
tell application "System Events"
tell application process "Photos"
click menu bar item "Image" of menu bar 1
try
click menu item "Delete 1 Photo" of menu 1 of menu bar item "Image" of menu bar 1
on error
click menu item "Remove 1 Photo from album" of menu 1 of menu bar item "Image" of menu bar 1
end try
repeat while (sheet 1 of window 1 exists)
delay 0.1
end repeat
end tell
end tell
end repeat
end tell
NOTE: you can filter deleted photos with some criteria.
The following script automatically deletes all photos whose name contains “1”, without confirmation:
tell application "Photos"
activate
set thePhotos to media item of album "Alexandra"
repeat with thePhoto in thePhotos
if name of thePhoto contains "1" then
spotlight thePhoto
delay 1
tell application "System Events"
tell application process "Photos"
click menu bar item "Image" of menu bar 1
try
click menu item "Delete 1 Photo" of menu 1 of menu bar item "Image" of menu bar 1
on error
click menu item "Remove 1 Photo from album" of menu 1 of menu bar item "Image" of menu bar 1
end try
end tell
delay 0.5
keystroke return
end tell
end if
end repeat
end tell
The Last NOTE: The deleted photos goes to the folder “Recently Deleted”. This is a trash of Photos app. To permanently empty this trash you should add to end of the scripts above that:
tell application "System Events" to tell application process "Photos"
click static text 1 of UI element 1 of row 8 of outline 1 of scroll area 1 of splitter group 1 of window 1
repeat until (window 1 exists)
delay 0.1
end repeat
click UI element "Delete All" of group 1 of splitter group 1 of window 1
delay 0.5
click UI element "OK" of sheet 1 of window 1
end tell
So. Photos.app doesn’t have a delete command to delete all photos from some album. Earlier I suggested a GUI-scriping solution to this problem. But now, after thinking a little with my brains, I thought it would be better to do it like this, without any GUI-scripting:
set theAlbumName to "Untitled Album" -- note: on my Mac its container is folder "Robert"
tell application "Photos"
set theAlbum to album theAlbumName of folder "Robert"
delete theAlbum
make new album named theAlbumName at folder "Robert"
end tell
I have already shown above how 1) how to delete specific photos from an album using GUI scripting, 2) clear the album from all photos without GUI scripting.
Now, I would like to show 3) how to delete from album a specific selected photo without GUI scripting. That is, the same as 1), but without GUI scripting:
set theAlbumName to "Untitled Album" -- note: on my Mac its container is folder "Robert"
set tempAlbumName to theAlbumName & "_Temp"
tell application "Photos"
activate
-- create reference to original album
set theAlbum to album theAlbumName of folder "Robert"
-- remember the ID of first selected photo, to use later
set theID to id of item 1 of (get selection)
-- get list of rest media items
tell theAlbum to set others to every media item whose id is not theID
-- make temporary album, create reference, add "other" media items
make new album named tempAlbumName at folder "Robert"
set tempAlbum to album tempAlbumName of folder "Robert"
add others as list to tempAlbum -- as list is required here
-- delete original album
delete theAlbum
-- restore name to original album's name
set name of tempAlbum to theAlbumName
end tell
NOTE: The given script implies the indication of the full path to the album by the user. With recursive handler it is possible finding the full path to album of selected message automatically. I’ll figure out how to do it. Help from other users is also greatly appreciated. There is only one condition - no GUI scripting.
UPDATE.
Apple’s slap… There is no way to automatically detect the (selected) album of the selected photo. At least I didn’t find it. You can, of course, delete the selected photo from all albums, and someone will be happy with that. But this is not the best solution.
Hopefully in the future Apple will provide a parent property of selected message, or, better, selected album property of Photos.app. In the meantime, for those who are satisfied with deleting the selected photo from all albums, I give below a recursive script to get a list of all albums in the nested structure of Photos.app:
property theAlbums : {}
on run {}
tell application "Photos"
activate
set theAlbums to albums
repeat with aFolder in folders
my getAlbumsAndFolders(aFolder)
end repeat
end tell
return theAlbums
end run
on getAlbumsAndFolders(aFolder)
tell application "Photos"
set thisAlbums to albums of aFolder
if thisAlbums is not {} then
repeat with anAlbum in thisAlbums
set end of theAlbums to contents of anAlbum
end repeat
end if
set thisFolders to folders of aFolder
if thisFolders is not {} then
repeat with thisFolder in thisFolderss
my getAlbumsAndFolders(thisFolder)
end repeat
end if
end tell
end getAlbumsAndFolders