Writing & Reading file using shell script

Hi everyone,

I am writing to a text file using a shell script.

set theFilePath to (path to desktop folder) & graphicList as string
set theFile to open for access file theFilePath with write permission
try
	write (do shell script "ls " & quoted form of POSIX path of x) to theFile
	close access theFile
on error
	close access theFile
end try

If I write directly to the file as above then I can not read it correctly using.

set theResult to paragraphs of (do shell script "/bin/cat " & docText & " | /usr/bin/grep " & fileName)

It does work however if I write to a BBEdit doc.

tell application "BBEdit"
	make new document with properties {name:"GraphicsList.txt", text:do shell script "ls " & quoted form of POSIX path of x}
	set bounds of window of document 1 to {4, 44, 472, 538}
	save window of document 1 to (path to desktop as string) & "GraphicsList.txt"
	close document 1 saving no
end tell

I think it might have something to do with the way the carriage returns are being handled but if I was sure I wouldn’t be asking.

Thanks for your help!

Craig

Model: Mac G5 Quad
Browser: Safari 522.12
Operating System: Mac OS X (10.4)

I just tried your script and the only problem I had was that I was specifying the variable x wrong. here’s what I came up with:

set x to ":Users:me:"
set theFilePath to (path to desktop folder) & "bob.txt" as string
set theFile to open for access file theFilePath with write permission
try
	set eof of theFile to 0
	write (do shell script "ls " & quoted form of POSIX path of x) to theFile
	close access theFile
on error msg
	display alert msg
	close access theFile
end try
do shell script "cat ~/Desktop/bob.txt"

But if you’re going to be writing the output of a shell script anyway, you might as well just put it all in one line:

set x to ":Users:me:"
set filePath to ":Users:me:Desktop:bob.txt"
do shell script "ls " & (quoted form of POSIX path of x) & " > " & (quoted form of POSIX path of filePath)

The Noob’s second method is the way to go. I like to take advantage of the generic locations like this, and use the recurrent switch (-R) to dig down:

set x to path to documents folder as Unicode text
set Now to short date string of (current date)
set filePath to (path to desktop folder as Unicode text) & "Docs_" & Now & ".txt"
do shell script "ls -R " & (quoted form of POSIX path of x) & " > " & (quoted form of POSIX path of filePath)

That works great! Thank you both for your response.

But, for my own education. Why does it not work when written to a text file the other way?
When searching for a specific item it returns the entire file contents.

Thanks again!

Craig

Written like this, your original works fine:

set theFilePath to (path to desktop folder as text) & "Out.txt"
set x to (path to documents folder as text)
set theFile to open for access file theFilePath with write permission
try
	write (do shell script "ls " & quoted form of POSIX path of x) to theFile
	close access theFile
on error
	close access theFile
end try