C-Command Software Forum

Using Applescript to copy RTF text

I am trying to write an Applescript to capture text to a file. By saving the file to a folder with a folder action to add to an EF library, I can save the text to a file with my choice of name, the EF library I want it to be added to, and have a copy of the file to take home to add to EF libraries on my other computer. I have a script that works, but it saves the text as pure ASCII. What is the trick to getting RTF text? My script:

set theText to the clipboard
set theTitle to the first paragraph of theText
set AppleScript's text item delimiters to " "
set wordCount to number of text items in theText
if wordCount > 5 then set wordCount to 5
set theTitle to text items 1 thru wordCount of theText as string
set theTitle to (change "," into "" in theTitle)
set theTitle to (change "'" into "" in theTitle)
set theTitle to (change "." into "" in theTitle)
set theTitle to (change "?" into "" in theTitle)
display dialog "Enter a name for file:" default answer theTitle
set theTitle to the text returned of the result
set thePath to "Documents:EF Miscellaneous Folder Action"
set theUserFolder to (path to home folder as text)
set thePath to theUserFolder & thePath & ":" & theTitle & ".rtfd"
set NewFile to open for access file thePath with write permission
write theText to NewFile
close access NewFile

The way to get RTF is to write something like:

set theRTF to the clipboard as «class RTF »

You can then write that to a filename that ends with “.rtf” and it will work. However you will not be able to manipulate the RTF as a string within AppleScript. There is also «class rtfd» but this is a stream of data that represents all the files in the “.rtfd” package, and I don’t know how to write that to disk using AppleScript. (In Cocoa you’d use NSFileWrapper.)