神秘的AppleScript错误:“;4.294967323E和#x2B;9Mahabalipuram“;不’;我不理解“;写入”;消息

神秘的AppleScript错误:“;4.294967323E和#x2B;9Mahabalipuram“;不’;我不理解“;写入”;消息,applescript,Applescript,我的一个通过AppleScript与iPhoto通信的脚本的用户收到了这个错误,我无法重现:918:955:执行错误:iPhoto收到了一个错误:“4.294967323E+9Mahabalipuram”不理解“write”消息。(-1708) 产生错误的AppleScript是: set nul to character id 0 set text item delimiters to nul set albumsFile to "/Users/[user]/Downloads/blah.b

我的一个通过AppleScript与iPhoto通信的脚本的用户收到了这个错误,我无法重现:
918:955:执行错误:iPhoto收到了一个错误:“4.294967323E+9Mahabalipuram”不理解“write”消息。(-1708)

产生错误的AppleScript是:

set nul to character id 0
set text item delimiters to nul

set albumsFile to "/Users/[user]/Downloads/blah.blah"
set fp to open for access (POSIX file albumsFile) with write permission

tell application "iPhoto"
    repeat with anAlbum in albums
        if anAlbum's type is regular album then
            set albumName to anAlbum's name
            if albumName is not "Last Import" then
                set albumPhotoIds to (id of every photo of anAlbum) as Unicode text
                if length of albumPhotoIds is greater than 0 then
                    set currentAlbum to anAlbum
                    repeat while currentAlbum's parent exists
                        set currentAlbum to currentAlbum's parent
                        set albumName to currentAlbum's name & " > " & albumName
                    end repeat
                    set albumId to anAlbum's id

                    set albumData to {"", albumId, albumName, ""} as Unicode text
                    write albumData to fp as Unicode text
                    write albumPhotoIds to fp as Unicode text
                    write nul to fp as Unicode text
                end if
            end if
        end if
    end repeat
end tell

close access fp

有人知道这里出了什么问题吗?这个Github问题还有一点背景知识:

Write来自StandardAdditions,而不是iPhoto,因此您不能告诉iPhoto编写。这将是一种大致如下的情况:

property nul : character id 0
set text item delimiters to nul

set albumsFile to (path to downloads folder as text) & "blah.txt"

tell application "iPhoto"
    repeat with anAlbum in albums
        if anAlbum's type is regular album then
            set albumName to anAlbum's name
            if albumName is not "Last Import" then
                set albumPhotoIds to (id of every photo of anAlbum)

                if length of albumPhotoIds is greater than 0 then
                    set currentAlbum to anAlbum
                    repeat while currentAlbum's parent exists
                        set currentAlbum to currentAlbum's parent
                        set albumName to currentAlbum's name & " > " & albumName
                    end repeat
                    set albumId to anAlbum's id
                    set albumData to {"", albumId, albumName, ""} as Unicode text

                    my writeIt(albumsFile, albumData, albumPhotoIds)
                end if
            end if
        end if
    end repeat
end tell

on writeIt(albums_File, album_Data, album_PhotoIds)
    try
        set fp to open for access albums_File with write permission
        write album_Data to fp
        write album_PhotoIds to fp
        write nul to fp
        close access fp
    on error
        try
            close access fp
        end try
    end try
end writeIt
这可能有效(未经测试);它通常会出现这种错误。但是,正如adayzone所指出的,最好是重新构建脚本

tell me to write albumData to fp as Unicode text
tell me to write albumPhotoIds to fp as Unicode text
tell me to write nul to fp as Unicode text

这也很好地说明了tell是如何工作的(有时会“碍事”)

谢谢,我会看看它是否有用。奇怪的是,我以前用过的这种方式对我和许多其他用户都有效。谢谢。这确实有效(或者,等效地,将三次写入放在“告诉我”块中),并且比@adayzdone更容易修复(另外,它不会在每次循环迭代中打开和关闭文件)。我越来越不是AppleScript的粉丝了。