I want to be able to create a new TextEdit document with an AppleScript file. The AppleScript file will then be triggered by a system-wide keyboard shortcut via FastScripts. Specifically, I want the AppleScript to:
- Prompt the user to enter a filename.
- Create a blank TextEdit file with that filename and save it to a predetermined location. I would like the file to be a rich text document (which is the default type of document created in TextEdit when one clicks "File" → "New").
- Set the preset font size of that file to be 18.
- Open the file in TextEdit, with window bounds of {160, 10, 883, 639}. (Note that TextEdit will most likely not already be open when the AppleScript file is run.)
- Ensure that the blinking cursor is on the second line of the document, so that the user can immediately begin typing. (I hate typing on the very first line in TextEdit, from a visual standpoint, and have to first hit enter every time that I create a new file before I can begin typing.)
Here is what I have:
-- Getting file name from user:
repeat
set customFilename to the text returned of (display dialog "Save as:" with title "Do you want to create a new, blank TextEdit RTF document?" default answer "")
-- Ensuring that the user provides a name:
if customFilename is "" then
beep
display alert "The filename cannot be empty." message "Please enter a name to continue."
else
exit repeat
end if
end repeat
-- Creating the desired file path:
set filePath to "/Users/Me/Desktop/"
set fullFilepath to filePath & customFilename & ".rtf"
-- Checking if the file already exists:
tell application "Finder"
set formattedFullFilepath to POSIX path of fullFilepath
if exists formattedFullFilepath as POSIX file then
tell current application
display dialog "The file \"" & formattedFullFilepath & "\" already exists!" & "
" & "Do you want to overwrite the file?" buttons {"No", "Yes"} default button 1 with title "File Already Exists..." with icon caution
if the button returned of the result is "No" then
return
end if
end tell
end if
end tell
That's where I am, at this point.
I'm not sure how exactly to approach the rest. I could create a new TextEdit document within TextEdit itself, and then save the file with the custom filename. Or I could create the RTF file outside of TextEdit, and then open the file with TextEdit.
Similarly, the AppleScript can either insert the blank line by keystroke after the file is opened, or the AppleScript can actually write the line to the file when the file is created.
And I have no idea how to set the preset font size for a specific document in AppleScript (without changing the application-wide default font size), or if it is even possible.
Best Answer
Having read your question, here is an example of how I would code it.
How to create the Custom RTF Document Template,
customRTFDocumentTemplate
, used in theon createCustomRTFDocument()
handler:In TextEdit create a new Rich Text document and set the font and size as desired, then add any default content, in this case a newline. With this done, press ⌘A to select all, then ⌘C to copy this information to the Clipboard.
Now in Script Editor use the following command to get the RTF class data from the Clipboard.
What is returned will be used as the Custom RTF Document Template by assigning it to a variable in the actual script, e.g.:
Note that the above template has the default font, Helvetica, with size set to 18 and one blank line as its content. If you want a different font then Helvetica, set both the font and size before adding content, a blank line in this case, then use the info above to get is as RTF class data from the Clipboard.
Also note that in the
on openDocument()
handler, thebounds
list is set to{160, 22, 883, 639}
not{160, 10, 883, 639}
as the second item in thebounds
list should not be less then the height of the menu bar.