Chapter 35. Scripting TextEditMac 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 TextEditset 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 'fil' *) end tell tell application "TextEdit" activate -- make TextEdit the frontmost app open {fil as alias} -- 'open' command takes a 'list of aliases' parameter set text of document 1 to "First sentence of this new document." (* write a line to the file *) end tell This script first gets a folder (for storing the new file) and a filename from the script user, using the choose folder and display dialog scripting additions. Figure 35-1. TextEdit and its Format:Font menu
The script then makes a new file using the Finder's make command with the user's chosen filename (for the sake of brevity I have left out the usually required checks for the cancellation of these dialogs or for the possibility that the user did not enter any text for the filename). Then TextEdit is made the frontmost or active application (i.e., activate), and it opens the new file. The TextEdit open command takes as its parameter a list of aliases. The code fragment open {fil as alias} first coerces the file object to an alias and then stores this alias in a single-item list that is passed to the open command. Just leaving the fil variable as a document file object (which is what the Desktop's make new file command returns), as in open {fil }, will generate an error in TextEdit. This happens because its open command takes a list of aliases as its parameter, not a list of document file objects. The TextEdit app has document elements (see the TextEdit classes section in this chapter). The following code would return a list of all open TextEdit documents (i.e., TextEdit windows visible in the Finder or on the Dock): tell app "TextEdit" to get documents The final line of Example 35-1 sets the text property of the first TextEdit document (document 1) to a string: "First sentence of this new document." In TextEdit, document 1 is the front window that would appear on the desktop if you activated TextEdit by choosing the program in the Dock or by clicking on one of its windows. |