循环浏览照片中的每张照片

Loop through every photo in Photos

我想清理我的照片库,特别是将我所有的 iPhone 屏幕截图移动到一个新相册,这样我就可以轻松浏览它们并删除我想要的内容。

为此我首先想在 Photos 中制作一个智能相册,但 Photos 似乎无法过滤图像的尺寸,所以我启动了 AppleScript Editor :)。

我创建了以下脚本,它适用于我创建的"Test Album":

tell application "Photos"

    set source_album_name to "Test Album"
    set target_album_name to "Screenshots"

    set target_width to 640
    set target_height to 1136

    if not (exists container named target_album_name) then
        make new album named target_album_name
    end if

    set target_album to container target_album_name
    set source_album to container source_album_name

    set imageList to {}

    repeat with photo in media items in source_album

        if width of photo = target_width and height of photo = target_height then
            set the end of imageList to photo
        end if

    end repeat

    add imageList to target_album

end tell

此脚本循环遍历名为 Test Album 的相册,并将高度和宽度与 iPhone 5S 的尺寸进行比较。当它们匹配时,它会将照片添加到 Screenshots 图库中。没问题。

现在我想 运行 我的整个照片集上的这个脚本,所以我将行 repeat with photo in media items in source_album 更改为 repeat with photo in every media item

一旦超过第一项 (Photos got an error: Can’t get item 2 of every media item. Invalid index),就会产生错误。

之后我将代码更改为:

set all_images to every media item
repeat with photo in all_images

但加载一段时间后,脚本退出并显示代码 -10000,可能是因为该库中的照片数量 (27.000)。

有没有办法像这样翻页?

编辑: 更改 set 行以包含更好的查询具有相同的效果,导致 AppleEvent handler failed 错误编号 -10000.

set all_images to every media item whose width = target_width and height = target_height

不确定您是否介意进入终端中的 shell,或者您是否可以使它与 iPhoto 一起工作,但如果没问题,这可能会让您开始查找 HOME 目录中的所有文件以下是 PNG 并且符合您的尺码...

#!/bin/bash
isIphone() {
   # Get width using sips, don't bother getting height if not 640 wide
   w=$(sips -g pixelWidth  "" | awk 'END{print }')
   [ "$w" != "640" ] && return
   # Get height using sips, just return if not iPhone size
   h=$(sips -g pixelHeight "" | awk 'END{print }')
   [ $h != "1136" ] && return
   # Output image size and name
   echo $w,$h, 
}

# Go through all PNG files in HOME directory listing iPhone-sized ones
find ~ -iname "*.png" -type f -print0 | while read -d $'[=10=]' -r file ; do isIphone "$file"; done

输出

640,1136, ./iPhone/2013 09 iPhone/IMG_0269.PNG
640,1136, ./iPhone/2013 09 iPhone/IMG_0363.PNG
640,1136, ./iPhone/2013 09 iPhone/IMG_0376.PNG

-10000 错误是由于照片 1.0 中的错误造成的。我已将其作为雷达 20626449 提交给 Apple(在 http://openradar.appspot.com/radar?id=6090497735000064 的 OpenRadar 上)。该错误是间歇性的,但库中的照片越多,在任何给定的脚本命令中出现的可能性就越大。

没有完全可靠的解决错误的方法,但似乎有帮助的一件事是,如果 "All Photos" 相册在启动脚本之前在照片中 select 编辑。不幸的是,Photos AppleScript 套件无法 select 相册,因此您需要在启动脚本之前手动执行此操作。