OSX照片,Applescript:循环时刻?

时间:2016-04-13 23:49:29

标签: applescript osx-elcapitan photos

我对AppleScript很陌生,但已经与其他脚本语言一起广泛使用(多年),所以对游戏来说并不是全新的。我很遗憾地告别了Aperture并将我近100K的数字图像导入了照片(所以我对照片也很陌生)。我想建立一个理智的结构来寻找图像,即

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的任何有用的引用,如如何遍历它们的列表,查找包含在其中的图像列表等等。看到用两个属性ID和名称定义的对象“时刻”;但我正在挣扎着试图找到所有时刻的主列表或时刻的内容。

我觉得来自熟悉这个应用程序(以及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

2 个答案:

答案 0 :(得分:1)

我在优胜美地(10.10.5)上为照片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

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

答案 1 :(得分:1)

Current working version

代码现在太长了,无法在此发布,但感谢您的起点!它现在还在运行 - 当然,这很慢,但似乎很有效。我的照片库中有超过97K的图像,所以这可能需要数天。 [更新:10000个图像每2个小时,所以大约18个小时] AS语法仍然让我觉得奇怪的是:-)但是多亏了你的示例代码我可以将PDQ打包在一起,我希望能解决这个问题。

[更新]是的,它有效。大约20小时的运行时间,零错误,看起来像成功。非常感谢代码示例。问题解决了。