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

发布于 2024-12-21 11:32:45 字数 1905 浏览 1 评论 0原文

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

I am trying to modify an applescript that takes a text file, and creates a new todo with each line of the text file:

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

I would like instead, to be able to simply use the clipboard as the data source instead so I don't have to create and save a text file each time, is there any way to load the clipboard, and for each newline, perform the newToDo function?

This is my attempt so far but it doesn't seem to work and puts the entire clipboard into one line, I can't seem to find the proper delimiter.

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 

EDIT:
With the help of the answer below, the code is incredibly simple!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2024-12-28 11:32:45

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

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

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

set LF to character id 10

Applescript has a command called "paragraphs" which is very good at figuring out what the line delimiter is. As such give this a try. Note that you won't need the "text item delimiters" stuff with this approach.

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

Note that if you want to use your code, this is the proper way to get a line feed character...

set LF to character id 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文