3

我正在尝试修改一个带有文本文件的applescript,并使用文本文件的每一行创建一个新的待办事项:

set myFile to (choose file with prompt "Select a file to read:")
open for access myFile

set fileContents to read myFile using delimiter {linefeed}
close access myFile

tell application "Things"

    repeat with currentLine in reverse of fileContents

        set newToDo to make new to do ¬
            with properties {name:currentLine} ¬
            at beginning of list "Next"
        -- perform some other operations using newToDo

    end repeat

end tell

相反,我希望能够简单地将剪贴板用作数据源,这样我就不必每次都创建和保存文本文件,有没有办法加载剪贴板,并且对于每个换行符,执行newToDo 功能?

到目前为止,这是我的尝试,但它似乎不起作用并将整个剪贴板放在一行中,我似乎找不到合适的分隔符。

try
    set oldDelims to AppleScript's text item delimiters -- save their current state
    set AppleScript's text item delimiters to {linefeed} -- declare new delimiters

    set listContents to get the clipboard
    set delimitedList to every text item of listContents

    tell application "Things"
        repeat with currentTodo in delimitedList
            set newToDo to make new to do ¬
                with properties {name:currentTodo} ¬
                at beginning of list "Next"
            -- perform some other operations using newToDo
        end repeat
    end tell

    set AppleScript's text item delimiters to oldDelims -- restore them
on error
    set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
end try 

编辑:在下面的答案的帮助下,代码非常简单!

set listContents to get the clipboard
set delimitedList to paragraphs of listContents

tell application "Things"
    repeat with currentTodo in delimitedList
        set newToDo to make new to do ¬
            with properties {name:currentTodo} ¬
            at beginning of list "Next"
        -- perform some other operations using newToDo
    end repeat
end tell
4

1 回答 1

6

Applescript 有一个名为“paragraphs”的命令,它非常擅长找出行分隔符是什么。因此,请尝试一下。请注意,您不需要使用这种方法的“文本项目分隔符”。

set listContents to get the clipboard
set delimitedList to paragraphs of listContents

请注意,如果您想使用您的代码,这是获取换行符的正确方法......

set LF to character id 10
于 2011-12-15T22:42:29.507 回答