【问题标题】:Loop through every photo in Photos循环浏览照片中的每张照片
【发布时间】:2015-05-02 04:26:32
【问题描述】:

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

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

我创建了以下脚本,它适用于我创建的“测试专辑”:

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

【问题讨论】:

  • every media item 是否可能包括,比如音轨,它可能没有高度/宽度?​​
  • 嗯,它是媒体项目对象的一个​​属性,我猜这个值会是 0 或者至少不包含在查询中。但是当它崩溃时,我会尝试弄清楚。
  • 问题是widthheight 属性是整数,不能是missing value。 (也试过了,它在第一条记录处崩溃,整数到缺失值的转换错误)。

标签: macos applescript


【解决方案1】:

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

没有完全可靠的方法来解决该错误,但似乎有帮助的一件事是,如果在开始脚本之前在“照片”中选择了“所有照片”相册。不幸的是,Photos AppleScript 套件无法选择相册,因此您需要在启动脚本之前手动执行此操作。

【讨论】:

  • 谢谢。我认为这确实是一个错误。一直在尝试解决它,但它现在是随机发生的。
【解决方案2】:

不确定您是否介意在终端中放入 shell,或者您是否可以使用 iPhoto 进行此操作,但如果没问题,这可能会让您开始查找 HOME 目录及以下目录中的所有文件,即 @ 987654321@s 和你的尺寸相匹配...

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

# Go through all PNG files in HOME directory listing iPhone-sized ones
find ~ -iname "*.png" -type f -print0 | while read -d $'\0' -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

【讨论】:

  • 不太确定这是否可能,因为文件的信息存储在库中,并且对于磁盘上的文件不必相同(文件可能与 iCloud 同步或不同步)。
  • 好的,没问题。我不确定它是否有帮助 - 如果没有人想出更好的东西,它可能会让你开始:-)
猜你喜欢
  • 2015-05-07
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多