Finder hangs while trying to write into text file

Hi all !

I’m new here. This is my first post. Just to let you know :smiley:

I’ve written a little applescript to write a string to a text file. If the text file already exists, it should delete it and create a new one.

The problem is that sometimes it works and sometimes the Finder went’s down. (Mostly after doing nothing for a short periode of time.)

When “the Finder wents down”, all my desktop icons disapear and my home user directory isn’t accessible any more. I have to restart the Finder manually to get things working again.

This is the applescript:


tell application "Finder"
	if exists file "Macintosh HD:textfile.txt" then
		do shell script "rm /textfile.txt"
	end if
	make new file at "Macintosh HD" with properties {name:"textfile.txt", kind:"file", creator type:"ttxt", owner:"2al", owner privileges:"read write"}
	set f to open for access "Macintosh HD:textfile.txt" with write permission
	write "ladida text" to f
	close access f
end tell

Thanks a lot for help
Frederic

Model: Powermac G4 1.25 GHz 15"
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hello

It seems that the explanation maybe on the do shell script track.

I ran 30 times this version:

tell application "Finder"
	if exists file "Macintosh HD:textfile.txt" then
		delete file "Macintosh HD:textfile.txt"
	end if
	make new file at "Macintosh HD" with properties {name:"textfile.txt", kind:"file", creator type:"ttxt", owner:"2al", owner privileges:"read write"}
	set f to open for access "Macintosh HD:textfile.txt" with write permission
	write "ladida text" to f
	close access f
end tell

Yvan KOENIG (from FRANCE lundi 16 octobre 2006 18:32:18)

Thanks for reply

I had myself the “delete” function instead of shell command “rm” already in my script but didn’t like that the deleted file will show up in the trash directory.

I also thought of calling an “empty trash” function but i can’t do that since the script will not run on my private computer and i need to leave decision of emptying the trash to the computer owner.

Is there an applescript function which really deletes a file like “rm” does ?

Frederic

Hello

OK, don’t trash the file, use the set eof command.

When the file is open for access, set its eof to zero will free the disk space without moving the file to the trash.

Look at “AppleScript Scripting Additions Guide” for details. It is very old but on this subject it is always right.

tell application "Finder"
	if not (exists file "Macintosh HD:textfile.txt") then
		make new file at "Macintosh HD" with properties {name:"textfile.txt", kind:"file", creator type:"ttxt", owner:"2al", owner privileges:"read write"}
	end if
	set f to open for access "Macintosh HD:textfile.txt" with write permission
	set eof of f to 0
	write "ladida text" to f
	close access f
end tell

Yvan KOENIG (from FRANCE lundi 16 octobre 2006 20:28:14)