AppleScript名:listの項目をフィルタリング(キーワードを含む) |
— Created 2014-11-21 by Takaaki Naganoya — 2014 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "ASObjCExtras" set aList to {"piyomaru", "Naganoya", "Takaaki", "MacBook Pro Retina mid 2012", "piyomarusoft"} set bList to filterListUsingPredicate(aList, "SELF contains[cd] %@", "Piyo") –部分一致 –> {"piyomaru", "piyomarusoft"} set cList to filterListUsingPredicate(aList, "SELF matches[cd] %@", "piyomaru") –完全一致 –> {"piyomaru"} set dList to filterListUsingPredicate(aList, "SELF like[c] %@", "*soft") –後方一致 –> {"piyomarusoft"} on filterListUsingPredicate(aList as list, aPredicateStr as string, targStr as string) set setKey to current application’s NSMutableSet’s setWithArray:aList set aPredicate to current application’s NSPredicate’s predicateWithFormat_(aPredicateStr, targStr) set aRes to (setKey’s filteredSetUsingPredicate:aPredicate) set bRes to aRes’s allObjects() set cRes to bRes as {list, list of string or string} return cRes end filterListUsingPredicate –2Dリストから、指定インデックスアイテムで、指定データが該当する最初のものを返す on searchInListByIndexItem(aList as list, itemNum as integer, hitData as string) –ListからNSMutableSetへの型変換 set setKey to current application’s NSMutableSet’s setWithArray:aList –Predicate Stringを組み立てる if itemNum < 1 then return {} set aPredicateStr to ("SELF[" & (itemNum – 1) as string) & "] == ’" & hitData & "’" –抽出 set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicateStr set aRes to (setKey’s filteredSetUsingPredicate:aPredicate) set bRes to aRes’s allObjects() –NSArrayからListに型変換して返す set cRes to bRes as list if cRes is not equal to {} then set cRes to contents of first item of cRes end if return cRes end searchInListByIndexItem |
More from my site
(Visited 61 times, 1 visits today)