XText
XText is a wrapper object of AppleScript's text object. XText provides object oriented interface to manipulate text and some advanced features.
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).
- An instance of XText is immutable like string or Unicode class object.
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 x_text to XText's make_with("this is a pen")
set x_text to XText's make_with_list({"this", "is", "a", "pen"}, space)
(*Replacing *)
log x_text's replace("pen", "flower")
-- result : [XText] this is a flower
(* Appending and Prepennding *)
set spaced_text to x_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 x_text's starts_with("this") -- (*true*)
log x_text's ends_with("this") -- (*false*)
log x_text's include("is") -- (*true*)
log x_text's offset_of("is") -- (*3*)
(* Make a list with splitting *)
log x_text's as_list_with(space) -- result : {"this", "is", "a", "pen"}
(* Obtain a part of text *)
log x_text's character_at(1) -- result : [XText] t
log x_text's word_at(1) -- result : [XText] this
log x_text's paragraph_at(1) -- result : [XText] this is a pen
(* Obtain of AppleScript's text properties *)
log character 1 of x_text -- result : "t"
log word 1 of x_text -- result : "this"
log length of x_text -- result : 13
(* Obtain content as AppleScript's text *)
log x_text's as_text() --(*this is a pen*)
History
- 1.4 -- 2020-06-17
- Removed dependency on XList.
- Removed as_xlist_with.
- Use make_with_xtext of XList.
- Added make_with_list, make_with_xlist, as_list.
- “as_list_with” accepts a list of text as delimiters parameter.
- “replace” accepts a list of text to replace targets.
- 1.3.2 -- 2020-01-27
- Use OpenHelpBook.scptd instead of HelpBook.osax
- added sprintf handler.
- 1.3.1 -- 2017-05-08
- Fixed links to XList in the help book.
- 1.3 -- 2016-10-19
- Enabled to work with AppleScript Libraries.
- Remove dependency on ModuleLoader.
- OS X 10.9 or later is required.
- 1.2.2 -- 2015-09-04
- character_at returns a new instance of XText instead of AppleScript text class object.
- 1.2.1 -- 2012-04-22
- The second argument can accept a text.
- BEL (character id 7) has been added into removed characters by strip, strip_beginning and strip_endding.
- Update Help.
- Fixed : "Edit Link" and handler's copy link does not work in Mac OS X 10.6.
- 1.2 -- 2010-02-02
- ModuleLoader 2.1 or later is requied to load "XText"
- Removed dependency on "ShowHelpBook".
- "join" was renamed to "join_list".
- 1.1 -- 2008-03-19
- Remove dependency on StringEngine
- Add meny class methods
- Add Japanese documents
- 1.0 -- 2007.07.10
- First Release