how to create a text file

Hi All,

I want to create a text file at location “MAC:Rajeev:DataFolder” with the following text

File name should “person.txt”

Thanks
Rajeev

try this:

set LogFile to "MAC:Rajeev:DataFolder:person.txt"
set errorList to "<Rajeev>
<Kumar>
<Sinha>
<India>"

set tmpLogdatei to (open for access file LogFile with write permission)
write errorList starting at eof to tmpLogdatei
close access tmpLogdatei

greets from TMA

From the FAQ:
http://bbs.applescript.net/viewtopic.php?id=14412
http://bbs.applescript.net/viewtopic.php?pid=46912
http://bbs.applescript.net/viewtopic.php?pid=47369
http://bbs.applescript.net/viewtopic.php?pid=47370

Hi is there a reason I should not do it like this. without opening and closing the file.

this seems to work, if the file is not there its made.
if it has text already its added to.

Cheers

set theText to "<Rajeev>
<Kumar>
<Sinha>
<India>"
set theRead to ""
tell application "Finder"
	set The_Path to "MAC:Rajeev:DataFolder:"
end tell
set file_name to "person.txt"
set the destination_file to The_Path & file_name
do shell script " touch " & quoted form of POSIX path of The_Path & file_name
try
	set theRead to read file destination_file
on error
	if theRead is "End of file error" then
	end if
end try
write theText & return & theRead to file destination_file

TMA’s code is the way to go (maybe adding some extra error trapping if needed). It’s faster and cleaner. And will also create the file if needed.

Sorry I was not very clear. (been up all night)
What I mean’t is if you are writing to a file in AS is there any reason you Need to State open for access.

** scrub that just worked out TMA’s script.
Writing to a file is ok, but the Open for access creates the file also if its not there.

And I like the eof bit.

This means I may stop using do shell script to chop up text files.

Can I ask what the code is for setting the text at the beginning of the file.

Thanks
Mark

Hi All,

Thanks fine it is working, but here is a problem. If the person.txt is already present in that directory then it will ask for over write. Present scenario, If I will run it twice it again type these text in the file. But I want to first check if it is allready available in that directory it should ask for replacement or first it delete that file and then write it.

Thanks
Rajeev

Some times I use also a previous “touch” (before writing to the file), as “open for access” will set the creator and file type to “ttxt/TEXT” by default. You could also instruct the Finder to change them to (ASCII 0) - 4 times, but I find “touch” easier and faster.

When you use “open” and “close” for access you’re creating something as a “buffer”, which is believed to be a safe practice, just in case someone else is trying to write to your file at the same time. I think also some options in the read/write commands are only accesible if you first “open for access” the file.

The options for read/write commands are in the Standard Additions dictionary.

If you use this code in an existing file containing “I am a dog”:

set f to open for access file x with write permission
write "Love" to f
close access f

It will write at the beginning, and you will have now “Love a dog”. If you use:

set f to open for access file x with write permission
write "Love" to f
write " Al" to f
close access f

You will have instead “Love Aldog”.

You can set the size in bytes of the file using “set eof”:

set f to open for access file x with write permission
set eof of f to 0
write "Love" to f
close access f

This way, you empty the file before writing.

And so on…

set x to "MAC:Rajeev:DataFolder:person.txt"
try
	x as alias
	display dialog "Should I overwrite?" buttons {"No", "Yes"} with icon 1 default button 2
	if button returned of result is "Yes" then error
on error --> file doesn't exist or user wishes overwriting
	--> write from scratch, overwrite, whatever
end try

This looks pretty similar to other suggestions.

set DataFile to "MAC:Rajeev:DataFolder:person.txt"
set TheText to "<Rajeev>
<Kumar>
<Sinha>
<India>" as text
set DF to open for access file DataFile with write permission
try
	set eof of DF to 0
	write TheText to DF
	close access DF
on error
	close access DF
end try
beep 3
display dialog "All done!" with icon note giving up after 3

i just use my handler for logdatas:

errorList = your text as string
LogFile = your destination file
overwriteVar = true/false

on writeLogDatei(errorList, LogFile, overwriteVar)
	tell application "Finder"
		activate
		if overwriteVar then
			try
				delete file LogFile
			end try
		end if
		
		try
			close access file LogFile
		end try
		set tmpLogdatei to (open for access file LogFile with write permission)
		write errorList starting at eof to tmpLogdatei
		close access tmpLogdatei
	end tell
end writeLogDatei

@mark: and when ya want to add text at start of logfile you have to read out all the text from logfile, set your newStrings+oldStrings and write all back to the logfile.

hope i can help.

greets from
TMA

JJ , TMA

Thats a bit clearer for me now.

Thanks

Hi All

This time I have changed my code and it is working fine.


set theHD to (path to startup disk as string)
set theTargetPath to theHD & "Library:Application Support:Adobe:PrintSpt:"

tell application "Finder"
	
	set EMP_CODE to text returned of (display dialog "Please enter your code" default answer "XXX") as string
	set theText to ("<RAJEEV>
	<KUMAR>
	<SINHA>" & EMP_CODE)
	--try
	--	if EMP_CODE is not string then
	--		display dialog "Invalid Code Entered"
	--	end if
	--end try
	set theFileName to EMP_CODE & ".txt"
	set destination_file to theTargetPath & theFileName
	
	set tempDest_File to (open for access file destination_file with write permission)
	
	write theText starting at eof to tempDest_File
	close access tempDest_File
	
	
	tell application "Adobe InDesign CS2"
		activate
		
	end tell
end tell

Now I want to check if the files already exist in that folder. If already exist, then it should be over write without asking to user.

Thanks
Rajeev