Applescript - 如何获取由换行符分隔的剪贴板的每一行并对每一行执行操作?
我正在尝试修改一个采用文本文件的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Applescript 有一个名为“paragraphs”的命令,它非常擅长找出行分隔符。因此,请尝试一下。请注意,使用这种方法您将不需要“文本项分隔符”之类的东西。
请注意,如果您想使用您的代码,这是获取换行符的正确方法......
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.
Note that if you want to use your code, this is the proper way to get a line feed character...