Home > other >  macOS Photos and AppleScript: Quicker organization method
macOS Photos and AppleScript: Quicker organization method

Time:10-19

The Goal: In macOS Photos App, highlight/select a bulk of photos, iterate each one. AppleScript will tell Photos to quickview the photo, ask what 'Album(s)' to add the photo to, then it will ask for a title and caption.

I'm doing this because the 'Info' panel that provides this function doesn't allow for rapid entry, and it's pretty small to work with.

Info Panel for Photos

So I suppose I need a couple things:

  1. Show the picture (tell Photos to quick look it)
  2. Identify the 'My Album' folders (eliminate the smart folders from the mix)
  3. Prompt and Update the Title and the Caption (called name and description in the def)
  4. Add the photo to the selected Albums.

I'm not sure if it's just me, but the AppleScript scripting on Photos is just not something I can wrap my head around very well.

Here's what I have so far, but I was hoping you can help.

tell application "Photos"
    
    --Find All Albums
    set thefullList to the name of every album of every folder
                     -- Need:Figure out how to strip Smart Albums out of this query 

    -- Set comma as the delim to separate the folders/albums
    set oldDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ","
    set thefullList to every item of thefullList
    set AppleScript's text item delimiters to oldDelimiters
    
    -- Test the output (fails to delim, and won't display the string)
    display dialog thefullList
    

    -- Clean list of Albums 
    set albumNames to {thefullList}
    
    
    -- Get the selected photos from Photos
    set theSelection to (get selection)
    
    -- Iterate through each photo
    repeat with i in theSelection
        
    -- Tell Photos to 'quick look' 
                      -- Need:(no idea how to make that work yet)

    -- Ask the user to choose the Album(s) this photo should go into    
        set theAlbumChoice to choose from list albumNames with prompt "Where should this photo go?"
    
    -- Tell the Album that this photo is now added
        set theAlbum to theAlbumChoice
        add i to theAlbum
    
    -- Ask for Title and Caption
display dialog "What's the Title of this Photo?" 
set theTitle to text returned of result
        
display dialog "What's the Caption of this Photo?" 
set theCaption to text returned of result

    -- Get the Photo ID for adding MetaData
set selectionID to id of item i of theSelection

    -- Set the Title and caption
set name of media item id selectionID to theTitle
set description of media item id selectionID to theCaption

        
    end repeat
end tell

CodePudding user response:

property albumNames : {}
property albumIDs : {}

tell application "Photos"
    activate
    my getAlbumNames(it) -- get all album names and IDs (recursively)
    
    repeat with aPhoto in (get selection) -- process each selected photo
        
        try -- Get the Photo ID
            set selectionID to id of aPhoto
        on error errorMessage
            set ATID to AppleScript's text item delimiters
            set AppleScript's text item delimiters to "\""
            set selectionID to text item 2 of errorMessage
            set AppleScript's text item delimiters to ATID
        end try
        
        spotlight media item id selectionID -- Tell Photos to 'quick look'
        
        -- Ask the user to choose the Album(s) this photo should go into    
        set theAlbumChoice to choose from list albumNames with prompt "Where should this photo go?"
        if theAlbumChoice is false then return
        
        -- Find destination folder's ID and add photo to it
        repeat with k from 1 to count albumNames
            if (item 1 of theAlbumChoice) is (item k of albumNames) then
                set theAlbumID to item k of albumIDs
                exit repeat
            end if
        end repeat
        add {media item id selectionID} to album id theAlbumID
        
        -- Ask for Title and Caption, and set them
        display dialog "What's the Title of this Photo?" default answer ""
        set theTitle to text returned of result
        display dialog "What's the Caption of this Photo?" default answer ""
        set theCaption to text returned of result
        set name of media item id selectionID to theTitle
        set description of media item id selectionID to theCaption
    end repeat
    
end tell

on getAlbumNames(aFolder) -- recursive handler
    tell application "Photos"
        set albumNames to albumNames & name of albums of aFolder
        set albumIDs to albumIDs & id of albums of aFolder
        repeat with subFolder in (get folders of aFolder)
            my getAlbumNames(subFolder)
        end repeat
    end tell
end getAlbumNames
  • Related