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

标签 list applescript record repeat bbedit

尝试学习如何使用 AppleScript 记录和列表以发挥最大潜力,我一直在尝试创建 BBEdit 项目的报告,但发现文档非常有限。我问了一个问题yesterday试图找出为什么我的查找模式不起作用,但在发现问题是由于我缺少返回结果: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

它返回了列表项,并使用获取匹配数检查新变量,我可以获取记录内目标列表中的项目数量。当我查看列表中的内容时(从 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

消息如下:

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

关于list - 如何将 AppleScript 列表转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43564172/

相关文章:

ios - 将 ASMX Web 服务以 XML 形式返回的数据加载到 iPhone/iPad 中会导致应用程序崩溃

java - 如何将列表中的get(0)转换为常量并获取值

list - 使用 Prolog 折叠添加列表中的所有项目

variables - 比较 Applescript 中的变量错误?

list - 在 Applescript 中实现键盘音量控制按钮 - 在循环内设置音量不起作用

objective-c - 在用户键入时更新标签 使用 Cocoa-Applescript

objective-c - 对象-c/iOS :About use NSUserDefaults set serial numbers/password

Java:将一对元素插入链表

python:逐点列表总和

record - 通过引用访问记录字段