Text AppleScript从“中读取字符串”;。txt";文件到变量

Text AppleScript从“中读取字符串”;。txt";文件到变量,text,applescript,Text,Applescript,我正在尝试构建一个AppleScript,它读取“.txt”文件的每一行(带有换行符),并将每一行的内容存储到AppleScript变量中 set newFile to ("Macintosh HD:Users:Username:Desktop:Test.txt") set theFileContents to paragraphs of (read file newFile) set recipientEmail to paragraph 1 of theFileContents as tex

我正在尝试构建一个AppleScript,它读取“.txt”文件的每一行(带有换行符),并将每一行的内容存储到AppleScript变量中

set newFile to ("Macintosh HD:Users:Username:Desktop:Test.txt")
set theFileContents to paragraphs of (read file newFile)
set recipientEmail to paragraph 1 of theFileContents as text
set senderEmail to paragraph 2 of theFileContents as text
set theSubject to paragraph 3 of theFileContents as text
set theBody to (paragraphs 4 thru -1 of theFileContents) as text
我的意思是:

假设有一个包含以下内容的“Test.txt”文件:

Apples  
Oranges  
Pears  
如您所见,“Test.txt”文件的内容在每一行上都有一个字符串,一个引入新字符串的换行符,等等

我很想知道如何制作AppleScript,以便将每行的字符串复制到各个AppleScript变量中

(这样一来,第一行的“苹果”将储存在variableA中,第二行的“橙子”将储存在variableB中,“梨”…variableC中,等等。)

请让我知道,从你的经验,如何最好地完成这一点。我知道这有点复杂,我现在的处境是:

(* 
This portion of the AppleScript accesses the contents of the ".txt" file named "Test," though takes all of the content and places it into a single variable. 
*)

set newFile to ("Macintosh HD:Users:Username:Desktop:Test.txt")
set theFileContents to (read file newFile)

{ AppleScript code to read each line to individual variables }

我知道一定有其他人在试图实现这一点。

此示例适用于这样一种情况,即您知道要分配给每个已知变量集的预期段落

set newFile to ("Macintosh HD:Users:Username:Desktop:Test.txt")
set theFileContents to paragraphs of (read file newFile)
set recipientEmail to paragraph 1 of theFileContents as text
set senderEmail to paragraph 2 of theFileContents as text
set theSubject to paragraph 3 of theFileContents as text
set theBody to (paragraphs 4 thru -1 of theFileContents) as text
另一种选择是动态搜索段落中的字符串,如果匹配,则将其分配给该变量。比如:

set newFile to ("Macintosh HD:Users:jweaks:Desktop:horses.txt")
set theFileContents to paragraphs of (read file newFile)

set recipientEmail to ""
set senderEmail to ""
set theSubject to ""
set theBody to ""

repeat with p in theFileContents
    if p contains "To:" then
        set recipientEmail to p
    else if p contains "From:" then
        set senderEmail to p
    else if p contains "Subject:" then
        set theSubject to p
    else
        set theBody to theBody & p & return
    end if
end repeat

非常感谢你为回答这个问题所做的努力,jweaks。由于我仍在掌握AppleScript最佳实践,我在考虑您的建议,将“.txt”文件的内容放入一个列表,将项目分配给AppleScript变量(如果需要),并开始头脑风暴如何完成它。我同意这似乎是最简单、最有效的方法:

将“.txt”文件读入项目列表:苹果、桔子和梨 使用分隔符换行符将段落列表设置为读取文件“Macintosh HD:Users:tombettinger:Desktop:Test.txt”

苹果 将变量A设置为段落列表的第1项

橘子 将variableB设置为段落列表的第2项

梨 将变量C设置为段落列表的第3项

显示每个变量的内容(可选注释) 显示对话框variableA&&&variableB&&variableC

AppleScript结尾
只要“.txt”文件的内容堆叠在一个表中,这种方法将支持我搜索的信息的可访问性。再次感谢

嘿,上面苹果、橘子和梨的例子应该看起来像一个一栏三行的表格,比如:苹果(新行)、橘子(新行)、梨(新行)。你确定要每一行都有自己的变量吗?这是非常有问题的,除非您已经知道文件将有多少行/变量。动态创建声明的变量需要创建脚本对象。你想用变量/段落完成什么?更常见的方法是将段落读入列表,然后处理每个列表项/段落。嘿,谢谢你的帮助,jweaks!老实说,我只看到将每个变量作为变量使信息更容易访问的好处。例如,假设AppleScript使用Java程序编写的文本文件来自动发送用户在Java程序中输入的电子邮件。我见过这样的实现,主题、正文和收件人电子邮件都是AppleScript中的变量(电子邮件地址(一个变量)、邮件正文(另一个变量),等等)。我真的不喜欢这两种方法,如果你知道更好的方法,请分享。谢谢这似乎不会将文件内容转换为字符串。有什么解决办法吗?我不知道你的意思。变量theFileContents是一个字符串列表,每个项目一个段落。