Name

TextEdit

Dictionary classes

attribute run

A subdivision of a block of text, an attribute run is a group of characters that all have the same attributes, such as font or size. An attribute run is just a different way of abstracting or grouping parts of a text block. For example, if the first paragraph of a document’s text has some characters that are 12 points in size and others that are 18 points, then getting the attribute runs of that paragraph would return two separate chunks of text in a list (one group would be 12 points in size and the other would be 18 points). However, getting paragraph 1 of that text would return one chunk of characters of different sizes. In other words, the paragraph would contain the two attribute runs. The following example gets every attribute run of a document’s text (a list containing three attribute runs). The first line of the text contains the characters “hi here is some more text k,” but the last “k” character is in a different font and size than the sentence’s other characters. Consequently, the “k” and its following carriage return character is considered a separate attribute run then its preceding characters. The return value of the code every attribute run of text of document 1 is at the bottom of the script displayed within comment characters:

tell application "TextEdit"
   activate
   every attribute run of text of document 1
   (* returns a list of three attribute runs:
   {"hi here is more text ", "k
   ", "
   Meeting notes:
   Wednesday, October 11, 2000 12:58:16 PM"}
   *)
end tell

The following are attribute run elements:

The following are attribute run properties:

character

A character object is what you would expect it to be, a single character inside of a word or string. The following example returns the first word of a document as a list of character objects. If you instead used the following code then the return value would be a string like “F”:

get character 1 of word 1 of text of document 1
tell app "TextEdit"
   get characters of word 1 of text of document 1
end tell
(* Example return value:
{"F", "i", "r", "s", "t"}  *)

The following are character elements:

The following are character properties:

document

A document object represents an open TextEdit document, as depicted in Figure 35-1. You can get a reference to one or more documents by grabbing the TextEdit application’s document elements, as in tell app "TextEdit" to get documents. This code returns a list that looks like:

{document 1 of application "TextEdit", document 2 of application "TextEdit"}

The following example gets the various properties of a document. You can view the values of these properties using the Event Log of Script Editor. This example shows some Event Log output at the bottom of the script:

tell application "TextEdit"
   set doc to document 1 (* the front document is stored in doc variable *)
   (* a document's properties revealed *)
   doc's path -- the Unix path
   doc's modified
   doc's name
   set txt to text of doc (* returns the content of the document 
if any (if the document is empty, returns an empty string "") *)
   set parcount to (count of txt's paragraphs)
   set wdcount to (count of txt's words)
   (* Event Log output:
   get document 1
   --> document 1
   get path of document 1
   --> "/Users/bruceper/Documents/newfile.rtf"
   get modified of document 1
   --> 0
   get name of document 1
   --> "newfile.rtf"
   get every text of document 1
   --> "Hi, I'm pleased to be the first paragraph of this document. 
My font is  \
   Verdana."
   *)
end tell

The following are document elements:

text

The text of a TextEdit document can be seized with code such as:

tell app "TextEdit" to get text of document 1

You can also write to a document, without using the open for access, write, or close access scripting additions, by using code such as:

set the text of document 1 to "My chunk of text"

This code shows how to append text, such as a date string, to an existing TextEdit document:

tell app "TextEdit"
   set cr to ASCII character 13 (* use as a return or new line character *)
   set tmessage to cr & "Meeting notes:" & cr &¬
   ((current date) as string)
   set docs to documents -- docs contains a list of open TextEdit documents
   repeat with d in docs
      if ((name of d) contains "memo log") then (* only add text to "memo log"
file *)
         set text of d to (text of d) & tmessage (* append the text stored
in var tmessage to end of file *)
         (* the path looks like "/users/oneuser/library/desktop/myfile.rtf" *)
         set pth to path of d
   exit repeat
      end if
   end repeat
   display dialog "the memo file is at: " & pth
end tell

The following are document properties:

path string

This property returns a string that looks like "/users/oneuser/desktop/myfile.rtf." This Unix-style pathname identifies where the document is stored on the computer. The back-slash (“/”) character that begins path says “begin at the startup disk or root.” The standard disk, file, and folder delimiter for AppleScript, the colon (“:”), is still used by many AppleScript commands (such as choose folder) to represent where the file is stored. If the TextEdit document has not yet been saved, then its path property returns nothing in OS X, not even an empty string (“”). You can set the path property of a document (this will not raise an error in my testing), then use TextEdit’s save command to save the file to the new path.

modified integer (read-only)

This property returns 1 if the document has been modified since it was last saved or if the document has not been modified. The following example finds out if a document has been saved, then saves the document (using the save command) if the document has unsaved changes:

tell application "TextEdit"
   activate
   (* if the document has been changed since it was last saved its 'modified' 
property will return 1 *)
   if (modified of document 1) > 0 then
      save document 1
      close document 1
   else
      close document 1
   end if
end tell

name string

name returns a string that is the name of the document file. If you have just created the document in TextEdit but have not yet saved it, the name property returns nothing (not even a string such as “untitled”). Trying to find out whether the document has a valid name (such as by accessing the length of name to see if the name has more than zero characters) raises an error at least in Mac OS X. You might try this document name test in future releases, or use a try block to catch and examine the error. Chapter 7 , describes error trapping with the try statement.

class integer (read-only)

Accessing the class property for the document object raises an error in Mac OS X.

paragraph

A paragraph object is a chunk of text that is terminated by a new line or paragraph character. You can set the paragraphs of a document’s text with code such as:

tell app "TextEdit" to set paragraph 3 of text of document 1 to¬
 "new paragraph"

If you try to get paragraphs of text of document 1, for example, and the document does not contain any content, then a script error is raised. An easy way to find out whether a TextEdit document contains any text yet is to check the length of the number of words in the document, as in the following example:

tell app "TextEdit"
   activate
   set l to (text of document 1)
   if (length of 1) > 0 then
      set notEmpty to true
   end if
end tell

The following are paragraph elements:

The following are paragraph properties:

text

text represents the body or content of a document. The whole chunk of content will be returned as a string from code such as:

tell app "TextEdit" to get text of document 1

If the document does not yet have any content, then its text element returns an empty string (“”). Once you have the text in memory, you can get or set the values of its characters, words, or paragraphs. The following example finds out whether the existing content of a document contains the word Copyright; if it does not, then Copyright 2001 is appended to the end of the document:

tell application "TextEdit "
   activate
   set cr to ASCII character 13
   set txt to text of document 1
   set wd to (words of txt)
   set len to length of wd
   if (len > 0) then
      if wd does not contain "Copyright" then
         set (text of document 1) to txt & cr & "Copyright 2001"
      else
         display dialog "copyright included"
      end if
   end if
end tell

The following are text elements:

The following are text properties:

word

A word (e.g., “sentence”) is a series of characters unbroken by a space character. A word contains a character object. The syntax words of text of document 1, for example, will return a (potentially long) list of words. The space characters separating the words will be left out of the list. This syntax makes it very easy to search a document’s words for a specific word, as in

Set found to ((text of document 1) contains "Copyright")

The following is a word element:

The following are word properties:

application

This class represents the TextEdit application itself. In TextEdit’s dictionary, you can find the TextEdit application object described under the TextEdit Suite (Chapter 1 describes AppleScript dictionaries). The application has four properties or attributes (i.e., the value of its name property is “TextEdit”). You can get references to all of TextEdit’s documents with code such as:

tell app "TextEdit" to get documents

The following example queries the property values of the copy of TextEdit running on the machine where the script executes. You can view the output (the property values) in Script Editor’s Event Log window. Display this window by typing Command-E when Script Editor is the frontmost application, then make sure that the checkboxes labeled “Show Events” and “Show Event Results” are checked.

tell application "TextEdit"
   frontmost
   name
   version
 (* Example Event Log output:
   tell application "TextEdit"
      get frontmost
      --> 0
      get name
      --> "TextEdit"
      get version
      --> 0
   end tell
   *)
end tell

The following are application elements:

document

TextEdit can have zero or more open documents. Each one of these documents is considered a document object with its own properties or attributes. Each open document is indexed from front to back in the manner of document 1 (the frontmost document if you make TextEdit the active application), document 2, and so on. For example, to count the open documents use:

tell app "TextEdit to count documents

To close an open document, use:

tell TextEdit to close document 1

(or whatever its index is). See the document class description for a demonstration of how to get a document’s properties.

window

A TextEdit window is a desktop window that is showing a TextEdit document. You can get references to all the names of the open TextEdit windows by using the code:

tell app "TextEdit" to get name of windows

This code returns a list that might look like:

{"newfile.rtf", "newfile 2.rtf"}

The latter list contains the name of one window (i.e., the filename of the document contained by the window) as a string.

The following are application properties:

color

color objects have just one property: class. Most of the other TextEdit classes, such as character, word, paragraph, and text, have color properties that can be queried using AppleScript. They return data value types, as in <<data RGB FFFF433951F7>>.

The following is a color property: