Applescript 用于从导出图像的Apple脚本;照片;应用

Applescript 用于从导出图像的Apple脚本;照片;应用,applescript,automator,apple-photos,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

我在照片应用程序中有相册、嵌套相册和文件夹。我希望我的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停止运行脚本并返回错误“event timed out”。AppleScript不会取消操作,它只是停止脚本的执行。如果希望脚本继续,可以将语句包装在try语句中。但是,脚本是否可以发送命令以在超时后取消有问题的长时间操作取决于执行该命令的应用程序。有关try的更多信息。

格式错误和不必要的空行使代码无法阅读。更新了我的脚本。支票,请。我仍然无法获取最外层的文件夹/相册。您已对脚本进行了一些主要编辑。我想我回答了最初的问题,这是关于如何访问顶级文件夹/相册名称的指导。我用一个函数更新了答案,为您的输入创建了FoldersHanks。它在一些文件夹中运行良好,但没有创建新文件夹,然后超时。感谢您的输入。它在一些文件夹中运行良好,然后没有创建新文件夹,然后超时@床垫