XList
XList provides a wrapper object to treat AppleScript's list as iterator, stack and queue.
XList will fill many missing features of AppleScript's list.
use XList : script "XList"
on run
(*== Iterator ==*)
set an_iterator to XList's make_with({"a", "b", "c"})
repeat while an_iterator's has_next()
set an_item to next() of an_iterator
log an_item
end repeat
(*== Queue ==*)
set a_queue to make XList
a_queue's unshift("a")
a_queue's unshift("b")
log a_queue's shift() -- result : "b"
log a_queue's shift() -- result : "a"
(*== Stack ==*)
set a_stack to make XList
a_queue's push("a")
a_queue's push("b")
log a_queue's pop() -- result : "b"
log a_queue's pop() -- result : "a"
(*== Accessing list elements ==*)
set a_list to XList's make_with({"a", "b", "c"})
log a_list's item_counts() -- 3
log a_list's item_at(2) -- "b"
log a_list's has_item("b") --true
log a_list's has_item("d") --false
log a_list's index_of("b") -- 2
log a_list's index_of("d") -- 0
log a_list's delete_at(2) -- "b"
log (set_item of a_list for "e" at 2) -- "e"
log a_list's list_ref() -- {"a", "e"}
(*== Conversion to Text ==*)
log a_list's as_text_with(", ") -- "a, e"
(*== Looping with block script ==*)
set before_c to missing value
script block1
on do(x)
if (x is "c") then
return false
else
set before_c to x
return true
end if
end do
end script
an_iterator's each(block1)
log before_c -- result : b
script block2
on do(x, sender)
if x is "b" then
tell sender
set_item_at("d", current_index()) -- change an item of a list
end tell
end if
return true
end do
end script
an_iterator's enumerate(block2)
log an_iterator's all_items() -- result : {"a", "d", "c"}
(*== Generating new list using “map” ==*)
script block3
on do(x)
return x & "a"
end do
end script
log an_iterator's map(block3)
(*[XListInstance]
1 aa
2 da
3 ca*)
end run
History
- 1.8 -- 2020-06-17
- Removed dependency on XText.
- Removed as_xtext_with
- Use make_with_xlist of XText
- Added as_list
- each and each_rush return the target of XList instance.
- Changed “text item delimiters” in as_text_with, as_unicode_with and as_string_with is not restored.
- It is recommended to use make_with_xlist of XText.
- 1.7.3 -- 2020-04-10
- Improved performance of each, map, map_as_list.
- Added each_rush.
- 1.7.2 -- 2020-01-27
- Use OpenHelpBook.scptd instead of HelpBook.osax.
- 1.7.1 -- 2017-05-09
- Fixed links to XText in the help book.
- 1.7 -- 2016-10-18
- Enabled to work with AppleScript Libraries.
- Remove dependency on ModuleLoader.
- OS X 10.9 or later is required.
- 1.6r2 -- 2014-10-01
- Fixed : A script link of the sample code in the HelpBook does not work.
- 1.6 -- 2012-11-28
- "each", "map" and "map_as_list" will be passed contents of elements of a list to "do" handler of a script object intead of references to elements of a list.
- passing a referece to an element does not work with AppleScriptObjC.
- Added "enumerate".
- Added "set_item_at".
- "each", "map" and "map_as_list" will be passed contents of elements of a list to "do" handler of a script object intead of references to elements of a list.
- 1.5 -- 2012-05-27
- Added "add_from_list".
- Added "shallow_copy".
- Added "deep_copy".
- Added "iterator".
- 1.4.1 -- 2012-04-21
- Update Help.
- Fixed : "Edit Link" and handler's copy link does not work in Mac OS X 10.6.
- item_at can accept list of indexes.
- Added as_text_with.
- 1.4 -- 2010-01-29
- Removed dependecy on "ShowHelpBook".
- Added "items_in_range"
- The "count" command is used to obtain length of a list instead of "length" property in "count_items".
- ModuleLoader 2.1 or later is required to load "XList".
- 1.3 -- 2008-03-20
- Removed dependency on StringEngine
- 1.2.1 -- 2008.02.02
- Added exchange_items().
- 1.2 -- 2008.01.05
- The handler 'next()' will raise error number 1351 when there is not next element in the list.
- In the previous version, 'missing value' is returned. But 'missing value' can be accepted as a list element.
- The handler 'next()' will raise error number 1351 when there is not next element in the list.
- 1.1.1 -- 2007.12.04
- Fixed errors of the refernece manual.
- 1.1 -- 2007.07.10
- Renamed "SmartList" to "XList"
- Renamed "next_item" to "next"
- Added make_with , make, make_with_text, count_items, set_item, index_of, current_index, has_item,as_xtext_with, as_unicode_with, as_string_with
- 1.0.2 -- 2007.06.01
- Conetns of a list can be changed by map and each.
- A reference to an item of a list is passed to do handler.
- Conetns of a list can be changed by map and each.
- 1.0.1 -- 2007.05.30
- Added English document
- Added reset()
- Added all_items()
- Added each()
- 1.0 -- 2007.05.23
- First Release