将Applescript变量设置为从文本文件列出

时间:2017-09-28 01:07:13

标签: list variables random applescript

我是Applescript的新手和一般的编码世界。我正在尝试使用小脚本让我的脚湿透并享受一些乐趣。

我想制作一个随机提示生成器。我知道如何将变量设置为列表,但我想到的是比一些选择大得多。我想知道如何从文本文件中获取信息。在所述文件中,我有200多个提示可供选择。我想创建一个脚本,从中选择一个随机的脚本,并在对话框中显示它们。我无法将初始变量设置为文件内容作为列表。跟进(随机选择)我认为我对此非常了解。我希望这很清楚,谢谢你的期待。

编辑: 到目前为止,这是我的代码。我一直遇到错误:"无法制作" /Users/Home/Desktop/text.rtf"到类型文件。"不确定那是什么意思。

set draw_promptList to {}
set draw_prompt to read "/Users/Home/Desktop/text.rtf"
repeat with i from 1 to count of paragraphs in draw_prompt
set end of draw_prompt to paragraph i of draw_prompt
end repeat
set the list_count to the count of draw_prompt
set pick to random number from 1 to list_count
display dialog "Try drawing " & some item of draw_prompt & return

编辑9/28:我根据修订版修复了它并且它有效。它完全符合我的要求:从文本文件中的长字列表中选择一个随机字。

set draw_promptList to {}
set draw_prompt to read "/Users/Home/Desktop/test.txt"
set the list_count to the count of paragraphs in draw_prompt
set pick to random number from 1 to list_count
repeat with i from 1 to count of paragraphs in draw_prompt
    set end of draw_promptList to paragraph i of draw_prompt
end repeat
display dialog "Try drawing " & some item of draw_promptList

1 个答案:

答案 0 :(得分:0)

如果我正确理解您要求的内容,读取文件并将该文件的每一行转换为列表中的项目,那么这是一种可行的方式。< / p>

set theFileOfPromptsList to {}
set theFileOfPrompts to read "/path/to/FileOfPrompts"

repeat with i from 1 to count of paragraphs in theFileOfPrompts
    set end of theFileOfPromptsList to paragraph i of theFileOfPrompts
end repeat

如果您想确保不添加空行,(如果有),请使用以下内容:

set theFileOfPromptsList to {}
set theFileOfPrompts to read "/path/to/FileOfPrompts"

repeat with i from 1 to count of paragraph in theFileOfPrompts
    if paragraph i of theFileOfPrompts is not "" then
        set end of theFileOfPromptsList to paragraph i of theFileOfPrompts
    end if
end repeat