What's the proper way to save this TextEdit Doc as rtf on my desktop?

tell application "TextEdit"
	set E to make new document at end of documents with properties {name:"Test Format"}
	tell E
		set its text to (date string of (current date) as text)
		set font of paragraph 1 to "HelveticaNeue-Bold"
		set size of paragraph 1 to 14
		set color of paragraph 1 to {6206, 18214, 52487} -- dark blue
	end tell
end tell

One of my attempts (reported in bugs) actually erased my desktop folder.

I’m stuck on a PC so I can’t try this now, but you might try your other attempt with a text path:

set folderpath to path to desktop folder as text
   set E to make new document at end of documents with properties {name:"Test Format", path:folderpath}

Hi,

I don’t like the proper way :wink:

tell application "TextEdit"
	activate
	set E to make new document at end of documents with properties {name:"Test Format"}
	tell E
		set its text to (date string of (current date) as text)
		set font of paragraph 1 to "HelveticaNeue-Bold"
		set size of paragraph 1 to 14
		set color of paragraph 1 to {6206, 18214, 52487} -- dark blue
	end tell
end tell
tell application "System Events"
	tell process "TextEdit"
		keystroke "s" using {command down, shift down}
		tell window 1
			repeat until exists sheet 1
				delay 0.5
			end repeat
			keystroke "d" using command down
			click button "Save" of sheet 1
		end tell
	end tell
end tell

I think that’s it Stefan – the “proper way” is broken and further, I’ve discovered that my method breaks Standard Additions too – you have to restart before “path to desktop folder” works again. :frowning:

This works

tell application "TextEdit"
	set pathtofile to "Macintosh HD:Users:[b]shortusername[/b]:Desktop:pdf:test.txt" as text
	set E to make new document at end of documents
	tell E
		--set format to "rtf"
		set its text to (date string of (current date) as text)
		set font of paragraph 1 to "HelveticaNeue-Bold"
		set size of paragraph 1 to 14
		set color of paragraph 1 to {6206, 18214, 52487} -- dark blue
		save in file pathtofile
	end tell
end tell

… although I can’t get it to set the format to rtf. :frowning:

Hi, Adam.

  1. I did that once. It’s because you’ve specified the Desktop folder as the save destination. The ‘save’ paradigm with any application is that you’re saving a document to a file, not a file to a folder, so you have to specify a file path (or file specifier) as the destination. Otherwise, the folder you’ve specified will very likely get overwritten. Some applications protect you from yourself in this respect; TextEdit apparently not. :slight_smile:

  2. A TextEdit document’s ‘path’ property is a POSIX path. On my Tiger machine, if I set the document’s ‘path’ to a POSIX path (including the file name, of course!), just saving the document without an ‘in’ parameter creates the file as and where expected. The path has to be set after the document’s created:

tell application "TextEdit"
	set E to make new document at end of documents
	tell E
		set its text to (date string of (current date) as text)
		set font of paragraph 1 to "HelveticaNeue-Bold"
		set size of paragraph 1 to 14
		set color of paragraph 1 to {6206, 18214, 52487} -- dark blue
		
		-- Either:
		-- (path) parenthesised so that the compiler doesn't think 'path to'.
		set (path) to POSIX path of ((path to desktop as Unicode text) & "Test Format.rtf")
		save
		
		-- Or:
		save in file ((path to desktop as Unicode text) & "Test Format.rtf")
	end tell
end tell

I’m assuming you have TextEdit configured to use RTF by default.

AHA!! – Makes perfect sense, should have seen it myself. :rolleyes: Thanks, as always, Nigel. I still consider it a bit of a bug that TE’s warning buttons don’t work – no matter what you select, (Overwrite or Don’t Overwrite) it overwrites! The warning that the desktop is locked now makes sense, though.

Leave it to TE to require a posix path – I tried many things but not that.

tell application "TextEdit"
	set E to make new document at end of documents
	tell E
		set its text to (date string of (current date) as text)
		set font of paragraph 1 to "HelveticaNeue-Bold"
		set size of paragraph 1 to 14
		set color of paragraph 1 to {6206, 18214, 52487} -- dark blue
		
		-- Either:
		-- (path) parenthesised so that the compiler doesn't think 'path to'.
		set (path) to POSIX path of ((path to desktop as Unicode text) & "Test Format.rtf")
		save
		
		-- Or:
		save in file ((path to desktop as Unicode text) & "Test Format.rtf")
	end tell
end tell

I was actually playing with the formatting and “discovered” this problem serendipitously.

Final question, Nigel: is there a way to underline text from a script (other than mashing the TE GUI?)

{EDIT} I now see why StefanK uses a gui method to save – when you save as outlined above, TE ignores your preference setting and saves it as plain text with all the formatting hash in place. TE really is defective, in my view.

Not that I can find. Underlining part of the text manually splits it into separate ‘attribute runs’ for AppleScript, but they all appear to have the same properties. I thought an underlined font might be switch in, but apparently not.

On both my machines (Tiger and Jaguar), TE ignores everything except the preference setting. The files either contain plain text or text with RTF formatting tags.

I find it a handy little app in its own right, but its AppleScript implementation certainly leaves a lot to be desired! :frowning:

I’m not sure if I made it clear in my script that one of the “Either: . Or:” sections should be either commented out or removed. The script errors (for me) if they’re both left in.

Thanks, Nigel. Yes it was clear that they were mutually exclusive.