macOS Photos 和 AppleScript:更快速的整理方法

macOS Photos and AppleScript: Quicker organization method

目标: 在 macOS Photos App 中,highlight/select 大量照片,逐一迭代。 AppleScript 会告诉照片快速查看照片,询问要将照片添加到什么 'Album(s)',然后它会询问标题和标题。

我这样做是因为提供此功能的 'Info' 面板不允许快速输入,而且使用起来非常小。

所以我想我需要一些东西:

  1. 显示图片(告诉照片快速查看)
  2. 识别 'My Album' 文件夹(从组合中删除智能文件夹)
  3. 提示并更新标题和说明(在 def 中称为名称和说明)
  4. 将照片添加到所选相册。

我不确定是不是我一个人的问题,但照片上的 AppleScript 脚本不是我能很好理解的东西。

这是我目前所知道的,但我希望你能提供帮助。

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
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