Apple脚本从“照片”应用程序导出图像

时间:2019-11-28 22:28:26

标签: applescript automator apple-photos

我在“照片”应用程序中有相册以及嵌套的相册和文件夹。我希望我的applescript导出保持应用程序中具有相册或文件夹结构的图像。

我尝试了在线提供的脚本:

tell application "Finder"

    set location_1 to (choose folder with prompt "Choose a folder to export into") as text

end tell



tell application "Photos"

    set x to name of every folder

    choose from list x with prompt "Choose a project to export"

    set theP to item 1 of result

    tell application "Finder" to make new folder at alias location_1 with properties {name:theP}

    tell folder theP

        set initflist to every folder

        set initalist to every album

        if initflist is equal to {} then


            log "process albums"
            processAlbums(initalist, location_1 & theP) of me

        else

            if initalist is not equal to {} then

                log "process albums"
                processAlbums(initalist, location_1 & theP) of me

            end if

            log "process sub folders "
            processSfolders(initflist, (location_1 & theP)) of me

        end if

    end tell

end tell



on processAlbums(alist, apath)

    tell application "Photos"

        repeat with a in alist

            tell a

                set theimages to get media items of album a
                set thename to name of a

                tell application "Finder"

                    if not (exists folder thename in alias apath) then

                        make new folder at alias apath with properties {name:thename}

                    end if

                    set destination to apath & ":" & thename & ":"

                end tell

                with timeout of 6000 seconds

                    tell a

                        set settings to "JPEG - Original Size"

                        export theimages to alias destination

                    end tell

                end timeout

            end tell

        end repeat

    end tell

end processAlbums



on processSfolders(flist, fpath)

    tell application "Photos"

        repeat with a in flist

            try

                set thename to name of a

                tell application "Finder"

                    if not (exists folder thename in alias fpath) then

                        make new folder at alias fpath with properties {name:thename}

                    end if

                end tell

                tell a

                    set sAlist to every album

                    set sflist to every folder

                    if sflist is equal to {} then

                        processAlbums(sAlist, fpath & ":" & thename) of me

                    else

                        if sAlist is not equal to {} then

                            processAlbums(sAlist, fpath & ":" & thename) of me

                        end if

                        processSfolders(sflist, fpath & ":" & thename) of me

                    end if

                end tell

            on error errMsg
                log "error"
            end try

        end repeat


    end tell

end processSfolders

问题在于,它仅获取子相册的名称,而不获取顶级相册的名称。我必须维护相册或文件夹的整个结构。

我不知道AppleScript,所以我尝试调整它,但是到目前为止还没有运气。我可以得到指示吗?

1 个答案:

答案 0 :(得分:1)

您可以获得文件夹的名称以及该文件夹中包含的相册,这将使您能够创建顶层目录和子目录。相册只能包含媒体项目,而不能包含相册。顶级专辑被分类为文件夹或容器。在用于照片的Applescript词典中,它提供了这些项目的定义。

tell application "Finder"
set location_1 to (choose folder with prompt "Choose a folder to export into") as text
end tell

tell application "Photos"
    activate
    set fds to folders

    repeat with fd in fds
        set fdName to name of fd
        set abNames to every album of fd
        if parent of fd is missing value then
            my createFolders(fdName, abNames, location_1, fd)
        end if
    end repeat
end tell

on createFolders(fName, aAlbums, fPath, fd)

    tell application "Finder"
        if not (exists folder fName in alias fPath) then
            make new folder with properties {name:fName} at fPath
        end if

        repeat with a in aAlbums
            set aName to name of a
            set aPath to ((fPath as alias) as text) & fName
            if not (exists folder aName in alias aPath) then
                make new folder with properties {name:aName} at aPath
            end if
            set exPath to ((aPath as alias) as text) & aName

            my exportImages(a, exPath)

        end repeat
    end tell

    tell application "Photos"
        set rcFolders to every folder of fd
        repeat with rcFd in rcFolders
            set rcAlbums to every album of rcFd
            set rcName to name of rcFd
            set rcPath to ((fPath as alias) as text) & fName
            my createFolders(rcName, rcAlbums, rcPath, rcFd)
        end repeat
    end tell
end createFolders

on exportImages(photoAlbum, destination)
    tell application "Photos"
        set theimages to get media items of photoAlbum
        with timeout of 6000 seconds
            tell photoAlbum
                set settings to "JPEG - Original Size"
                export theimages to alias destination
            end tell
        end timeout
    end tell
end exportImages

编辑-错误处理

要处理错误,请找到导致错误的命令并将其包装在try块中。解决方案可能是退出应用程序,以便该过程结束,并可能添加短暂的延迟,然后继续执行脚本。

try
    export theimages to alias destination
on error
    -- statements to execute in case of error
    error "The exporting of images failed to complete"
    quit
end try

从开发人员参考中-当命令未能在指定的时间内完成(无论是默认的两分钟,还是使用with timeout语句设置的时间),AppleScript将停止运行脚本并返回错误“事件超时” ”。 AppleScript不会取消该操作-只是停止执行脚本。如果希望脚本继续运行,则可以将这些语句包装在try语句中。但是,您的脚本是否可以在超时后发送命令来取消令人讨厌的冗长操作,取决于执行该命令的应用程序。有关尝试statements的更多信息。