4

我一直在通过谷歌四处寻找一些指导,让我通过 AppleScript 在 iPhoto 中做一些我需要做的事情,但到目前为止还没有找到很多。有各种旧版本的 iPhoto 脚本的各种旧讨论,但没有什么对我的需要特别有帮助。基本上,在伪代码中,我希望这样做:

for each photo in library
  if photo.Description contains "a known string"
    photo.Description = photo.Description.Replace("a known string", "")
  end if
end for

也就是说,我有一段错误的文字出现在我图书馆的每张(嗯,几乎每张)照片中。我猜我在过去的某个时候搞砸了一批更改,直到现在才注意到。无论是那个还是从 iPhoto '08 到 '11 的升级都以某种方式做到了。无论哪种方式,最终结果都是一样的。

我对 AppleScript 并不精通,并且很难找到正确的语法/词汇来使用它。基本上,我在这tell application "iPhoto"部分,但不知道该说什么。如果库中照片的组织层次结构很重要:

  1. 每张照片都按时间顺序组织成事件。(活动是我的主要组织形式。)
  2. 有很多专辑,但并非所有专辑都在专辑中。
  3. 有一个智能相册,其中包含每张错误的照片。当然,这是基于照片描述中已知字符串的存在。所以我想如果最终代码循环遍历这个智能相册中的照片,可能需要记住这一点,因为智能相册可能会改变被迭代的数组,不是吗?

有人有任何参考资料或示例代码来帮助我吗?相反,有没有人知道一种更好的方法来进行这种一次性的大规模修复?

编辑:我使用以下代码进行了测试:

tell application "iPhoto"
    activate
    set thePhotos to get every photo
    repeat with aPhoto in thePhotos
        if aPhoto's comment contains "[known string]" then
            log aPhoto's comment
            tell aPhoto to set it's comment to text 1 thru (offset of "[known string]" in aPhoto's comment) of aPhoto's comment
            log aPhoto's comment
            exit repeat
        end if
    end repeat
end tell

这导致了以下输出:

tell application "iPhoto"
    activate
    get every photo
    get comment of photo id 4.294977224E+9
    (*comment of photo id 4.294977224E+9*)
    offset of "[known string]" in comment of photo id 4.294977224E+9
    «event ascrgdut»
    offset of "[known string]" in comment of photo id 4.294977224E+9
end tell
tell current application
    offset of "[known string]" in «class pcom» of «class ipmr» id 4.294977224E+9
Result:
error "iPhoto got an error: Can’t make comment of photo id 4.294977224E+9 into type string." number -1700 from comment of photo id 4.294977224E+9 to string

编辑:今天早上我有一些时间来修补它,看起来只需要一些类型转换。此代码现在成功地更改了它找到的第一张匹配照片:

tell application "iPhoto"
    activate
    set thePhotos to get every photo
    repeat with aPhoto in thePhotos
        if aPhoto's comment contains "[known string]" then
            log aPhoto's comment as text
            set theComment to aPhoto's comment as text
            set theComment to text 1 thru (offset of "[known string]" in theComment) of theComment
            tell aPhoto to set it's comment to theComment
            log aPhoto's comment as text
            exit repeat
        end if
    end repeat
end tell

现在备份我的库并删除exit repeat. 并且可能会在运行时去做其他事情:)

4

2 回答 2

4

这是一个“蛮力”版本。这将遍历每张照片。如果需要,您可以通过限制某些专辑来使其更加优雅。

tell application "iPhoto"
    set thePhotos to get every photo
    repeat with aPhoto in thePhotos
        if aPhoto's comment contains "theString" then
            tell aPhoto to set it's comment to "newString"
        end if
    end repeat
end tell
于 2011-01-17T14:25:30.823 回答
1

那这个呢。您需要为想要影响的项目制作专辑或智能专辑,但这无论如何破坏性较小。

tell application "iPhoto"
    activate
    set thePhotos to get every photo in current album whose comment contains "TEST123"
    repeat with aPhoto in thePhotos
        tell aPhoto to set it's comment to "123TEST"
    end repeat
end tell
于 2012-07-03T02:28:23.733 回答