将列表转换为applescript数组

时间:2017-06-11 17:58:13

标签: arrays syntax applescript quotations

我有一个applescript函数来处理数组的每个项目。我正在寻找一种方法将类似下面的列表放入我的applescript文件中:

arrayitem1
arrayitem2
arrayitem3

并使用applescript自动将其格式化为正确的applescript数组语法,所以我最终得到了这个:

set array1 to {"arrayitem1", "arrayitem2", "arrayitem3"}

可以想象,对于较长的数组,手动添加所有逗号和引号会很麻烦。

谢谢!

3 个答案:

答案 0 :(得分:1)

set stringofitems to "
item1
item2
item3
"

set text item delimiters to "
"

set listofitems to text items of stringofitems

repeat with theitem in listofitems

    # stuff to do with each item

end repeat

答案 1 :(得分:0)

如果我了解您想要的内容,我建议您选择并复制Google文档中的字符串,然后运行从剪贴板中提取字符串的脚本,进行更改你想要的,然后把结果放到剪贴板上准备粘贴到你想要的地方。

以下是使用您所说的内容的示例。

-- get the string into the script through the clipboard
set theString to the clipboard

-- convert the string into a list, using paragraphs
set theParagraphs to paragraphs of theString

-- convert the list to a string, with ", " between items
set AppleScript's text item delimiters to "\", \""
set theResult to ("\"" & theParagraphs as string) & "\""
set AppleScript's text item delimiters to ""

set the clipboard to theResult
-- "arrayitem1", "arrayitem2", "arrayitem3"

答案 2 :(得分:0)

如果原始文件是以行分隔的文本,则可以使用“paragraph”来拆分它。 endList必须定义为列表'{}'现在通过重复,剪贴板中内容的每一行都将作为新条目输入到列表endList中。

set {var, endList} to {the clipboard, {}}
repeat with x in paragraphs of var
    set endList to endList & x
end repeat
相关问题