如何将 AppleScript 列表转换为字符串

How to turn an AppleScript list into a string

试图学习如何使用 AppleScript 记录和列表以发挥其最大潜力我一直在尝试创建 BBEdit 项目的报告,但我发现的文档非常有限。我问了一个问题 trying to figure out why my find pattern wasn't working but after finding out that the issue was from me lacking returning results:true I was able to get the result record and I verified it was a record after reading Class and 运行:

class of findFunction

因为它说这是一条记录,所以我查看了 here 和 运行 length of findFunctioncount of findFunction 他们都返回了 2。我很好奇这两个项目是什么在记录中所以我使用 return findFunction 并被告知有:

found: true
found matches: list of X items

想知道匹配项在列表中的位置和文件,我做了更多搜索并阅读了 Lists and records 和 运行:

set theMatches to get found matches of findFunction

它返回了列表项并使用 get count of theMatches 检查新变量我能够在记录中获取目标列表中的项目数量。当我查看列表中的内容时(从 How to get a value from a list with a string in AppleScript? and Searching for items in list 中学习)我可以得出结论,当在 BBEdit 中使用 find 时,列表中的每个项目都包含:

end_offset : 
match_string :
message :
result_file :
result_kind :
result_line :
start_offset :

试验一个项目我设置了一个变量:

set itemOne to get item 1 of theMatches

并检查它是否适用于:

display dialog (result_file of itemOne) as text

并显示一个包含完整文件路径的对话框。尝试使用我创建的 DRY:

set filesResult to get (result_file of (get item 1 of theMatches)) as text

想要将上述任何内容添加到文件中,例如:

set filesResult to get (result_file of (get item 1 of theMatches)) as text
set theMessage to get (message of (get item 1 of theMatches)) as text
set combined to filesResult & ":" & theMessage

我记得可以使用剪贴板并找到 Set clipboard to Applescript variable? 所以我添加了:

set filesResult to the clipboard
make new text document
paste

但我 运行 遇到的问题是如何获取列表中的每个项目 found_matches 并将其添加到剪贴板的每一行中的一个项目?我考虑过使用 repeat,但尝试时出现错误:

repeat with x from 1 to (length of matchesItems)
    set filesResult to get (result_file of (get item x of theMatches)) as text
    set theMessage to get (message of (get item x of theMatches)) as text
    set combined to filesResult & ":" & theMessage
end repeat

留言:

The variable matchesItems is not defined.

那么我怎样才能将列表中的每一项都放入剪贴板,每一项都在其自己的行中,这样我就可以将剪贴板中的所有项目粘贴到一个新文件中?

澄清措辞

theList = {A,B,C} -- this is a list with 3 variables
theRecord = {A:something, B:somethingElse, C:somethingElseTwo} -- this is a record.

列表可以通过其索引寻址。

theList's item 1 -- A

记录可以通过其键寻址

A of theRecord -- something

要将列表中的所有项目放入一个字符串中,请按其索引重复它(假设每个项目都是文本类型)

set finalString to ""
repeat with thisItem in TheList
    set finalString to finalString & thisItem & return -- the return creates a new line
end repeat

然后你有 finalString 可以随心所欲地处理。

要获取记录的每一项,您必须知道它的键(如果它不是 ASOC NSDictionary)

set finalString to ""
set finalString to finalString & A of theRecord & return;
-- repeat last line with every key