Photos - Remove Photos from Album

Tell me if that was it. Otherwise, there might be a little more to it.

Edited: you never know.

Edited: the reason why I say this is I don’t have much time. I would have to create a new album and test on that. But, I’m washing clothes! :smiley: and hopefully can rest after that.

gl,
kel

Hi Poleary,

Did you get it to work?

Now I have some time and was thinking about trying out the ‘remove’ with the filter reference form. The part I didn’t like was removing every photo from an album. So, I have to crate a whole new small album and didn’t have time.

I’ll show you a good example if I can think of one.

Edited: btw, not sure but it read like you knew about AppleScript so I was very vague.

Later,
kel

Ok, what I was thinking is that say you have an album selected. So, what you want to do is remove every photo from that album. The selection doesn’t seem to cover albums. so, you need to somehow get your script to select an album.

I got it! You choose the album from the list or another way way might be to select a photo from an album and from there get the album. I kind of like the idea of selecting an album to delete items. Sounds good to me.

Edited: Ooo, It’s a little bit trickey.:smiley: Nice puzzle.

Later,
kel

I’m starting to think that the best way to do this is to go back to the original idea of deleting the album and creating a new album. Interesting though. What if I just wanted to delete every photo but the last one.

Darn, I can’t find the remove command. It must have been from iPhoto.:slight_smile: Ok, damn it. Gotta get serious here…:smiley:

Ok, think I found the secret. This reminds me of Mail when ti first came out. It seems the programmers don’t know about AppleScript very much.

I got it! Why the hell did they use container instead of album? Crazy man.

Darn, I remember now. Had to delete it from the database. Darn! That’s why I was asking how to delete items from the database. I think. Yeah, I think that was it. I remember something about sqlite or something like that.

The way Apple has it now, you can’t even hack your own computer!

Darn, I think you have to resort to uielement scripting to get rid of the photos.:frowning: It’s too bad because all you would actually need to do is delete it from the database, if.

gl,
kel

In the olden days you might find that database and make changes, but now days you might just as well wait and perhaps send in some feedback to Apple.

gl,
kel

Here’s as far as I got and then you need to search trough the database I think:

tell application "Photos"
	set the_album to first container whose name is "TestAlbum1"
	every media item of the_album
end tell

From there you need to find the database and modify it. And then you don;t know if that will even work I think.

gl,
kel

Hi there,

THANK YOU so much for your help. I wasn’t able to test it yet. Prior to your help, I got fed up with the speed of Photos on OS X. It was choking on my 60k picture library. I literally couldn’t do anything without it giving me the beachball of death.

I will definitely save this and use / try it out once I test it again under OS X El Capitain.

Thanks again, and sorry for the delay responding!

Hi poleary,

Actually, there is no solution as I remember. Maybe in the future there might be one.

gl,
kel

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