Tom Bettinger
Tom Bettinger

Reputation: 31

AppleScript to read strings from ".txt" files to variables

I am trying to build an AppleScript that reads each line of a “.txt” file (with a linefeed) and stores the contents of each line into AppleScript variables.

Here is what I mean:

Let’s say there was a “Test.txt” file with the contents:

Apples  
Oranges  
Pears  

As you can see, the “Test.txt” file’s contents have a String on each line, a linefeed introducing the new String, and so on.

I would really like to know how an AppleScript could be made so that each line’s Strings are copied into individual AppleScript variables.

(This way, “Apples” in the first line, would be stored in variableA, “Oranges” in the next would be stored in variableB, “Pears” … variableC, etc.)

Please let me know, from your experience, how best to accomplish this. I know it’s slightly more involved, here is where I am:

(* 
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 }

I know there must be others trying to accomplish this.

Upvotes: 3

Views: 8758

Answers (2)

Tom Bettinger
Tom Bettinger

Reputation: 31

Thank you so much for all of your effort to answer this question, jweaks. As I am still catching on to AppleScript best practices, I was thinking more about your recommendation of bringing the contents of the “.txt” file into a list, assigning the items to AppleScript variables (if needed), and began brainstorming how to accomplish it. I agree that it seems like the simplest and most efficient approach:

Reads the “.txt” file’s into a list of items: Apples, Oranges, and Pears

set paragraph_list to read file "Macintosh HD:Users:tombettinger:Desktop:Test.txt" using delimiter linefeed

Apples

set variableA to item 1 of paragraph_list

Oranges

set variableB to item 2 of paragraph_list

Pears

set variableC to item 3 of paragraph_list

Displays the contents of each variable (optional comment)

display dialog variableA & " " & variableB & " " & variableC

End of AppleScript

As long as the contents of the ".txt" file are stacked in a table, this approach will support the accessibility of information I was searching for. Thanks again!

Upvotes: 0

jweaks
jweaks

Reputation: 3792

This example is for a situation where you know the anticipated paragraphs you'll be looking for to assign to each of a known set of 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

Another option would be to dynamically search for a string in the paragraph, and if it matches, then assign it to that variable. Something like:

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

Upvotes: 4

Related Questions