【问题标题】:Apple script to export images from "Photos" application用于从“照片”应用程序导出图像的 Apple 脚本
【发布时间】:2019-12-15 07:55:38
【问题描述】:

我在照片应用程序中有相册、嵌套相册和文件夹。我希望我的 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 automator apple-photos


【解决方案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 的更多信息。

【讨论】:

  • 更新了我的脚本。请检查。我仍然没有得到最外层的文件夹/专辑。
  • 您对脚本进行了一些重大修改。我想我回答了最初的问题,即提供有关如何访问顶级文件夹/专辑名称的指导。
  • 我用创建文件夹的功能更新了答案
  • 感谢您的意见。它适用于某些文件夹,然后它没有创建新文件夹然后超时。
  • 感谢您的意见。它适用于某些文件夹,然后它没有创建新文件夹,然后它超时。 @马特斯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
相关资源
最近更新 更多