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

时间:2017-04-22 20:20:08

标签: list applescript record repeat bbedit

尝试学习如何将AppleScript记录和列表用于最大潜力我一直在尝试创建BBEdit项目的报告,但我发现文档非常有限。我问了一个问题yesterday,试图找出为什么我的查找模式不起作用,但在发现问题来自我缺乏returning results:true后,我得到了结果记录并且我验证了阅读Class并运行后,这是一条记录:

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?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

显示以下消息:

  

未定义变量matchesItems。

那么如何将列表中的每个项目都放到剪贴板中,并且每个项目都有自己的行,这样我就可以将剪贴板中的所有项目粘贴到新文件中?

1 个答案:

答案 0 :(得分:2)

澄清措辞

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