saving data with TextEdit

1.3k views Asked by At

I want to use TextEdit to save data. what I have so far

tell application "TextEdit"
open /Users/UserName/Desktop/save.rtf
end tell

This gives me

"Expected “given”, “in”, “of”, expression, “with”, “without”, other parameter name, etc. but found unknown token."

and highlights the . in .rtf I tried removing the .rtf

but when I compile it it turns into

(open) / Users / username / desktop / (save)

This code gives "The variable Users is not defined." also if possible can I have TextEdit run in the background without opening a window?

1

There are 1 answers

1
Lri On BEST ANSWER

Put quotes around the path and use POSIX file to get a file object for the path:

tell application "TextEdit"
    open POSIX file "/Users/UserName/Desktop/save.rtf"
end tell

You can modify the text of a document by changing the text property:

tell application "TextEdit"
    set text of document 1 to text of document 1 & "aa"
end tell

It removes all styles in rich text documents. It also inserts the text as 12-point Helvetica in plain text documents, regardless of the default font.

Creating a new rtf file:

tell application "TextEdit"
    make new document at beginning with properties {text:"aa"}
    close document 1 saving in POSIX file "/tmp/a.rtf"
end tell
printf %s\\n aa | textutil -inputencoding UTF-8 -convert rtf -stdin -output a.rtf