Read Record from File

Although I can read a phone number from a record, I have been unable to read a phone number from a record, written to a file. In regard to overall context of this script, my goal was to export key value records from Filemaker to be read into an applescript.

Regarding the applescript, itself, reading a phone number from a record succeeds.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set RecordExample to {Phone:"(123) 456-7890"}
--> {Phone:"(123) 456-7890"}

set phonefromRecord1 to RecordExample's Phone
# returns correct value from record
-->  "(123) 456-7890" 

Reading a phone number from a record in a file fails.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set p2dt to (path to desktop folder)'s POSIX path
set readfile to read (p2dt & "DataRecord.txt")
# fails to return a record
--> "ˇ˛{Phone:\"(415) 408-3365\"}" 

tell readfile
	set phonefromRecord2 to readfile's Phone
        # fails to return a record item
	--> Applescript Execution Error: Can’t get Phone of "ˇ˛{Phone:\"(123) 456-7890\"}"
	
	set RecordFromText to text 3 through -1
	RecordFromText's Phone
        # fails to return a record item
	-->  Applescript Execution Error: Can’t get Phone of "{Phone:\"(123) 456-7890\"}"
	
end tell

How do I manipulate a file, so that I can read its record?

Obviously you saved a string rather than a record. Save the record

set RecordExample to {Phone:"(123) 456-7890"}
set p2dt to (path to desktop folder)'s POSIX path
set fileDescriptor to open for access (p2dt & "DataRecord.txt") with write permission
write RecordExample to fileDescriptor
close access fileDescriptor

Then you can read

set p2dt to (path to desktop folder)'s POSIX path
set readfile to read (p2dt & "DataRecord.txt") as record
set phonefromRecord1 to readfile's Phone

Just wanted to mention that Applescript can easily and directly read and write data from and to Filemaker without needing an external text file.

I ran Stefan’s first script and the DataRecord text file contained the following:

I thought the script must be broken but Stefan’s second script returned the desired phone number. I changed the record to the list {“a”, “b”, “c”} and the text file contained:

It would appear that utxt is effectively a delimiter. Interesting.

It’s a bit more complex than that. “utxt” is the four-letter code for Unicode text, or these days simply text.

Thank you for clarifying the process.

Thanks Shane. I figured there had to be more to it than what I saw.