(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
filterList Handler Demo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
VER: 1.1 LAST UPDATE: 2016-06-10
PURPOSE:
• Demo how my filterList() handler works
AUTHOR: JMichaelTX
REQUIRED:
1. Mac OS X Yosemite 10.10.5+
REF: The following were used in some way in the writing of this script.
1. List Manipulation Routines
http://www.macosxautomation.com/applescript/sbrt/sbrt-07.html
2. AppleScript Users List (ASUL)
• Feedback and suggestions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)
set myList to {"@Test",
"other", "@end",
"else", "other",
"@Test"}
set
startsList to my filterList(myList,
"@", "starts with")
set
endsList to my filterList(myList,
"end", "ends with")
set containsList to my
filterList(myList,
"the",
"contains")
set replaceList to my
filterList(myList, {"@Test",
"@Done"}, "replace")
set
removeList to my filterList(myList,
"other", "remove")
set removeDupsList to my
filterList(myList,
"other",
"remove dups")
set removeAllDupsList to my
filterList(myList,
"",
"remove all dups")
set containsList to my
filterList(myList,
"the",
"bad action")
## will cause error
--~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on filterList(pList,
pMatch, pMatchAction)
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(*
VER: 1.1 LAST UPDATE: 2016-06-10
RETURNS: List filtered by the requested pMatchAction
PARAMETERS:
• pList : Source List of text items
• pMatch : Can be String, or List of two text items
• Use String to search List for all pMatchActions, except:
• Use List of {"MatchStr", "ReplaceStr"} for "replace" action
• pMatchAction : Action to perform on found items
Must be one of these strings:
• starts with -- returns only items that start with pMatch
• ends with -- returns only items that end with pMatch
• contains -- returns only items that contain pMatch
• replace -- returns all items, but replace items that
exactly match item 1 of pMatch with item 2 of pMatch
• remove -- returns only items that do not match pMatch
• remove dups -- returns all items, except dups of item
that matches pMatch
• remove all dups -- returns all items, except removes dups of all items
regardless of match.
AUTHOR: JMichaelTX (with help from friends at ASUL)
Since lists don't support "whose" or "where" clauses,
have to roll my own.
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*)
local foundItems,
oItem, pMatchStr,
pReplaceStr
if (class of
pMatch) is list then
set
pMatchStr to text of (item 1 of
pMatch)
set
pReplaceStr to text of (item 2 of
pMatch)
else
set pMatchStr to
pMatch
end if
set
foundItems to {}
repeat with
oItem in pList
if (pMatchAction =
"starts with") then
if (oItem starts with
pMatchStr) then
set end of
foundItems to text of
oItem
end if
else if (pMatchAction =
"ends with") then
if (oItem ends with
pMatchStr) then
set end of
foundItems to text of
oItem
end if
else if (pMatchAction =
"contains") then
if (oItem contains
pMatchStr) then
set end of
foundItems to text of
oItem
end if
else if (pMatchAction =
"replace") then
if (text of
oItem = pMatchStr) then
set end of
foundItems to pReplaceStr
else
set end of
foundItems to text of
oItem
end if
else if (pMatchAction =
"remove") then
if (text of
oItem ≠ pMatchStr) then
set end of
foundItems to text of
oItem
end if
else if (pMatchAction =
"remove dups") then
if (text of
oItem = pMatchStr) then
if (oItem is not in
foundItems) then
set end of
foundItems to text of
oItem
end if
else
set end of
foundItems to text of
oItem
end if
else if (pMatchAction =
"remove all dups") then
if (oItem is not in
foundItems) then
set end of
foundItems to text of
oItem
end if
else ### ERROR ###
error "Invalid pMatchAction in filterList handler:" &
return &
"»»»" & pMatchAction & ¬
"«««" & return &
return & ¬
"Must be one of the following:" &
return &
"starts with, ends with, contains, replace, remove, remove dups, remove all dups"
end if
end repeat
return foundItems
end filterList
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~