XText Reference

XText

What

XText はテキスト処理をオブジェクト指向で行える、AppleScript テキストのラッパーオブジェクトを提供します。置換、分割、空白文字列の除去など、生のAppleScriptでは面倒な処理を簡潔に記述することができます。

XText のインスタンスは AppleScript のテキストオブジェクトを parent とします。したがって・・・

クラスメソッドとインスタンスメソッドで同じようなメソッドがあります(e.g., repace, split など)が次のような違いがあります。

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*)
log a_text's ends_with("this") -- (*false*)
log a_text's include("is") -- (*true*)
log a_text's offset_of("is") -- (*3*)

(* Make a list with splitting *)
log a_text's as_xlist_with(space)
(*[XList]
1 this
2 is
3 a
4 pen*)

log a_text's as_list_with(space) -- result : {"this", "is", "a", "pen"}

(* Obtain a part of text *)
log a_text's character_at(1) -- result : [XText] t
log a_text's word_at(1) -- result : [XText] this
log a_text's paragraph_at(1) -- result : [XText] this is a pen

(* Obtain of AppleScript's text properties *)
log character 1 of a_text -- result : "t"
log word 1 of a_text -- result : "this"
log length of a_text -- result : 13

(* Obtain content as AppleScript's text *)
a_text's as_unicode()
a_text's as_string()
XText Reference