reading and displaying text

Hello,
I am having trouble getting a piece of code to work. Can anyone help? Thanks. I have created a text file using TextEdit saved as plain text. Here are the contents of the file:

“:cg4:net:tmp:Taperoom:file1.tga”,
“:cg4:net:tmp:Taperoom:file2.tga”,
“:cg4:net:tmp:Taperoom:file3.tga”

What I am trying to do is read the first line and display it. That’s it. Here is my code that does not seem to work. I have searched far and wide on the web and from the examples I have found this should work. Any ideas out there as to what I’ve done wrong?

code:


tell application "Script Editor"
	set the theFile to ":home:t:taperoom:desktop:ListOfSequences.txt"
	try
		open for access theFile
		set fileContents to (read theFile using delimiter {","})
		close access theFile
		set txtvar1 to item 1 of fileContents
		display alert txtvar1
	end try
	
	
end tell

The Event Log:

tell current application
open for access “:home:t:taperoom:desktop:ListOfSequences.txt”
565
read “:home:t:taperoom:desktop:ListOfSequences.txt” using delimiter {“,”}
end tell

any reason you have a tell Script Editor in there? Also - are you sure that path to your file is correct?

Try this:

tell application "Finder"
	set the theFile to choose file
	try
		open for access theFile
		set fileContents to (read theFile using delimiter {","})
		close access theFile
		set txtvar1 to item 1 of fileContents
		display alert txtvar1
	end try
end tell

Thank you for the response and suggestions.

When I changed to code to what you wrote it does work
:


tell application "Finder"
	set the theFile to choose file --"Macintosh HD:home:t:taperoom:desktop:ListOfSequences.txt"
	try
		open for access theFile
		set fileContents to (read theFile using delimiter {","})
		close access theFile
		set txtvar1 to item 1 of fileContents
		
	end try
	
	display alert txtvar1
end tell

but in the Event Log it displays the file path and I used that path to try making my code work with:


tell application "Finder"
	set the theFile to "Macintosh HD:home:t:taperoom:desktop:ListOfSequences.txt"
	try
		open for access theFile
		set fileContents to (read theFile using delimiter {","})
		close access theFile
		set txtvar1 to item 1 of fileContents
		
	end try
	
	display alert txtvar1
end tell


But my display alert tells me that “The variable txtvar1 is not defined.”

It seems to me that the problem is with my file path. I will try playing around with that and see if I can get it to work. Thanks for getting me a step closer.

The problem, I think, is that Mozart has created and saved the file in TextEdit and it is therefor an .rtf file (even if you change the extension to txt). Try this: create a simple file in TE: 1, 2, 3, 4, 5 and save it on your desktop as “Test”. TextEdit will add the extension .rtf to the file.

Then run:

set FR to open for access (path to desktop as text) & "Test"
read FR

and you’ll see that your simple test text has lots of other stuff in it than your one line.
You’ll have to save your files in an application that can produce a pure text (.txt) file.

thanks for the response, but when I set it such that the user can select the file that txt file I created is opened with no problem. I was sure when I used TextEdit to create a plain text document. But as a test I will use vi which I am sure will be a plain text doc.
Thanks

You were right Adam,
TextEdit, even in plain text, does not work. I used vi and had no problems. Thanks.

On my machines, saving a TextEdit document as plain text produces a plain text file and your script works. I’m not exactly sure why it’s not working for you, but it contains a few unwise things.

  1. You don’t need to put the code in an Application ‘tell’ block, since no applications are involved.
  2. The ‘try’ block should end before the ‘close access’ line to ensure that the file is closed again if anything goes wrong before that line’s reached. The way you’ve got it, an error will cause the execution to jump straight to the ‘display alert’ line. It may be that an error on a previous test run caused the ‘close access’ line not to be executed, leaving the file open with its file position pointer at the end. Trying to read it again without resetting the pointer would result in another error.
  3. Since ‘theFile’ is just a string, you should ideally use the word ‘file’ or ‘alias’ before it in the ‘open for access’ line. However, ‘open for access’ is one of those commands where you can get away with just the path.
  4. If you use ‘open for access’, you should use the reference it returns as the ‘read’ parameter, not ‘theFile’. Alternatively, use ‘theFile’ but don’t use ‘open for access’ or ‘close access’. (There’s quite an involved explanation for this, but I’ll attempt it if you actually want to know.)
set theFile to "Macintosh HD:home:t:taperoom:desktop:ListOfSequences.txt"
try
	set fRef to (open for access file theFile)
	set fileContents to (read fRef using delimiter {","})
on error msg
	close access fRef
	display dialog msg
	error number -128
end try

close access theFile

set txtvar1 to item 1 of fileContents
display alert txtvar1


-- Or:
set theFile to "Macintosh HD:home:t:taperoom:desktop:ListOfSequences.txt"
try
	set fileContents to (read file theFile using delimiter {","})
on error msg
	display dialog msg
	error number -128
end try

set txtvar1 to item 1 of fileContents
display alert txtvar1

That’s interesting, Nigel because on mine, TE v1.3 doesn’t offer the choice. It’s “Save As.” panel’s File Format menu offers Rich Text Format (RTF) and Word Format. How do you save as pure text on your machines?

Well, I suppose “saving as plain text” is not quite the right way to describe it. Sorry. I convert the document to plain text using “Make Plain Text” in the “Format” menu (or Shift-Command-T) and then save it.

Thanks, Nigel;

  1. I wish that was in TextEdit’s dictionary - not so, so gui scripting to get there.

  2. I wish BBEdit had a similar choice. It makes everything unicode text.

Or something along the lines of:

tell application "TextEdit"
	-- Get the front document text as plain text.
	set {text:myText} to (text of front document as string)
	-- Get a file specification for the proposed file. ('choose file name' catches any duplicates.)
	if (name of front document exists) then
		set defaultName to name of front document
	else
		set defaultName to name of front window
	end if
	if (defaultName does not end with ".txt") then set defaultName to defaultName & ".txt"
	set fred to (choose file name with prompt "Save As Plain Text:" default name defaultName)
end tell

set fRef to (open for access fred with write permission)
try
	set eof fred to 0
	write myText to fRef
end try
close access fRef

(* tell application "TextEdit"
	open (fred as alias)
end tell *)

Is this not “passing the buck”, Nigel?

Maybe. :slight_smile: As with much in AppleScript, it depends on your approach. If you think TextEdit’s scripting dictionary should have a command that saves an RTF document as plain text, then the above might be considered to be “passing the buck”. If you’re happy that you can achieve what you want with what there is, then it’s just “a way to do it”.

Agree with that philosophy