How to find out the albums a photo is in: Jacques Rioux's script

by:  léonie
Last modified: Apr 18, 2018 9:25 AM
Rating: 5 out of 5
5 3 likes 5,787 views Last modified Apr 18, 2018 9:25 AM

Photo for Mac can show you the "moment" a photo is in, but has no tools to find the albums, that contain the photo.

User Jacques Rioux posted an Apple Script, that can list all albums that contain a selected photo. See this link:


Re: Re: How can I identify which albums photos are assigned to in Photos.


Here is a version of Jacques script with a few error checks added by me:

This version has been tested on macOS Sierra 10.12.4. Scroll down for a version to be used with El Capitan or earlier.


-- Snip below this line to copy to the script editor.

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


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


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 resultcaption to "Searching for: "

try


set sel to selection

if sel is {} then error "The selection is empty" -- no selection


on error errTexttwonumbererrNumtwo

display dialog "No photos selected " & errNumtwo & return & errTexttwo

return

end try


set imagename to "unknown filename"

try

set targettoitem 1 of sel-- the image to seach for

tell target

set imagename to the filename of target

end tell

on error errTexttwonumbererrNumtwo

display dialog "Cannot get the filename of the first image: " & errNumtwo & return & errTexttwo

end try

set resultcaption to (resultcaption & imagename)

end tell


try


display alertresultcaptionbuttons {"Cancel", "OK"} asinformationalgiving up after 2

on error errTextnumbererrNum

if (errNum is equal to -128) then


-- User cancelled.

return

end if

end try

-- From Jacques Rioux's script:

tell application "Photos"


-- set sel to selection

if sel is {} then return -- no selection

try

set thisId to id of item 1 of sel

on error errTextnumbererrNum

display dialog "Error: cannot get the image ID" & errNum & return & errText & "Trying again"


try

delay 2

set thisId to id of item 1 of sel

on error errTexttwonumbererrNumtwo

display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo

error "giving up"

return

end try --second attempt

end try


set theseNames to {}

try

set theseNames to name of (albums whose id of media items contains thisId)

on error errTextnumbererrNum

display dialog "Error: cannot get the albums" & errNum & return & errText & "Trying again"

try

delay 2

set theseNames to name of (albums whose id of media items contains thisId)

on error errTexttwonumbererrNumtwo

display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo

error "giving up"

return

end try

end try

end tell


if theseNames is not {} then

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

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


-- return oTid

else

set t to "No album"

end if

activate


set resultcaption to resultcaption & ", found it in these albums:

" & t as string

set the clipboard toresultcaption

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

returnresultcaption-- léonie

-- end copy to the script editor. -- -- ✄ - ✄ - ✄ - ✄ - ✄ - ✄ - ✄ - ✄ - ✄ - ✄ - ✄ - ✄ - ✄ -



--- Use the version below with an iCloud Photo Library on El Capitan or older system versions.


This script is working well for regular Photos Libraries.


However, AppleScripts may give timeout errors, if used with an iCloud Photo Library, because the "selection" will not work to pass the selected photos to the script.

For iCloud Photo Library it will help to use a work around posted by Nic Fletcher and to create a top level album with the selected photos, then pass the name of the album to the script.

See this link for a modified version of the script, that uses Nic's work-around:

Re: Re: Re: How can I identify which albums photos are assigned to in Photos. https://discussions.apple.com/thread/7413184?answerId=29601611022#29601611022


==========================================

Version 3, tested on MacOS X 10.11.2 with Photos 1.3

==========================================


The following version of the script is meant to be used with iCloud Photo Library, but will work also on a local library. The photo is passed to the script as the first item in a top level album called "PhotoDropBox". I added some error checks for timeouts , and if the script fails on the first try it will try a second time after a short delay. Following a suggestion by Old Toad the result panel now will display the filename of the photo that has been passed as the photo to the script.

The search result will be displayed in a dialog panel and also copied to the clipboard.


Run this script by pressing the "Run" button in the script editor or from the scripts menu.


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 theAlbumName to "PhotoDropBox" -- change this to the name of the album you will use

set resultcaption to "Searching for: "

try


if existscontainertheAlbumName then


set thePhotosBuffer to containertheAlbumName

set sel to every media item of thePhotosBuffer

if sel is {} then error "Album " & theAlbumName & " is empty" -- no selection

else

error "Album " & theAlbumName & "does not exist"

end if


on error errTexttwonumbererrNumtwo

display dialog "Cannot open album: " & errNumtwo & return & errTexttwo

return

end try


set imagename to "unknown filename"

try

set target to item 1 of sel-- the image to seach for

tell target

set imagename to the filename of target

end tell

on error errTexttwonumbererrNumtwo

display dialog "Cannot get the filename of the first image: " & errNumtwo & return & errTexttwo

end try

set resultcaption to (resultcaption & imagename)

end tell


try


display alertresultcaptionbuttons {"Cancel", "OK"} asinformationalgiving up after 2

on error errTextnumbererrNum

if (errNum is equal to -128) then


-- User cancelled.

return

end if

end try

-- From Jacques Rioux's script:

tell application "Photos"


-- set sel to selection

if sel is {} then return -- no selection

try

set thisId to id of item 1 of sel

on error errTextnumbererrNum

display dialog "Error: cannot get the image ID" & errNum & return & errText & "Trying again"


try

delay 2

set thisId to id of item 1 of sel

on error errTexttwonumbererrNumtwo

display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo

error "giving up"

return

end try --second attempt

end try


set theseNames to {}

try

set theseNames to name of (albums whose id of media items contains thisId)

on error errTextnumbererrNum

display dialog "Error: cannot get the albums" & errNum & return & errText & "Trying again"

try

delay 2

set theseNames to name of (albums whose id of media items contains thisId)

on error errTexttwonumbererrNumtwo

display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo

error "giving up"

return

end try

end try

end tell


if theseNames is not {} then

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

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


-- return oTid

else

set t to "No album"

end if

activate


set resultcaption to resultcaption & ", found it in these albums:

" & t as string

set the clipboard toresultcaption

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

return resultcaption-- léonie

Comments