【问题标题】:Applescript - How to get each line of clipboard delimited by newline and perform an action with each line?Applescript - 如何获取由换行符分隔的每一行剪贴板并对每一行执行操作?
【发布时间】:2011-12-15 21:42:25
【问题描述】:

我正在尝试修改一个带有文本文件的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

【问题讨论】:

    标签: macos applescript clipboard todo


    【解决方案1】:

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

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

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

    set LF to character id 10
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2021-06-12
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      相关资源
      最近更新 更多