creating a new file and then writing to it

According to Rosenthal’s “AppleScript … a Comprehensive Guide”, to create a file reference you can:


tell application "Finder" to set fileRef to file "file name"
-- or --
set fileRef to alias "file name" -- with no tell application "Finder"
-- or --
set fileRef to a reference to file "file name" -- with no tell application "Finder"

Looking at the following, I get a “file not found” with set tempRef to open for access alias myScriptFile with write permission The only way I avoid this error is to place the end tell following if not libraryExists - end if??


on loadFilesScript()
	
	tell application "Finder" to ¬
		set myScriptPath to (path to application support folder from user domain as Unicode text)
	set myScriptFile to myScriptPath & "Excel Medical:allFiles.scpt"
	
	try
		set libraryExists to true
		alias myScriptFile
	on error
		set libraryExists to false
	end try
	
	if not libraryExists then -- create it
		set itsContent to "property theFiles : {}"
		
		set tempRef to open for access alias myScriptFile with write permission
		write itsContent to tempRef
		close access tempRef
	end if
	-- end tell

	set filesScriptRef to alias myScriptFile
	tell application "Script Editor" to save filesScriptRef as "text"
	set filesScriptObj to load script filesScriptRef
	
end loadFilesScript

As long as I’m pleading for help, when I do place the end tell following if not libraryExists - end if, with:

set filesScriptObj to load script filesScriptRef

I get another error “Script doesn’t seem to belong to AppleScript” … the file is created and contains the correct content = “property theFiles : {}” – but
when I open the file in Script Editor and attempt to save it, Script Editor responds with “Can’t save the file” … ??

Hi John

From Page 450
. you use a proper file reference

open for access "Macintosh HD:new file.txt"

will work but this is better:

open for access file "Macintosh HD:new file.txt"

This is OK too:

open for access alias "Macintosh HD:new file.txt"

The only problem is with the last script line is that if the file doesn’t exist, the script will refuse to compile

Personally I always use the second form (without any Finder tell block)

Thanks, Stephan … works like a champ … sometimes alias works, sometimes file is required depending on whether inside a tell app block … still trying to find the common why.

For that purpose, Rosenthal’s heavy weight AppleScript book is good, but falls short on explaining why … I suppose the only folk who know the under-the-cover details are the designers of AppleScript who started with a list of rules, I suppose.

I guess there is a terminology clash in a Finder tell block using the keyword file because the Finder has its own file element
I always use read/write commands (and other crucial commands from Standrad Additions) either in a handler or outside an application tell block

What tell is that supposed to end? The only one you have in your handler is for Finder, and that uses to. (See also: tell Statements)

Try something like this:

on loadFilesScript()
	try
		set scriptFile to ((path to application support folder from user domain as Unicode text) & "Excel Medical:allFiles.scpt")
		set scriptFile to scriptFile as alias
	on error
		script allFiles
			property theFiles : {}
		end script
		
		store script allFiles in file scriptFile replacing no
		set scriptFile to scriptFile as alias
	end try
	
	set filesScriptObj to load script scriptFile
end loadFilesScript

How 'bout that, Bruce … so doggone simple and short and it does work.

Just one curious thing? – using Leopard, the new file’s size reads 430K … open it up with Script Editor and save it … file size down to 8K.

As stated, it works, so just a curiosity thing.