create rtf file, open in textedit

i am very new to applescript, if anyone would have mercy, please respond.
i am trying to create a script that will create a rich text file named by the date (in format of YY.MM.DD for purposes of sorting alphabetically) and open this file for edit in textedit. Preferably, I would like to be able to create a file named by date followed by a short description (for example, activate script - it asks for a summary and then creates a file named by date followed by a summary (for example "07.12.28 - this is an example") and opens that file in textedit.app for editing).
i worked a lot with iptscrae but it's been a long time...and was nothing like this.

edit: please note, i've had trouble with spaces in scripts when it deals with file names (date - summary)

Message was edited by: eightyfiveonions

Message was edited by: eightyfiveonions

MacBook Pro 15" Core Duo, Mac OS X (10.5)

Posted on Dec 28, 2007 8:58 PM

Reply
9 replies

Dec 28, 2007 11:58 PM in response to eightyfiveonions

TextEdit won't make a new document with a title (at least in 10.4.11 anyway), you would have to save the untitled document as something or open an existing document. Since you are wanting a dialog to set a name with a date in it, I went for the latter approach. The following script makes a blank RTF document (well, there is some formatting stuff in there) and opens it in TextEdit:

<pre title="this text can be pasted into the Script Editor" style="font-family: Monaco, 'Courier New', Courier, monospace; font-size: 10px; padding: 5px; width: 720px; color: #000000; background-color: #E0E0E0; overflow: auto">-- get the current date text
tell (current date) to get ((its year) * 10000 + (its month) * 100 + (its day)) as text -- yyyyMMdd
tell the result to set TheDate to text 3 thru 4 & "." & text 5 thru 6 & "." & text 7 thru 8 -- yy.mm.dd

set TheFolder to (path to the desktop) -- or set TheFolder to (choose folder)
set TheName to text returned of (display dialog "Enter the name for the new document:" with title "New RTF document" default answer TheDate & " - " buttons {"Cancel", "OK"} default button "OK")

try -- create a new empty RTF document
set TheText to "{\\rtf1\\mac\\ansicpg10000\\cocoartf824\\cocoasubrtf440
{\\fonttbl}
{\\colortbl;\\red255\\green255\\blue255;}
\\margl1440\\margr1440\\vieww9600\\viewh8400\\viewkind0
}"
tell application "System Events" to get exists of file ((TheFolder & TheName & ".rtf") as text)
if not (the result) then
set TheOpenFile to open for access ((TheFolder & TheName & ".rtf") as text) with write permission
write TheText to TheOpenFile as «class utf8» starting at eof
close access TheOpenFile
else
display alert "The file \"" & ((TheFolder & TheName & ".rtf") as text) & "\" already exists" as critical
error
end if
on error
try
close access TheOpenFile
end try
return
end try

tell application "TextEdit" -- open the file and set the contents to the current date
activate
open file ((TheFolder & TheName & ".rtf") as text) as alias
set the text of document 1 to ((current date) as text) & return
set modified of document 1 to true
end tell
</pre>

Dec 29, 2007 6:42 AM in response to eightyfiveonions

With Tiger 10.4.11, although that property is shown to be read only, setting it just has TextEdit prompt you to save the document if you try to close it (which makes some sense, since the date was added to the text after opening it). The script seemed more complete (to me) that way, but you don't need that statement (especially if it doesn't work). Actually, I was wondering more about Leopard's use of Unicode text, glad it works for you.

Dec 29, 2007 6:54 AM in response to red_menace

I removed that and it runs without hiccup. I've also changed it to create plain text and leave off the .rtf extension. With my knowledge, I doubt it's clean, but it's working. Would there be a simple way to hide the rtf extension? If not, plain text is fine with me.
Thanks again!
Joe
edit: in leopard it shows as modified automatically, that was the problem

Message was edited by: eightyfiveonions

Dec 29, 2007 8:17 AM in response to eightyfiveonions

You can use the Finder to set the "extension hidden" property of the file:

<pre title="this text can be pasted into the Script Editor" style="font-family: Monaco, 'Courier New', Courier, monospace; font-size: 10px; padding: 5px; width: 720px; color: #000000; background-color: #E0E0E0; overflow: auto">-- get the current date text
tell (current date) to get ((its year) * 10000 + (its month) * 100 + (its day)) as text -- yyyyMMdd
tell the result to set TheDate to text 3 thru 4 & "." & text 5 thru 6 & "." & text 7 thru 8 -- yy.mm.dd

set TheFolder to (path to the desktop) -- or set TheFolder to (choose folder)
set TheName to text returned of (display dialog "Enter the name for the new document:" with title "New text document" default answer TheDate & " - " buttons {"Cancel", "OK"} default button "OK")
set TheFile to (TheFolder & TheName & ".txt") as text
set TheText to ((current date) & return) as text

try -- create a new text document with the current date
tell application "System Events" to get exists of file TheFile
if not (the result) then
set TheOpenFile to open for access TheFile with write permission
write TheText to TheOpenFile
close access TheOpenFile
else
display alert "The file \"" & TheFile & "\" already exists" as critical
error
end if
on error
try
close access TheOpenFile
end try
return
end try

tell application "Finder" to set extension hidden of (TheFile as alias) to true

tell application "TextEdit" -- open the file
activate
open file TheFile
end tell
</pre>

Dec 29, 2007 7:59 PM in response to red_menace

This is your script after I altered it a bit (and as you'll see it's for the purposes of journaling)
I'm not sure exactly why I moved the current date and return instructions back to the bottom here but that's what I had to do to get it to work. I'm a trial and error learner more than anything else, I'm going to read up on this a bit but it's working like a charm.
I guess it was because I set TheText variable back up for the RTF formatting and couldn't figure out how to incorporate the date there.
edit: the formatting on this post is a bit messed up.. your rtf formatting stuff is moved from location in first script to accommodate the second script but nothing else is different about it. shrugs

<pre title="this text can be pasted into the Script Editor" style="font-family: Monaco, 'Courier New', Courier, monospace; font-size: 10px; padding: 5px; width: 720px; color: #000000; background-color: #E0E0E0; overflow: auto">-- get the current date text
tell (current date) to get ((its year) * 10000 + (its month) * 100 + (its day)) as text -- yyyyMMdd
tell the result to set TheDate to text 3 thru 4 & "." & text 5 thru 6 & "." & text 7 thru 8 -- yy.mm.dd

set TheFolder to "OSX:Users:joseph:Documents:Journal:" -- or set TheFolder to (choose folder)
set TheName to text returned of (display dialog "Enter the name for the new document:" with title "New text document" default answer TheDate & " - " buttons {"Cancel", "OK"} default button "OK")
set TheFile to (TheFolder & TheName & ".rtf") as text
set TheText to "{\\rtf1\\mac\\ansicpg10000\\cocoartf824\\cocoasubrtf440
{\\fonttbl}
{\\colortbl;\\red255\\green255\\blue255;}
\\margl1440\\margr1440\\vieww9600\\viewh8400\\viewkind0
}"

try -- create a new text document
tell application "System Events" to get exists of file TheFile
if not (the result) then
set TheOpenFile to open for access TheFile with write permission
write TheText to TheOpenFile as «class utf8» starting at eof
close access TheOpenFile
else
display alert "The file \"" & TheFile & "\" already exists" as critical
error
end if
on error
try
close access TheOpenFile
end try
return
end try

tell application "Finder" to set extension hidden of (TheFile as alias) to true

tell application "TextEdit" -- open the file and print date
activate
open file TheFile
set the text of document 1 to ((current date) as text) & return
end tell</pre>

Unless u see any errors with what I've done, I'm going to run with it.

Message was edited by: eightyfiveonions

Dec 29, 2007 8:49 PM in response to eightyfiveonions

The RTF file uses formatted text, so instead of messing around with it too much I just copied the formatting from an empty document to make a template, then added the current date to the file after opening it. A plain text file doesn't have any formatting, so I just wrote the current date directly to the new file. Whatever works. The forum formatting does get a little bit goofy sometimes, it seems to be set up more for regular text and not so much for program or script code - I usually use this script to format my code for these forums.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

create rtf file, open in textedit

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.