What's wrong with this script?

this should work!! i’ve tried a million similar things… what am i doing wrong?


on clicked theObject
	tell save panel
		set required file type to "txt"
		set treat packages as directories to false
	end tell
	display save panel attached to window "main"
end clicked

on panel ended with result withResult
	if withResult is 1 then
		set the_file to path name of save panel
		set textinput to contents of text view 1 of scroll view "log" of drawer "drawer" of window "main"
		set the_header to ("this is the content:" & return & return)
		set the_data to (the_header & textinput)
		try
			open for access the_file with write permission
			set eof of the_file to 0
			write (the_data) to the_file starting at eof
			close access the_file
		on error
			try
				close access the_file
			end try
		end try
	end if
end panel ended

The only wierd thing I saw on brief inspection was…

on panel ended with result withResult

…might have to be…

on panel ended theObject with result withResult

Good luck…
j

This slightly simpler one doesn’t work either. the dialog box confirms the path correctly but then it saves it onto the root level of the hard drive with a name like “/Users/me/Desktop/savedammit.txt”

Anybody tried this before?


on clicked theObject
	tell save panel
		set required file type to "txt"
		set treat packages as directories to false
	end tell
	display save panel attached to window of theObject
end clicked

on panel ended theObject with result withResult
	if withResult is 1 then
		set woot to path name of save panel
		display dialog woot
		set fileRef to (open for access file woot with write permission)
		set eof of fileRef to 0
		write "this is a test" to fileRef
		close access fileRef
	end if
end panel ended

Checking the Standard Additions dictionary shows the following for “open for access”:

As you probably saw in the display dialog, the “the_file” will be a POSIX-style (or slash-delimited) path. The “open for access” command requires an HFS-style (or colon-delimited) path.

If I were you, I would rename the variable that you set “to path name of save panel” to reflect the POSIX-nature. So “set the_POSIX_file to path name of save panel”. Then something like “set the_file to (POSIX file the_POSIX_file)”. And then you should likely be able to use “the_file” as you have in the rest of the script.

Hope this helps…

Hey again, Supa.

I did some messing around with the save panel and found a few quirks with your original code. First… and this was the KEY for me… make sure that you point the ‘panel ended’ event of the “main” window at the script. The save panel inherits it’s applescript event binding from the window it’s attached to, so you need to enable the window’s panel ended event to make the save panel actually send it’s data when it ends.

Also, you DO need to use the posix path when writing/saving your file, as MarkDouma suggested. In your panel ended handler change…

set the_file to path name of save panel

…to…

set the_file to (POSIX file (path name of save panel))

I would also keep the ‘panel ended’ handler written like this…

on panel ended theObject with result withResult

… as I suggested before. Other than these few things, I found no other problems with your original code.

Damn details!! :wink: Hope that solves your problems…
j

that was the problem, it works great now, thanks a billion

How can you reference the save panel, since I have lots of panels that come from the “main” window, and I only want that to happen when the save panel ends?

This seems to work for me…

on panel ended theObject with result withResult
	if theObject is save panel then
		-- Blah, Blah, Blah... --
	end if
end panel ended

j

OK, thanks. Is there a way to change the “Kind” of the file. Currently, it’s a “Plain text file” but I want to be able to customize this, though still have it be a “.txt” file. For example, AppleWorks has the kind as "com.apple.appleworks.document. Is this possible in AppleScript?