Dᴀʀ*_*ᴅᴇʀ 3 applescript list record repeat bbedit
为了学习如何最大程度地使用 AppleScript 记录和列表,我一直在尝试创建 BBEdit 项目的报告,但我发现文档非常有限。昨天我问了一个问题,试图找出为什么我的查找模式不起作用,但在发现问题是由于我缺乏之后,returning results:true
我能够获得结果记录,并且在阅读Class并运行后验证它是一条记录:
class of findFunction
Run Code Online (Sandbox Code Playgroud)
因为它说这是一条记录,我在这里length of findFunction
查看并运行count of findFunction
,它们都返回了 2。我很好奇知道记录中的这两项内容是什么,所以我使用了return findFunction
并被告知有:
found: true
found matches: list of X items
Run Code Online (Sandbox Code Playgroud)
想知道匹配项位于列表中的位置和文件,我做了更多搜索并读取列表和记录并运行:
set theMatches to get found matches of findFunction
Run Code Online (Sandbox Code Playgroud)
它返回列表项并检查新变量,get count of theMatches
我可以获取记录内目标列表中的项目数量。当我查看列表中的内容时(学习自:How to get a value from a list with a string in AppleScript?和Searching for items in list),我可以得出结论,在使用find
BBEdit 时,列表中的每个项目都包含:
end_offset :
match_string :
message :
result_file :
result_kind :
result_line :
start_offset :
Run Code Online (Sandbox Code Playgroud)
试验一个项目,我设置了一个变量:
set itemOne to get item 1 of theMatches
Run Code Online (Sandbox Code Playgroud)
并检查它是否适用于:
display dialog (result_file of itemOne) as text
Run Code Online (Sandbox Code Playgroud)
并显示一个包含完整文件路径的对话框。尝试利用我创建的 DRY:
set filesResult to get (result_file of (get item 1 of theMatches)) as text
Run Code Online (Sandbox Code Playgroud)
想要将上述任何内容添加到文件中,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
我记得能够使用剪贴板并发现将剪贴板设置为 Applescript 变量?所以我补充道:
set filesResult to the clipboard
make new text document
paste
Run Code Online (Sandbox Code Playgroud)
但我遇到的问题是如何获取列表中的每个项目found_matches
并将其添加到剪贴板每行上的项目?我想过使用 arepeat
但当我尝试时出现错误:
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
Run Code Online (Sandbox Code Playgroud)
并附有一条消息:
变量 matchesItems 未定义。
那么,如何将列表中的每个项目放入剪贴板,并且每个项目都在其自己的行上,以便我可以将剪贴板中的所有项目粘贴到新文件中?
澄清措辞
theList = {A,B,C} -- this is a list with 3 variables
theRecord = {A:something, B:somethingElse, C:somethingElseTwo} -- this is a record.
Run Code Online (Sandbox Code Playgroud)
列表可以通过其索引来寻址。
theList's item 1 -- A
Run Code Online (Sandbox Code Playgroud)
记录可以通过其键来寻址
A of theRecord -- something
Run Code Online (Sandbox Code Playgroud)
要将列表中的所有项目放入字符串中,请按其索引重复它(表示每个项目都是文本类型)
set finalString to ""
repeat with thisItem in TheList
set finalString to finalString & thisItem & return -- the return creates a new line
end repeat
Run Code Online (Sandbox Code Playgroud)
然后你就可以用finalString做任何你想做的事了。
要获取记录的每个项目,您必须知道它的键(如果它不是 ASOC NSDictionary)
set finalString to ""
set finalString to finalString & A of theRecord & return;
-- repeat last line with every key
Run Code Online (Sandbox Code Playgroud)