Chapter 35. Scripting TextEdit

Mac OS X installs a handy and scriptable word processor called TextEdit. You can find it in the /Applications directory or by typing Option-Command-A when the Finder is active and double-clicking TextEdit from the resulting Finder window. TextEdit is not as feature-laden and bloated as Microsoft Word, nor as limited in functionality as SimpleText. It is useful for creating simple text documents where you want to control the font and color of the text, but more complex publishing tasks than this probably are not appropriate TextEdit jobs. Figure 35-1 shows TextEdit on the Mac OS X desktop.

It is likely that the TextEdit’s available AppleScript commands will change with new Mac OS X releases, so this chapter will focus on TextEdit’s major commands (e.g., count, open, save) and text-related classes. The TextEdit scriptable task that immediately comes to mind is creating a new file, opening it in TextEdit, and then creating the file’s contents. Example 35-1 creates a new file using the Finder app then has TextEdit open the file and place some text into it.

Example 35-1. Opening a New File in TextEdit
set fol to (choose folder) (* use the 'choose folder' osax to ask the user to choose a folder; this osax returns an alias type *) set nm to the text returned of (display dialog "Choose a file name:"¬ tell application "Finder" -- The Finder is better at making files set fil to (make new file at fol with properties {name:nm})(* store the new file in variable ...

Get AppleScript in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.