【问题标题】:Repeat loop with if conditions for first and last time第一次和最后一次使用 if 条件重复循环
【发布时间】:2023-01-14 09:00:13
【问题描述】:
我有一个我制作的脚本可以正常工作,但我必须对输出进行一些非常小的编辑。相反,我只想正确地做。
on run {input, parameters}
set the formatted to {}
set listContents to get the clipboard
set delimitedList to paragraphs of listContents
repeat with listitem in delimitedList
set myVar to "@\"" & listitem & "\"," & (ASCII character 10)
copy myVar to the end of formatted
end repeat
display dialog formatted as string
return formatted as string
end run
我想在第一个项目前添加一些不同的内容,并在最后一个项目上添加一些不同的内容。
我尝试了以下但脚本不正确。
repeat with n from 1 to count of delimitedList
-- not sure how to if/else n == 0 or delimitedList.count
end repeat
【问题讨论】:
标签:
loops
for-loop
applescript
【解决方案1】:
有更高效的方法,text item delimiters。它可以在列表项之间插入逗号和换行符
on run {input, parameters}
set the formatted to {}
set listContents to get the clipboard
set delimitedList to paragraphs of listContents
repeat with listitem in delimitedList
copy "@" & quote & listitem & quote to the end of formatted
end repeat
set {saveTID, text item delimiters} to {text item delimiters, {"," & linefeed}}
set formatted to formatted as text
set text item delimiters to saveTID
display dialog formatted
return formatted
end run
旁注:ASCII character 10 自 macOS 10.5 Leopard 以来已弃用,有 linefeed、tab (9)、return (13)、space (32) 和 quote (34)。