0

我想在applescript中制作一个小应用程序,轮询剪贴板的更改,并在检测到任何更改后,将剪贴板的内容转储到文本文件中。这是我想出的代码,它创建了文件,但没有写入任何内容。我究竟做错了什么?

property oldvalue : missing value

on idle
    local newValue
    set newValue to the clipboard
    if oldvalue is not equal to newValue then
        try
            tell application "Finder"
                try
                    set the_file to "/Users/xxx/Documents/dump2.txt" as POSIX file as alias
                on error
                    set the_file to (make new document file at ("/Users/xxx/Documents/" as POSIX file as alias) with properties {name:"dump2", text:""})
                end try
            end tell

            try
                open for access the_file with write permission
                write newValue to file the_file starting at eof
                close access the_file
            on error
                try
                    close access the_file
                end try
            end try

        end try

        set oldvalue to newValue

    end if

    return 1 
end idle
4

1 回答 1

1

open for access还接受 POSIX 路径作为文本作为参数,并且指定为参数的路径不必存在。您的脚本不起作用,因为make new document file返回一个 Finder 文件对象,并且该as POSIX file as alias部分总是导致错误。

property old : ""
on idle
    set new to the clipboard
    if new is not old then
        set old to new
        if new does not end with linefeed then set new to new & linefeed
        set b to open for access "/tmp/clipboard.txt" with write permission
        write new to b as «class utf8» starting at eof
        close access b
    end if
    return 1
end idle

as «class utf8»需要,因为write默认情况下仍使用主要编码(如 MacRoman 或 MacJapanese)。as Unicode text将是 UTF-16。

于 2013-08-04T04:22:02.443 回答