Text document to a list in Applescript

Thanks for the help. I am on the journey of applescripting and have now encountered the topic of lists. I know that a list can contain different data types and sometimes (i’m not sure about this-types can be coerced to submission). My problem is this:

I have a text document that contains an ordered set of text (let say 5 lines or paragrahs)

line 1
line 2
line 3
line 4
line 5

I can capture these with this script.

set theFile to (choose file with prompt “Select a file to read:”)
open for access theFile
set x to (read theFile)
close access theFile
display dialog x

I need to set each paragraph as an item in a list, then I can go from there. Any takers??

thank you!:smiley:

Something like this?

set aFile to (choose file with prompt "Select a file to read:" without invisibles)
open for access aFile
set txt to (read aFile)
close access aFile
set para to paragraphs of txt
repeat with aP in para
	if (count aP) ≠ 0 then display dialog aP -- ignores blank lines
end repeat

Adam,
you forgot to tell him how to set the seperate paragrpahs as seperate items in a list…i’m saying this to you because i don’t know how to do that:P
your script by iteself when ran and the file is choosen comes up with an error saying: “End of file error”

Hi hendo13;

para (in my script) is an AppleScript list variable containing each of the separate paragraphs in the text.

The file chosen must be in plain old text format or you’ll see all the formatting stuff (for example of an rtf file)

For the file that gave you an eof error, what was its creator?

thank you…this works great! However, for my own knowledge why is it that if I place do this I get this?

set aFile to (choose file with prompt “Select a file to read:” without invisibles)
open for access aFile
set txt to (read aFile)
close access aFile

set para to paragraphs of txt

get item 3 of txt

(repeat with aP in para
if (count aP) ≠0 then display dialog aP
end repeat
)

result -->“n”

I thought an item from a list would result something like this

set x to {1,2,3}
get item 2 of x

result–> 2

why is it that “get item” would result in capturing a single character?

thanks!

You have:

set para to paragraphs of txt
get item 3 of txt

where txt is the entire document. The default text item delimiters in AppleScript are “” (nothing) so a text item is one character.

If you had said:

set p3 to item 3 of para

p3 would return the third paragraph.

If you want the third paragraph, you can ask for it directly:

set p3 to paragraph 3 of txt

Adam,
I used a script that i made myself to create the text file. It’s just a plain old .txt file!

Hendo;

Assuming you mean: set f to open for access “myTxt” with write permission
write “some text” to f

Did you close f?

I just tried it with this:

set f to open for access (path to desktop as text) & "myTxt" with write permission
write "Now is the time for all good men" to f
close access f

then ran my earlier script and it worked for me.

well my script to make text files is a little more advanced than that…this is what it looks like:

set user_text to text returned of (display dialog "Type the text you want in the document" default answer return & return & return)
set file_name to (choose file name with prompt "Please type a file name")
if file_name as string does not end with ".txt" then set file_name to ((file_name as string) & ".txt")
my make_report(file_name, user_text)

set dMes to return & return & "Would you like to open the text file now?"
set opts to (display dialog "Done!" & dMes buttons {"Yes", "No Thanks"} default button 1 with icon 1 giving up after 30)
if button returned of opts is "Yes" or gave up of opts is true then
	tell application "TextEdit"
		activate
		try
			open (file_name as alias)
		on error errMs
			display dialog errMs buttons {"Cancel"}
		end try
	end tell
end if -- button is "Done"

to make_report(file_name, user_text)
	try
		do shell script "rm " & quoted form of POSIX path of file_name
	end try
	
	try
		set fileRefr to (a reference to (open for access file_name with write permission))
		write user_text to fileRefr
		close access fileRefr
	on error errx number errNum from badObj
		try
			close access fileRefr
		end try
		log errNum
		if (errNum is equal to -48) then
			do shell script "rm " & quoted form of POSIX path of file_name
			my make_report()
		else
			display dialog "There has been an error creating the file:" & return & return & (badObj as string) & errx & return & "error number: " & errNum buttons {"Cancel"}
		end if
	end try
end make_report
on error_and_cancel(ms)
	if gave up of (display dialog ms with icon 2 buttons {"Cancel"} default button 1 giving up after 15) then error number -128
end error_and_cancel

run that to see what i mean

I never run a script that contains the phrase : do shell script "rm " &… without pretty thorough study.

Looking at yours, however, I don’t see why it won’t open properly – I ran it and it opens properly for me.

Thanks! It’s good now.:slight_smile: