【问题标题】:How to turn an AppleScript list into a string如何将 AppleScript 列表转换为字符串
【发布时间】:2017-04-24 01:00:16
【问题描述】:

尝试学习如何最大限度地利用 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没有定义。

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

【问题讨论】:

    标签: list applescript record repeat bbedit


    【解决方案1】:

    澄清措辞

    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
    

    【讨论】:

    • 将 myList 的结尾设置为“c”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 2020-06-07
    • 1970-01-01
    相关资源
    最近更新 更多