1D List(Array)中における指定項目の出現位置(offset)を求めるAppleScriptです。標準命令「offset of ① in ②」を拡張して、本来はテキスト同士の処理であったものを、リスト同士で処理するようにしています。
サポートしているのは4つのパターンです。
(1)offset of string1 in string2
標準パターンですが、例によって標準命令よりも2.5倍速に高速化してあります。
(2)offset of list1 in list2
想定ターゲットのパターンです。1D Listと1D Listのoffsetを計算します。
(3)offset of list1 in string2
(2)を指定しようとして間違ったケースです。
(4)offset of string1 in list2
ASOC(use framework “Foundation”宣言時)では、そのままではoffset命令がだましにくくなるので(パラメータが文字列ではないとしてエラーになる)、using terms from scripting additionsで囲う必要がありました。
パラメータにヌルリストを指定した場合に、エラーになる件を修正(chkSequential)してあります。
AppleScript名:offset of list in list v3 |
— Created 2018-09-18 by Takaaki Naganoya — Modified 2018-12-14 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use framework "Foundation" use scripting additions –case 1 using terms from scripting additions –ここ、AppleScriptObjC(use framework "Foundation"宣言時)では必要になる set bRes to offset of "a" in "bcdefa" end using terms from –> 6 –case 2 (The target case) set aTargList to {"Finder", "で", "選択", "中", "の", "AI", "書類", "上", "の", "「", "製品", "名", "」", "レイヤー", "から", "抜き出し", "た", "コード", "を", "もと", "に", "スペック", "情報", "を", "Google", " ", "Spreadsheet", "から", "展開", "し", "て", "保存", "。"} set bList to {"の", "メニュー", "で"} using terms from scripting additions set aRes to offset of bList in aTargList end using terms from –> 2 –case 3 (Illegular case) set aTargList to {"Finder", "で", "選択", "中", "の", "AI", "書類", "上", "の", "「", "製品", "名", "」", "レイヤー", "から", "抜き出し", "た", "コード", "を", "もと", "に", "スペック", "情報", "を", "Google", " ", "Spreadsheet", "から", "展開", "し", "て", "保存", "。"} set bList to "で" using terms from scripting additions set aRes to offset of bList in aTargList end using terms from –> 4 –case 4 (Illegular case) set aTargList to "で" set bList to {"で"} using terms from scripting additions set aRes to offset of bList in aTargList end using terms from —> 1 –case 3a (Illegular case) set aTargList to {} set bList to "で" using terms from scripting additions set aRes to offset of bList in aTargList end using terms from –> false –case 2a (The target case) set aTargList to {"Finder", "で", "選択", "中", "の", "AI", "書類", "上", "の", "「", "製品", "名", "」", "レイヤー", "から", "抜き出し", "た", "コード", "を", "もと", "に", "スペック", "情報", "を", "Google", " ", "Spreadsheet", "から", "展開", "し", "て", "保存", "。"} set bList to {} using terms from scripting additions set aRes to offset of bList in aTargList end using terms from –> false on offset of bArg in anArg set aClass to class of anArg set bClass to class of bArg if {aClass, bClass} = {text, text} then –case 1 return getOffset(anArg, bArg) of me else if {aClass, bClass} = {list, list} then –case 2 (The target case) return execOffsetList(bArg, anArg) of me else if {aClass, bClass} = {text, list} then –case 3 (Illegular case) return execOffsetList(bArg, {anArg}) of me else if {aClass, bClass} = {list, text} then –case 4 (Illegular case) return execOffsetList({bArg}, anArg) of me end if end offset –1D List同士のoffset演算を行うルーチンの本体 on execOffsetList(aList as list, bList as list) set resList to {} repeat with i in aList set j to contents of i set aCount to 1 repeat with ii in bList set jj to contents of ii if jj = j then set the end of resList to aCount exit repeat end if set aCount to aCount + 1 end repeat end repeat –見つかったItem No.が連続値かどうかチェック set sRes to chkSequential(resList) of me if sRes = true then return contents of first item of resList else return false end if end execOffsetList –与えられた1D Listが連続値かどうかをチェックする on chkSequential(aList) if length of aList = 1 then return true if aList = {} then return false set aFirst to first item of aList set aList to rest of aList repeat with i in aList set j to contents of i if j is not equal to (aFirst + 1) then return false end if copy j to aFirst end repeat return true end chkSequential –テキスト同士のoffset ofを(2.5x fasterで)実行する on getOffset(str, searchStr) set d to divideBy(str, searchStr) if (count d) is less than 2 then return 0 return (length of item 1 of d) + 1 end getOffset on divideBy(str, separator) set delSave to AppleScript’s text item delimiters set the AppleScript’s text item delimiters to separator set strItems to every text item of str set the AppleScript’s text item delimiters to delSave return strItems end divideBy |
More from my site
(Visited 56 times, 1 visits today)