< Previous PageNext Page > Hide TOC

Text View Suite

This chapter describes the terminology in AppleScript Studio’s Text View suite.

The Text View suite defines two classes for displaying and manipulating text. The text class inherits from the view class and text view inherits from text. Starting in AppleScript Studio version 1.3, these two classes were merged.

Terminology

The classes in the Text View suite are described in the following section:

For enumerated constants, see “Enumerations.”

Classes

The Text View suite contains the following classes:

text
Plural: text
Inherits from: view
Cocoa Class: NSText

Not available starting in AppleScript Studio version 1.3.

!

Warning:  Starting in AppleScript Studio version 1.3, the text class is collapsed into the text view class. Properties you could previously access through the text class can now be accessed through the text view class. See the Version Notes section of the text view class for a list of merged properties.

text view
Plural: text views
Inherits from: view
Cocoa Class: NSTextView

Provides many features for displaying and manipulating text laid out in a defined area.

Important:  Starting in AppleScript Studio version 1.3, the text class is collapsed into the text view class. Properties you could previously access through the text class can now be accessed through the text view class. See the Version Notes section for a list of merged properties.

Figure 11-1 shows a window that contains a text view.


Figure 11-1  A text view that contains some text

A text view that contains some text

You will find the text view object on the Cocoa-Text pane of Interface Builder’s Palette window. You can set many attributes for text views in Interface Builder’s Info window.

For an overview of Cocoa’s text-handling system, see the document Text System Overview.

Properties of text view objects

In addition to the properties it inherits from the view class, a text view object has these properties:

alignment
 Access:read/write
 Class:enumerated constant from Text Alignment
 the alignment of the text
allows undo
 Access:read/write
 Class:boolean
 Should the text view allow undo?
background color
 Access:read/write
 Class:RGB color
 the background color of the view; a three-item integer list that contains the values for each component of the color; for example, green can be represented as {0,65535,0}
content
 Access:read/write
 Class:Unicode text
 the contents of the view; nearly synonymous with contents; for related information, see the Version Notes section for this class
contents
 Access:read/write
 Class:Unicode text
 the contents of the view; nearly synonymous with content; for related information, see the Version Notes section for this class
draws background
 Access:read/write
 Class:boolean
 Should the view draw its background?
editable
 Access:read/write
 Class:boolean
 Is the view editable?
field editor
 Access:read/write
 Class:boolean
 Is this a field editor? a field editor is used by simple text-bearing objects; for example, a text field object uses its window's field editor to display and manipulate text; the field editor can be shared by any number of objects and so its state may be constantly changing; for additional information, see the description for the fieldEditor:forObject: method in the NSWindow class
font
 Access:read/write
 Class:font
 the font for the view
horizontally resizable
 Access:read/write
 Class:boolean
 Is the view horizontally resizable
imports graphics
 Access:read/write
 Class:boolean
 Should the view import graphics?
maximum size
 Access:read/write
 Class:point
 the maximum size of the view; the size is returned as a two-item list of numbers {horizontal, vertical}; see the bounds property of the window class for information on the coordinate system; for changes introduced in AppleScript Studio version 1.4, see the main discussion for the application class, as well as the coordinate system property of that class
minimum size
 Access:read/write
 Class:point
 the minimum size of the view; the size is returned as a two-item list of numbers {horizontal, vertical}; see the bounds property of the window class for information on the coordinate system; for changes introduced in AppleScript Studio version 1.4, see the main discussion for the application class, as well as the coordinate system property of that class
rich text
 Access:read/write
 Class:boolean
 Does the text support Rich Text Format? default is true
ruler visible
 Access:read/write
 Class:boolean
 Is the ruler visible?
selectable
 Access:read/write
 Class:boolean
 Is the view selectable?
smart insert delete enabled
 Access:read/write
 Class:boolean
 Is the smart insertion and deletion of text enabled? default is true; controls whether the view inserts or deletes space around inserted and deleted words so as to preserve proper spacing and punctuation
spell checking enabled
 Access:read/write
 Class:boolean
 Is spell checking enabled for the text view?
text color
 Access:read/write
 Class:RGB color
 the color of the text; a three-item integer list that contains the values for each component of the color; for example, green can be represented as {0,65535,0}
text container inset
 Access:read/write
 Class:point
 the amount of the empty space the view leaves around its associated text container; the text container represents the region where text is laid out; the inset is returned as a two-item list of numbers {width, height}; default is {0,0}
text container origin
 Access:read only
 Class:point
 the origin of the text container within the text view, which is calculated from the view’s bounds rectangle, container inset, and the container's used rect; the text container represents the region where text is laid out; the origin is returned as a two-item list of numbers {left, bottom}; see the bounds property of the window class for information on the coordinate system; for changes introduced in AppleScript Studio version 1.4, see the main discussion for the application class, as well as the coordinate system property of that class
uses font panel
 Access:read/write
 Class:boolean
 Can the view use the font panel?
uses ruler
 Access:read/write
 Class:boolean
 Should the text view use rulers?
vertically resizable
 Access:read/write
 Class:boolean
 Is the view vertically resizable?


Elements of text view objects

In addition to the elements it inherits from the view class, a text view object can contain the elements listed below. Your script can access most elements with any of the key forms described in “Standard Key Forms.”

text (see “Terminology Supplied by the Cocoa Application Framework”)
 Specify by: “Standard Key Forms”
 

the view’s text


Events supported by text view objects

A text view object supports handlers that can respond to the following events:

Drag and Drop
conclude drop
drag
drag entered
drag exited
drag updated
drop
prepare drop

Editing
begin editing
changed
end editing
should begin editing
should end editing

Key
keyboard up

Mouse
mouse entered
mouse exited
scroll wheel

Nib
awake from nib

View
bounds changed

Examples

The following statement, from the Open Panel sample application (first distributed with AppleScript Studio version 1.1), shows how to set the text of a text view. The statement actually clears the text view by setting its text to an empty string.

set contents of text view "path names" of scroll view  "path names" of window "main"  to ""

You can use the following script in Script Editor to set the text color in a text view to green. Similar script statements will work within an AppleScript Studio application script (though you won’t need the tell application block).

tell application "myTextViewApp"
    tell text view "text" of scroll view "scroller"  of window "main"
        set text color to {0, 65535, 0}
    end tell
end tell

Text-handling terminology can be a bit confusing. AppleScript Studio’s Text View suite defines the text view class, which (prior to AppleScript Studio version 1.3) inherited from the text class—starting in version 1.3, the properties of the text class were merged into text view class.

In addition, Cocoa defines the Text suite, which defines classes such as character, paragraph, text, and word, which in turn have elements such as character, paragraph, and word, and properties such as color, font, and size. The Text suite is a default suite that is available to all Cocoa applications that support scripting.

To further complicate matters, AppleScript classes such as string and Unicode text have character, paragraph, text, and word elements. In addition, if a class and a property (such as text) have the same name, use of the name within a tell statement defaults to the class.

The following script shows various operations you can perform on the text from a text view in a window. A text view is automatically enclosed in a scroll view, so the script accesses the text view through the scroll view. This script was tested with AppleScript Studio version 1.2 (and again with version 1.3) in Script Editor, but you can use similar statements in an application script (where you won’t need the tell application block). This script may include statements that do not run with earlier versions of AppleScript Studio.

For this example, the text in the text view was “This is the only sentence.” Note that you will get evaluation errors for some lines in this script if there is no text in the text view, or, for example, less than 7 characters (causing the statement character 7 of text of text view 1 to generate an error).

tell application "simple"
    tell window 1
        tell scroll view 1
            class of text of text view 1 -- text
            word 1 of text of text view 1 -- result: "This"
            set myTextObject to text of text view 1
                -- result: "This is the only sentence."
            class of myTextObject
                -- result: Unicode text
                -- In Studio version 1.1, the result is string.
            -- Next line generates error because Unicode text doesn’t
            --  have a color property
            --color of myTextObject
                -- result: Can't get color of "This is the  only sentence."
            word 1 of myTextObject -- result: "This"
            character 7 of myTextObject -- result: "s"
            character 1 of word 1 of myTextObject -- result: "T"
            set myText to contents of text view 1
                -- result: "This is the only sentence."
            class of myText -- result: Unicode text
            word 3 of myText -- result: "the"
            character 13 of myText -- result:"o"
            editable of text view 1 -- result: true (inherited from  text)
            background color of text view 1 -- result: {65535, 65535,  65535}
            set myTextRef to a reference to (text of text view 1)
                -- result: every text of text view 1 of scroll view  1
                --          of window 1 of application "simple"
            class of myTextRef -- result: text
            color of myTextRef -- result: {0, 0, 0}
            --font of myTextRef -- NSCannotCreateScriptCommandError
            contents of myTextRef
                -- result: "This is the only sentence."
            size of myTextRef -- result: 12.0 (depends on font)
            word 1 of myTextRef -- result: "This"
            color of word 1 of myTextRef -- result: {0, 0, 0}
            set color of word 1 of myTextRef to {65535, 0, 0}
                -- result: color of first worth in view ("This")  is red
        end tell
    end tell
end tell

See the Examples section for the scroll command for information about how to scroll text in a text view.

See the Examples section for the view class for information about how to rotate text in a text view.

Version Notes

Starting with AppleScript Studio version 1.3, the text class has been collapsed into the text view class. The following properties, which you could previously access through the text class, can now be accessed through the text view class:

Starting with AppleScript Studio version 1.2, a script can say word 1 of text view 1 instead of word 1 of text of text view 1 (the default of text is assumed), though the longer version still works.

Support for drag-and-drop commands was added in AppleScript Studio version 1.2. See the “Terminology” for details. In particular, the description for the conclude drop event handler provides information on supporting drag and drop for text view and text field objects.

The content property was added in AppleScript Studio version 1.2. You can use content and contents interchangeably, with one exception. (These properties came over from the text class to the text view class in AppleScript Studio version 1.3.) Within an event handler, contents of theObject returns a reference to an object, rather than the actual contents. To get the actual contents of an object (such as the text contents from a text field) within an event handler, you can either use contents of contents of theObject or content of theObject.

For a sample script that shows the difference between content and contents, see the Version Notes section for the control class.

See the Examples section for the scroll command for information about how to scroll text in a text view.



< Previous PageNext Page > Hide TOC


© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-10-31)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.