用于iPhoto Automation的AppleScript语法

时间:2011-01-16 22:10:48

标签: applescript iphoto

我一直在寻找谷歌的一些指示,让我在iPhoto中通过AppleScript进行我需要做的事情,但到目前为止还没有发现很多。对于各种旧版本的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. 有一张智能相册,其中包含每张错误的照片。当然,这是基于照片描述中已知字符串的存在。所以我想如果最终的代码在智能相册中循环播放照片,可能需要记住,因为智能相册可能正在改变迭代的数组,不是吗?
  4. 是否有人提供任何参考或示例代码以帮助我前进?相反,是否有人知道更好的方法来进行一次性质量修复?

    修改:我使用以下代码运行测试:

    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。并且可能会在运行时暂时执行其他操作:)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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