从 "Photos" 应用程序导出图像的 Apple 脚本

Apple script to export images from "Photos" application

我在照片应用程序中有相册和嵌套相册和文件夹。我希望我的 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,我尝试调整这个,但到目前为止没有成功。请问可以指路吗?

您可以获得文件夹的名称,以及文件夹中包含的相册,这将使您能够创建顶级目录和子目录。相册只能包含媒体项目,不能包含相册。顶级相册被分类为文件夹或容器。在照片的 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 停止 运行 脚本并 returns 错误 "event timed out"。 AppleScript 不会取消操作——它只是停止脚本的执行。如果您希望脚本继续运行,您可以将这些语句包装在一个 try 语句中。但是,您的脚本是否可以在超时后发送命令以取消有问题的冗长操作取决于执行该命令的应用程序。有关尝试 statements.

的更多信息