Reading file contents into a string

Hi,

I’m having a problem with reading file contents into a string. Here is the code:

set test to do shell script "cat /Applications/Quake3/baseq3/iquake.cfg"
	
set content of text field "readTest" to test

This gives me an error:

Can’t make «class conT» of «class texF» “readTest” into type reference. (-1700)

I’m not sure if this is the right shell script to get the content of a text file, but at least in Terminal it showed me the line I had in my file. Any suggestions?

PS: I’m not sure if this is the right forum, but I’m doing the project in AppleScript Studio, so I guess it’s the right forum

Thanks,
dr4cula

Hi,

you are in the right forum. :wink:
I think, the reference isn’t complete, try this

set content of text field "readTest" of window "main" to test

Thanks, it worked!

Hm, I ran into another problem, but since it is with shell script, I’ll post it here :smiley:

Anyway, I have this:

do shell script "echo " & "(text)" & " > /Applications/Quake3/baseq3/iquake.cfg"

But that gives me an error:

sh: -c: line 1: syntax error near unexpected token text' sh: -c: line 1: echo (text) > /Applications/Quake3/baseq3/iquake.cfg’ (2)

Those () are the problems, but I need those to write some content into the text file. I tried without () and it worked. I guess it’s some sort of a unix thing, but I don’t know how to get rid of the error …

Thanks,
dr4cula

try

do shell script "echo " & quoted form of ("(text)" & " > /Applications/Quake3/baseq3/iquake.cfg")

Well, now it didn’t give me an error, but it didn’t do anything. It didn’t write (test) into the file.

sorry, remove the parentheses

do shell script "echo " & quoted form of "(text)" & " > /Applications/Quake3/baseq3/iquake.cfg"

Why are you using shell scripts to read and write to files? Applescript has built in commands for this, and they should provide you with all you need to write simple text to files and more. Do a search for “write to file” for plenty of examples.

Because I couldn’t get those read from file and write to file work. Said something bad file or whatever. Besides, the shell scripts gave me alot more answers on google and I decided to go with those. Anyway, StefanK, thanks alot, it worked! :slight_smile: