Skip to content

Instantly share code, notes, and snippets.

@lincerely
Created March 17, 2023 19:27
Show Gist options
  • Save lincerely/5851b8497f0159c28e29f36f341ee90a to your computer and use it in GitHub Desktop.
Save lincerely/5851b8497f0159c28e29f36f341ee90a to your computer and use it in GitHub Desktop.
This script opens the album where the selected photo is in.
# Show in Album
#
# This script opens the album where the selected photo is in.
# If the photo is in multiple albums, it displays a list to choose from.
#
# It assumes:
# - no album with duplicate names; and
# - no folder/album with the same name in the same folder; and
# - your UI is in English, if not then you need to change the menu item name in the last part of the script.
#
# To install:
# - put this script inside `~/Library/Scripts/Applications/Photos`
# - In preferences of script editor, enable script menu in menu bar.
# - This script requires accessibility features to click through menu item to open album. In System Preference > Security & Privacy > Privacy, enable accessibility for 'Script Menu'.
#
# Tested in MacOS 10.14, Photos 4.0.
tell application "Photos"
set selectedPhotos to selection
if selectedPhotos is {} then return
set photo to item 1 of selectedPhotos
set photoID to id of photo
set thoseNames to name of (albums whose id of media items contains photoID)
if length of thoseNames is 1 then
set selectedAlbums to thoseNames
else
set selectedAlbums to (choose from list thoseNames with title "Search result" with prompt "Open:" without multiple selections allowed and empty selection allowed)
end if
if selectedAlbums is not false then
set the clipboard to item 1 of selectedAlbums
set selectedAlbum to album (item 1 of selectedAlbums)
set pathnames to {}
set lastpath to selectedAlbum
set end of pathnames to name of lastpath
repeat while parent of lastpath is not missing value
set lastID to id of lastpath
set thisParentFolder to item 1 of (folders whose id of containers contains lastID)
set lastpath to thisParentFolder
set end of pathnames to name of lastpath
end repeat
set pathnames to reverse of pathnames
tell application "System Events" to tell process "Photos"
set frontmost to true
set currentMenu to menu bar item "View" of menu bar 1
click currentMenu
set currentMenu to menu item "Albums" of menu 1 of currentMenu
click currentMenu
set currentMenu to menu item "My Albums" of menu 1 of currentMenu
click currentMenu
repeat with pathname in pathnames
set currentMenu to menu item pathname of menu 1 of currentMenu
click currentMenu
end repeat
end tell
end if
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment