Failure to save from TextEdit

All,

Sorry to bother. I’m having issues trying to save a document from TextEdit on Tiger. I’m new to AppleScript and have found no good example for saving an Untitled file to a specific path + filename.

I’m expecting this to save the file as /Users/grapeguy/Dev/25053.txt. Instead I get the Save dialog with the correct path but “Untitled 2” as the filename.


property out_path : "Users:grapeguy:Dev:"
property message_id : 23053

tell application "TextEdit"
	activate
	make new document at the front
	keystroke "this is a test"
	save document 1 in file ((out_path as text) & message_id)
end tell

Thanks in advance.

Bill Stephens

Hi Bill,

no file specifier, just the string path


property out_path : "Users:grapeguy:Dev:"
property message_id : 23053

tell application "TextEdit"
	activate
	make new document at the front
	set text of document 1 to "this is a test"
	save document 1 in (out_path & message_id) -- the coercion is useless: out_path IS text
end tell

Stefan,

Thanks for the quick reply, but this is still failing. I get the same save dialog with the “untitled” filename.

Bill S.

The script works also in Tiger.
Is the path in the property correct.
HFS paths start with the disk name (e.g. "Macintosh HD:Users:grapeguy:Dev:)

Stefan,

That doesn’t work either. I’ve tried saving from TextEdit, Safari and Firefox and it has never worked. What a pain.

The only way that I’ve found that works is this:


set logAlias to out_path & message_id & ".txt"
set logFile to (open for access file logAlias with write permission) -- open the file (creates it if necessary)
set eof logFile to 0 -- clear out any existing content
write pageSource & return to logFile
close access logFile

Unfortunately, that causes odd characters to be present in the file that I cannot insert in this message.

Thanks,
Bill S.

Does this work for you?

property out_path : "Users:grapeguy:Dev:"
tell application "TextEdit"
	set theDoc to make new document with properties {text:"this is a test"}
	close theDoc saving yes saving in (out_path & "23053.txt")
end tell

Tom

Browser: Camino
Operating System: Mac OS X (10.4)

Tom_x,

That works, but the “.txt” is missing from the filename (which is OK).

Bill S.

It should have the extension .txt – try deleting TextEdit’s prefs file (com.apple.TextEdit.plist).

In your Finder Preferences->Advanced, do you have ‘Show all file extensions’ checked?

Tom_X,

The preferences setting has been updated. It works perfectly.

I can’t figure out why the regular ‘save’ call doesn’t work. To avoid the headaches I’ll use your method.

Thanks,
Bill S.

I can’t figure it out either, but there does seem to be a problem. Under the normal implementation of the save command, the following should work, provided that out_path is correct, that all the folders exist, and that you’ve got TextEdit configured to create new documents in Plain Text mode:

property out_path : "Macintosh HD:Users:grapeguy:Dev:"
property message_id : 23053

tell application "TextEdit"
	activate
	make new document at the front
	set text of document 1 to "this is a test"
	save document 1 in file (out_path & message_id & ".txt")
end tell

It does work on my Tiger machine ” at first ” but then it begins not to, producing instead a Save dialog in the way you describe. After that, it works sometimes, but usually not. I haven’t been able to pin down the influencing factors. I’ve tried committing the ‘new document’ reference to a variable and Stefan’s suggestion of using an HFS path instead of a file specifier. Both either work or don’t. The only cure so far seems to be to quit TextEdit between each save.

It may be that TextEdit’s save implementation is inherently flaky ” or at least that its designers are. I looked at the problem last night on my Jaguar machine and save only works there if used with a POSIX path! (This goes against all the traditions of AppleScript and isn’t noted in TextEdit’s dictionary.) On my Tiger machine, using a POSIX path saves the file to the top level of the startup disk with the entire path as its name!