Script: Search for albums with a certain phrase in the name and combine them into a new album

by: 
Last modified: Jan 7, 2018 4:35 AM
1 2309 Last modified Jan 7, 2018 4:35 AM

Photos allows you to search for text, but it is difficult to open all albums with a certain phrase, like "Mexico" or "Antarctica" in the name at the same time. To combine all albums into one album, that contain a certain phrase in the name, you could run an Apple script.


The script below will do the following:

  • It will ask for string to search for in the album names, for example "Mexico".
  • Then it will create a list of all photos from the albums with "Mexico" in the name.
  • To return the found photos it will create a new album named like the search string you entered and add the photos to this album.
  • You will see the Script Editor bounce in the Dock, when it is done. Click it to open the dialog panel and dismiss it by clicking o.k.


To run the script, open Script Editor. Copy and paste the code below the dashed line into a new Script Editor document and press the run button.

A copy is in my Dropbox: Dropbox - Photos Search for Album Name.scpt

-- ✄ -- ✄ -- -----------------------------------------------------------


-- Select the photo in Photos, then run this script by pressing the "Run" button in the script editor or from the scripts menu.


-- ask for the search string

set SearchFor to text returned of (display dialog "Enter the text to search for in the album name: " default answer "new" buttons {"OK"} default button "OK")


tell application "Photos"


activate


-- Add the photo you want to search for to a top level album as the first item in the album



set albumsFound to (albums whose name contains SearchFor) -- list of all albums with "SearchFor" in the name

set albumNames to name of (albums whose name contains SearchFor)


set photosInMatchingAlbums to {}


repeat with thisAlbum in albumsFound

set theseItems to (the media items of thisAlbum) as list

set photosInMatchingAlbums to photosInMatchingAlbums & theseItems

end repeat


-- return photosInMatchingAlbums --testing


-- create an album with the photos from all albums

try

if not (exists container SearchFor) then


makenewalbumnamedSearchFor

end if

set theSearchForAlbum to containerSearchFor

on error errTexttwonumbererrNumtwo

display dialog "Cannot create new album " & SearchFor & errNumtwo & return & errTexttwo

end try


addphotosInMatchingAlbumstotheSearchForAlbum



end tell



if albumNames is not {} then

set {oTid, text item delimiters} to {text item delimiters, return} --

set {t, text item delimiters} to {albumNames as string, oTid}


-- return oTid

else

set t to "No album"

end if


set resultcaption to "Found " & SearchFor & " in these albums:

" & t & " and created a new Album " & SearchFor as string

set the clipboard toresultcaption-- add the album names to the Clipboard


display dialogresultcaptionbuttons {"OK"} default button "OK" -- you can press the Enter key or the return Key to close the dialog


return resultcaption-- léonie


This user tip was generated from the following discussion: Merge Photos.app albums all containing certain text

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.