XText Reference

XText is a wrapper object of AppleScript's text object. XText provides object oriented interface to manipulate text and some advanced features.

XText is a wrapper object of AppleScript's text object to provide object oriented interface to manipulate text. You can describe complex text handlings with simple statements.

The parent of instances of XText is AppleScript's text object. Therefore ...Properties of AppleScript's text can be obtained form a XText instance in same way to normal AppleScript's text object (string and Unicode text).e.g., length of a_text, word 1 of a_textAn instance of XText is immutable like string or Unicode class object.

There are same purpose methods in the both of class methods and instance methods (e.g., repace, split and so on). But there are folloing differeces.

Sample

Using Class Methods

use XText : script "XText"

tell XText
store_delimiters() -- storing AppleScript's text item delimiters

log (replace for "this is a pen" from "pen" by "flower")
-- result : "this is a flower"

set a_list to split("this is a pen", space)
log a_list -- result : {"this", "is", "a", "pen"}
log join_list(a_list, "-") -- result : "this-is-a-pen"

log strip(" this is a pen ") -- ressult : "this is a pen"
log strip_beginning(" this is a pen ")
-- result : {" ", "this is a pen "}
log strip_endding(" this is a pen ")
-- result : {" "," this is a pen"}

log formatted_text("$1 is $2.", {"XText", "useful"})
-- result : "XText is useful."

restore_delimiters() -- restoring AppleScript's text item delimiters
end tell

Using Instance Methods

use XText : script "XText"

(* Make a XText Instance *)
set a_text to XText's make_with("this is a pen")

(*Replacing *)
log a_text's replace("pen", "flower")
-- result : [XText] this is a flower

(* Appending and Prepennding *)
set spaced_text to a_text's prepend(return & tab)
set spaced_text to spaced_text's push(space)

log spaced_text
(*[XText]
this is a pen *)

(* Stripping *)
log spaced_text's strip() -- result : [XText] this is a pen

set a_result to spaced_text's strip_beginning()
log item 1 of a_result
(*
*)
log item 2 of a_result
-- result : [XText] this is a pen

set a_result to spaced_text's strip_endding()
log item 1 of a_result
(* *)
log item 2 of a_result
(*[XText]
this is a pen*)

(*Check Contents *)
log a_text's starts_with("this") -- (*true

Class Methods

store_delimiters

Store current AppleScript's text item delimiters to prepare to call text handling routines.

restore_delimiters

Restore a AppleScript's text item delimiters stored by previous store_delimiters()

replace

split

Make a list with splitting a text with specified delimiter.

join_list

Join a list of text with specified delimiter into a text.

strip

Remove white spaces and new line characters (space, tab, return, line feed, BEL) placed at beginning and ending of a text.

strip_beginning

Remove white spaces and new line characters (space, tab, return, line feed, BEL) placed at beginning of a text.

strip_endding

Remove white spaces and new line characters (space, tab, return, line feed, BEL) placed at endding of a text.

formatted_text

Replace words of "$1", "$2"... in a_text with item 1 of a_list, item 2 of a_list ...

It is useful to format a message to display.

sprintf

Generate formatted text using printf command.

Constructor

make_with

make a XText instance with given an AppleScript's text

Instance Methods

Manipulate Text

push

Appending a passed text

prepend

Prepending a passed text

replace

Replacing sub-text

replace_in_range

Replacing sub-text in specefied range.

format_with

Output with inserting texts into the template

strip

Remove white spaces and new line characters (space, tab, return, line feed) placed at the beginning and the endding.

strip_beginning

Remove white spaces and new line characters (space, tab, return, line feed) placed at the beginning.

strip_endding

Remove white spaces and new line characters (space, tab, return, line feed) placed at the endding.

Check Text Contetns

starts_with

If the contents of the XText starts with a given text, ture is returned

ends_with

If the contents of the XText ends with a given text, ture is returned

include

If the contents of the XText includes a given text, ture is returned

is_equal

If the contents of the XText is equal to a given text, ture is returned

offset_of

Obtain the position of passed text.

Obtain Sub Text

character_at

Obtain a character at specified index

word_at

Obtain a word at specified index

paragraph_at

Obtain a paragraph at specified index

text_in_range

Obtain a text in a specified range of indexes

Convert to List with Splitting

as_xlist_with

Make a XList instance of which elements are text items splitted with a specified delimiter

as_list_with

Make a list of which elements are text items splitted with a specified delimiter

Convert to AppleScript's text

as_text

Obtain contents of the XText instance with Unicode text class

as_unicode

A synonym of as_text.

as_string

Obtain contents of the XText instance with string class

Debugging

log

logging contents of the XText instance