Open new TextEdit document

I wrote the following script to generate call reports. It launches TextEdit, adds a header and formats the text. The problem I’m having is that I get an error if TextEdit is already running. If it’s not running, it opens a doc on launch. How do I get it to open a new doc if it’s already running? Here’s what I have so far:

– Get timestamp, format it to the clipboard
set time_stamp to do shell script “date +%Y-%m-%d\ %H:%M:%S”
set date_header to "Date: " & time_stamp & return & "From: " & return & "Re: "
set the clipboard to the date_header

– Open TextEdit
tell application “TextEdit”
– build header
set paragraph 1 of document 1 to date_header & return
set paragraph 2 of document 1 to "From: " & return
set paragraph 3 of document 1 to "Re: " & (return & return)
– format text
set plain_font to font of paragraph 1 of document 1
set i to 1
repeat 3 times
set font of paragraph i of document 1 to plain_font & “-Bold”
set i to i + 1
end repeat
end tell

BTW – I am obviously a newbie…

I think this will do what you want:

As to your question, test to see if document 1 exists, if it doesn’t, make a new document at the beginning [of documents].


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]


Also, you can use the ‘launch’ command to bypass the initial window:

tell application “TextEdit”
make new document at front
end tell


Thanks Jon for cleaning up my script. I’ve just started learning Applescript and have learned more from having my stuff ripped apart here than from struggling thru the product documentation.

Kel - I like your launch suggestion as it doesn’t overwrite a document that might already be open. Muchas gracias amigo