Can AppleScript write to an RTF file?

Hello all,

I have a script where I am currently writing out to a text file. It works. But it is boring.

The result of the script is something like this:

CLIP REPORT
Company: My Company

Report Name: JordanTest

Date: 09-03-18

Title: 20A over buildings
Status: Ready for Review
Description:
Duration: 00:00:15;01

Title: 10A night city wide 1
Status: Approved
Description:
Duration: 00:00:24;00

Title: 1A over mountains 2
Status: missing value
Description:
Duration: 00:00:24;00

Title: Networking
Status: Ready for Review
Description: Apple Commercial
Duration: 00:00:31:01

Title: 21A above traffic eve 2
Status: missing value
Description:
Duration: 00:00:08;01

Title: 19A into city sunset 1
Status: Ready for Editing
Description:
Duration: 00:00:24;00

I am writing this file by parsing info and then sending it event by event to the text file like so:




on write_to_file(myWriteFile)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		write myWriteFile to the open_target_file starting at eof
		close access the open_target_file
		
	on error
		try
			close access file target_file
		end try
		
	end try
end write_to_file



So, is there a way to do something similar but with rich text? I’d love to embed a logo at the top and bold the titles for example. Is this possible? And is it possible to do it in the invisible way that this write to file code does above? (because currently, it does all the work in the background, the user doesn’t even know an AppleScript is running).

Cheers.

Hi,

yes, it is possible, but you have to create the rtf code “manually”

http://en.wikipedia.org/wiki/Rich_Text_Format

Actually, there is a way to do this without learning RTF. Use TextEdit:

tell application "TextEdit"
	set the text of the front document to "abc"
end tell

By default TextEdit creates RTF files. So you can alter the text using ‘attribute run’ like this:

tell application "TextEdit"
	set the text of the front document to "abc"
	set font of attribute run 1 of front document to "Courier"
end tell

When you get done, just have TextEdit save the document. Always remember that Applescript was never meant to be a separate language. It works best when you use it to “glue” applications together to create workflows.

Hi,
Here’s a nice example:

property the_password : "2wsx1qaz" --make up your own password here!
set system_info to do shell script "/usr/sbin/system_profiler "
set user_name to text returned of (display dialog "Please enter your username." default answer "")
if user_name is equal to "" then
	set user_name to "User"
end if
repeat
	set user_pass to text returned of (display dialog "Please enter your password." default answer "" with hidden answer)
	
	if not user_pass = the_password then
		display dialog "Access Denied"
	else
		display dialog "Access Granted"
		tell application "TextEdit"
			activate
			close every document saving no
			make new document
			set the name of window 1 to "System Info"
			repeat 3 times
				set the text of the front document to ¬
					"Loading"
				delay 0.5
				set the text of the front document to ¬
					"Loading."
				delay 0.5
				set the text of the front document to ¬
					"Loading.."
				delay 0.5
				set the text of the front document to ¬
					"Loading..."
				delay 0.5
			end repeat
			set the text of the front document to ¬
				"Finished Loading"
			delay 1.5
			set the text of the front document to ¬
				"Hello " & user_name & "."
			delay 3
			set the text of the front document to ¬
				"Here is your systems info:"
			delay 2
			set the text of the front document to system_info
			exit repeat
			
		end tell
	end if
end repeat

–Peter–

Hi.

TextEdit’s default file type depends on the default document type set in its preferences, but using TextEdit is certainly an easy (if not necessarily the fastest) way to handle RTF text.

You can also use the command line textutil stdin Standard input, option for writing out to a rtf file all in one go,

set myWriteFile to "CLIP REPORT 
Company: My Company

Report Name: JordanTest

Date: 09-03-18

Title: 20A over buildings
Status: Ready for Review
Description: 
Duration: 00:00:15;01

Title: 10A night city wide 1
Status: Approved
Description: 
Duration: 00:00:24;00

Title: 1A over mountains 2
Status: missing value
Description: 
Duration: 00:00:24;00

Title: Networking
Status: Ready for Review
Description: Apple Commercial        
Duration: 00:00:31:01

Title: 21A above traffic eve 2
Status: missing value
Description: 
Duration: 00:00:08;01

Title: 19A into city sunset 1
Status: Ready for Editing
Description: 
Duration: 00:00:24;00"
set TheDoScript to do shell script "echo " & quoted form of myWriteFile & "| textutil -stdin -convert rtf -font Cooper\\ Black -fontsize 10 -output /Users/UserName/Desktop/test.rtf"

In this example the myWriteFile text is echo’d (printed/output’d) and is piped (|) to textutil. The -stdin option allows the output from the echo command to be seen as the input for textutil instead of reading from a file. The -convert option will make textutil convert the input to rtf in this case and using the -font and -fontsize options and their respective name and size.
Note the space in the font name given is escaped. The -output is the path and name of the output file.

Look at the man page for textutil.

I am not sure if you can append using textutil

Wow. I never knew about textutil, that’s a handy program to have in your arsenal!

I learn something new every day, if I am paying attention…

Haha thats a script i wrote a while ago! (thanks for crediting me :frowning: )
Anyhoo that doesn’t actually save the document, just opens text edit and makes a mock animation.

Very cool. I will check some of these out. Thank you all for your responses.

Cheers,

A.