Applescript - 如何获取由换行符分隔的剪贴板的每一行并对每一行执行操作?

waf*_*ffl 3 macos clipboard applescript todo

我正在尝试修改一个带有文本文件的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
Run Code Online (Sandbox Code Playgroud)

相反,我希望能够简单地使用剪贴板作为数据源,这样我就不必每次都创建和保存文本文件,有没有办法加载剪贴板,并且对于每个换行符,执行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 
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

reg*_*633 6

Applescript 有一个名为“段落”的命令,它非常擅长确定行分隔符是什么。因此,请尝试一下。请注意,这种方法不需要“文本项分隔符”。

set listContents to get the clipboard
set delimitedList to paragraphs of listContents
Run Code Online (Sandbox Code Playgroud)

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

set LF to character id 10
Run Code Online (Sandbox Code Playgroud)