Creating/Writing text files

I don’t know why this is rocket science but I need to create a text file and write to it if it does not exist. I also need to rewrite the file if it does exist. All the various examples I can find obscure what I need to do by throwing in fancy ways to get a file name from the user. I already have the file name I need to use. Given a known filename path, how do I determine if the file exists, create it if not, then write to it or overwrite the existing file. Thanks.

Hi,

take a look at this thread: http://bbs.applescript.net/viewtopic.php?id=20063

Hi,
I use this handler in part of a larger script but I think this will do what you want. If you don’t want to use TextEdit then we can just write the data to a file ref.

set data_entry to "my text file"
Enter_Data(data_entry)

on Enter_Data(data_entry)
	set HD_Path to path to startup disk as string
	set text_filename to "Job Log"
	set master_text_file to "Macintosh HD:Job Log"
	tell application "Finder"
		if alias master_text_file exists then
			tell application "TextEdit"
				activate
				open master_text_file
				make new text at after last paragraph of front document with data data_entry & return
				save document 1
				close window 1
			end tell
		else
			
			tell application "TextEdit"
				activate
				make new document
				make new text at after last paragraph of front document with data data_entry & return
				save document 1 in file (HD_Path & text_filename)
				
				close window 1
				close every document saving no
			end tell
		end if
	end tell
end Enter_Data

Put the data you want to write in the data_entry variable and this will write it to the text file.

Thanks for the replies but my problem seems to be the path. Here is what I have:

When I to use that variable I always get the error:

Can’t make file “Common:AIM Apps:AIM Group List” into type reference.

For example:

Not quite sure what you mean but if you’re trying to check if a file exists and if it doesn’t then create a blank text file to write to then this should be what you need.

set groupfile to file "Common:AIM Apps:AIM Group List"
tell application "Finder"
	if alias groupfile exists then
		--do something
	else
		set fileRef to (open for access ("AIM Group List") with write permission)
	end if
end tell

sorry you’ll need to change the first line of the script from

set groupfile to file "Common:AIM Apps:AIM Group List"

to

set groupfile to "Common:AIM Apps:AIM Group List"

Here is where I’m getting myself into trouble. You have the scenario correct. However, where does your suggested code put the file? Here is what I did to test your suggestion (for now I am assuming the file does not exist):

set groupfile to "Common:AIM Apps:AIM Group List"
tell application "Finder"
	if not (alias groupfile exists) then
		set file_ref to (open for access ("AIM Group List") with write permission)
		write "test line" to file_ref
		close access file_ref
	end if
end tell

How does the open for access command know to put the file in groupfile? When I run this, there are no errors but there is also no file that I can find anywhere.

Hi there,
if “Common:AIM Apps:” is the path to where you want to put a file called “AIM Group List” then try the script below.

set groupfile to "Common:AIM Apps:AIM Group List"
tell application "Finder"
	if not (alias groupfile exists) then
		set file_ref to (open for access ("Common:AIM Apps:AIM Group List") with write permission)
		write "test line" to file_ref
		close access file_ref
	end if
end tell

hope this is getting closer to what you want?

You should find your file on top level of the startup disk :wink:

PS: testing whether the file exists, is not necessary with AppleScript’s read/write commands.
If the file doesn’t exist, it will be created automatically

I don’t get it. That is what I had waaaaay back at the beginning, before I even posted this problem. The only difference is the parentheses. So what is the rule? How do I know when to use them or not? Thanks.

You need to specify the whole path, e.g.
open for access “AIM Group List” with write permission → creates a file “AIM Group List” on top level of the startup disk
open for access ((path to desktop as Unicode text) & “AIM Group List”) with write permission → creates a file “AIM Group List” on desktop
open for access “Common:AIM Apps:AIM Group List” with write permission → creates a file “AIM Group List” in folder “AIM Apps” of disk “Common”