applescript - OSX 照片,Applescript : loop through Moments?

标签 applescript osx-elcapitan photos

我对 AppleScript 非常陌生,但已经与其他脚本语言进行了广泛的合作(多年),所以对这个游戏来说并不是完全陌生的。我刚刚悲伤地告别了 Aperture,并将我近 100K 的数字图像导入到 Photos 中(所以我对 Photos 也是全新的)。我想建立一个合理的结构来查找图像,即

Folders named for Year (e.g. 2004)
contain Folders named for Month (e.g. 05)
contain Albums named for each day for which there are images (e.g. 2004-05-03)
(yes this is basically the Picasa convention, which I am accustomed to and like)

将我的 100K 图像导入“照片”后,显然已经成功,我发现它们默认组织为“时刻”(即日期),但没有分层组织来使浏览更容易,如上所述。所以我想编写一个AppleScript来执行以下操作:

foreach Moment
   parse the date and create folder for year if it does not exist already
   create folder inside year for month if not exists already
   create album inside month for day, if not exists already
   assign all photos in this Moment to that album

看起来很简单,手动需要花费数十个小时:-)所以除非我可以编写脚本,否则永远不会发生。我一直在阅读(看似相当有限的)AppleScript 照片词典,到目前为止还没有看到任何对 Moments 有用的引用,例如如何循环遍历所有列表、查找其中包含的图像列表等。查看对象“moment”定义了两个属性:ID 和 name;但我正在努力寻找所有时刻的主列表或时刻的内容。

我有一种感觉,熟悉这个应用程序(和 AppleScript)的人提供的 2 段或一段示例代码可以节省我几个小时的谷歌搜索时间。有谁知道该怎么做吗?

[更新]

我尝试过 DLing 并安装一个有前途的照片脚本库

https://photosautomation.com/scripting/script-library.html

但到目前为止还无法执行它的任何函数。我还尝试了对照片最基本的探索性命令:

tell application "Photos"
    activate
    set moms to the moments of application
    display dialog moms
end tell

并收到一条不太有启发性的错误消息:

error "Can’t get every moment of application." number -1728 from every «class IPmm» of application

最佳答案

我在 Yosemite (10.10.5) 上为 Photos 1.0.1 编写了这篇文章,因此您可能需要进行一些修改,但这是我能做的最好的事情,希望它能在您的操作系统上运行。我试图为您评论它,以便您能够看到它在做什么。

on run
    tell application "Photos"
        set mediaItems to every media item
        repeat with mediaItem in mediaItems
            set mdate to (date of mediaItem) -- get the date of the file
            set yearName to year of mdate as string -- year of the file
            set YrFolder to my yearFolder(yearName) -- make the year folder

            set mmonth to month of mdate -- month of the file
            set monthName to my monthNum(mmonth as string) -- month name as a number
            set SubFold to my subFolder(monthName, YrFolder) -- make the month number folder

            set mday to day of mdate -- day of the file
            set dayName to my dayNum(mday as string) -- get the day as a two digit number
            set albumName to yearName & "-" & monthName & "-" & dayName as string -- create the album name
            set finalAlbum to my makeAlbum(albumName, SubFold) -- make the album

            add {mediaItem} to finalAlbum -- put the item in the album
        end repeat
    end tell

end run


on dayNum(mday)
    if (count of characters in mday) = 1 then
        return "0" & mday as string
    else
        return mday as string
    end if
end dayNum

on monthNum(mmonth)
    if mmonth = "January" then
        set num to "01"
    else if mmonth = "February" then
        set num to "02"
    else if mmonth = "March" then
        set num to "03"
    else if mmonth = "April" then
        set num to "04"
    else if mmonth = "May" then
        set num to "05"
    else if mmonth = "June" then
        set num to "06"
    else if mmonth = "July" then
        set num to "07"
    else if mmonth = "August" then
        set num to "08"
    else if mmonth = "September" then
        set num to "09"
    else if mmonth = "October" then
        set num to "10"
    else if mmonth = "November" then
        set num to "11"
    else if mmonth = "December" then
        set num to "12"
    end if
    return num
end monthNum


on makeAlbum(albName, theFolder)
    tell application "Photos"
        set yrFold to name of parent of theFolder
        if exists container albName of container (name of theFolder) of container yrFold then
            return (container albName of container (name of theFolder) of container yrFold)
        else
            return make new album named albName at theFolder
        end if
    end tell
end makeAlbum


on subFolder(subName, YrFolder)
    tell application "Photos"
        if exists container subName of container (name of YrFolder) then
            return container subName of container (name of YrFolder)
        else
            return make new folder named subName at YrFolder
        end if
    end tell
end subFolder

on yearFolder(yearName)
    tell application "Photos"
        if container named yearName exists then
            return container named yearName
        else
            try
                return make new folder named yearName
            on error
                return ""
            end try
        end if
    end tell
end yearFolder

祝你好运,很想知道它是否适合你。

关于applescript - OSX 照片,Applescript : loop through Moments?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36611477/

相关文章:

php - 从 Ruby 或 PHP 或 Cocoa 访问 Mac 应用程序

.net - 如何使用 .NET 检索在数码相机上拍摄的照片的属性?

environment-variables - 通过 launchd.conf 设置环境变量在 OS X Yosemite/El Capitan/macOS Sierra/Mojave 中不再有效?

ios - Xcode 6.4 Scheme 选项在 El Capitan Beta 中消失

android - 用相机拍照时android会广播吗

php - 手机中的图片方向

objective-c - 如何使用代码旋转 iOS 模拟器?

c++ - Finder 列 View 修改事件列的宽度

applescript - 如何使用AppleScript获取最前面的应用程序的名称并使用它来获取文件路径

Python3 与输入函数和 CJK 的意外视觉交互