リスト(配列)に入れたレコードから、属性値ラベルと値を指定してしぼりこみを行うAppleScriptです。
{{label1:"data", label2:"data", label3:"data"},{label1:"XXXX", label2:"YYYYY", label3:"ZZZZ"}}
このようなデータの絞り込みは、従来はDatabase Events(macOS標準搭載のSQLiteラッパー、AppleScriptのフィルタ参照でデータの絞り込みを行う専用ツール)を利用したり、サードパーティのデータベースアプリケーションを併用する必要があったわけですが、前者はデータベースファイルを作成する必要があり、後者は購入する必要がありました。
# データベースファイルの作成をScripterに意識させない作りになっていれば、もう少し使い勝手がよかったはずですし、サンプルScriptが希少すぎ。海外の技術誌のバックナンバーをあさってようやくサンプルを見つけた時にはアゴが外れそうな勢いで驚きました
macOS 10.10以降でCocoaの機能が利用できるようになり、こうした配列に入れたレコードからデータを抽出するのに他のアプリケーションの機能を借りてくる必要がなくなりました。
正確にいえば、すべての配列要素を順次読み出して、指定の属性ラベルの値を調べて抽出といった地道な処理は昔からやっていましたが、そういうやり方だと、より高度な記述をしにくいところです。
AppleScript名:リストからフィールド内容を指定して抽出 |
— Created 2017-05-22 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aList to {{partOfSpeechLevel1:"名詞", baseForm:"北朝鮮", pronunciation:"キタチョーセン", position:0, partOfSpeechLevel3:"地域", reading:"キタチョウセン", surface:"北朝鮮", known:true, allFeatures:"名詞,固有名詞,地域,国,*,*,北朝鮮,キタチョウセン,キタチョーセン", conjugationType:"*", partOfSpeechLevel2:"固有名詞", conjugationForm:"*", allFeaturesArray:{"名詞", "固有名詞", "地域", "国", "*", "*", "北朝鮮", "キタチョウセン", "キタチョーセン"}, partOfSpeechLevel4:"国"}, {partOfSpeechLevel1:"記号", baseForm:"*", pronunciation:"*", position:3, partOfSpeechLevel3:"*", reading:"*", surface:":", known:false, allFeatures:"記号,一般,*,*,*,*,*,*,*", conjugationType:"*", partOfSpeechLevel2:"一般", conjugationForm:"*", allFeaturesArray:{"記号", "一般", "*", "*", "*", "*", "*", "*", "*"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"接頭詞", baseForm:"正", pronunciation:"セイ", position:4, partOfSpeechLevel3:"*", reading:"セイ", surface:"正", known:true, allFeatures:"接頭詞,名詞接続,*,*,*,*,正,セイ,セイ", conjugationType:"*", partOfSpeechLevel2:"名詞接続", conjugationForm:"*", allFeaturesArray:{"接頭詞", "名詞接続", "*", "*", "*", "*", "正", "セイ", "セイ"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"恩", pronunciation:"オン", position:5, partOfSpeechLevel3:"*", reading:"オン", surface:"恩", known:true, allFeatures:"名詞,一般,*,*,*,*,恩,オン,オン", conjugationType:"*", partOfSpeechLevel2:"一般", conjugationForm:"*", allFeaturesArray:{"名詞", "一般", "*", "*", "*", "*", "恩", "オン", "オン"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"氏", pronunciation:"シ", position:6, partOfSpeechLevel3:"人名", reading:"シ", surface:"氏", known:true, allFeatures:"名詞,接尾,人名,*,*,*,氏,シ,シ", conjugationType:"*", partOfSpeechLevel2:"接尾", conjugationForm:"*", allFeaturesArray:{"名詞", "接尾", "人名", "*", "*", "*", "氏", "シ", "シ"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"記号", baseForm:"「", pronunciation:"「", position:7, partOfSpeechLevel3:"*", reading:"「", surface:"「", known:true, allFeatures:"記号,括弧開,*,*,*,*,「,「,「", conjugationType:"*", partOfSpeechLevel2:"括弧開", conjugationForm:"*", allFeaturesArray:{"記号", "括弧開", "*", "*", "*", "*", "「", "「", "「"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"北極星", pronunciation:"ホッキョクセイ", position:8, partOfSpeechLevel3:"一般", reading:"ホッキョクセイ", surface:"北極星", known:true, allFeatures:"名詞,固有名詞,一般,*,*,*,北極星,ホッキョクセイ,ホッキョクセイ", conjugationType:"*", partOfSpeechLevel2:"固有名詞", conjugationForm:"*", allFeaturesArray:{"名詞", "固有名詞", "一般", "*", "*", "*", "北極星", "ホッキョクセイ", "ホッキョクセイ"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"2", pronunciation:"ニ", position:11, partOfSpeechLevel3:"*", reading:"ニ", surface:"2", known:true, allFeatures:"名詞,数,*,*,*,*,2,ニ,ニ", conjugationType:"*", partOfSpeechLevel2:"数", conjugationForm:"*", allFeaturesArray:{"名詞", "数", "*", "*", "*", "*", "2", "ニ", "ニ"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"記号", baseForm:"」", pronunciation:"」", position:12, partOfSpeechLevel3:"*", reading:"」", surface:"」", known:true, allFeatures:"記号,括弧閉,*,*,*,*,」,」,」", conjugationType:"*", partOfSpeechLevel2:"括弧閉", conjugationForm:"*", allFeaturesArray:{"記号", "括弧閉", "*", "*", "*", "*", "」", "」", "」"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"量産", pronunciation:"リョーサン", position:13, partOfSpeechLevel3:"*", reading:"リョウサン", surface:"量産", known:true, allFeatures:"名詞,サ変接続,*,*,*,*,量産,リョウサン,リョーサン", conjugationType:"*", partOfSpeechLevel2:"サ変接続", conjugationForm:"*", allFeaturesArray:{"名詞", "サ変接続", "*", "*", "*", "*", "量産", "リョウサン", "リョーサン"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"指示", pronunciation:"シジ", position:15, partOfSpeechLevel3:"*", reading:"シジ", surface:"指示", known:true, allFeatures:"名詞,サ変接続,*,*,*,*,指示,シジ,シジ", conjugationType:"*", partOfSpeechLevel2:"サ変接続", conjugationForm:"*", allFeaturesArray:{"名詞", "サ変接続", "*", "*", "*", "*", "指示", "シジ", "シジ"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"記号", baseForm:" ", pronunciation:" ", position:17, partOfSpeechLevel3:"*", reading:" ", surface:" ", known:true, allFeatures:"記号,空白,*,*,*,*, , , ", conjugationType:"*", partOfSpeechLevel2:"空白", conjugationForm:"*", allFeaturesArray:{"記号", "空白", "*", "*", "*", "*", " ", " ", " "}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"ミサイル", pronunciation:"ミサイル", position:18, partOfSpeechLevel3:"*", reading:"ミサイル", surface:"ミサイル", known:true, allFeatures:"名詞,一般,*,*,*,*,ミサイル,ミサイル,ミサイル", conjugationType:"*", partOfSpeechLevel2:"一般", conjugationForm:"*", allFeaturesArray:{"名詞", "一般", "*", "*", "*", "*", "ミサイル", "ミサイル", "ミサイル"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"成功", pronunciation:"セイコー", position:22, partOfSpeechLevel3:"*", reading:"セイコウ", surface:"成功", known:true, allFeatures:"名詞,サ変接続,*,*,*,*,成功,セイコウ,セイコー", conjugationType:"*", partOfSpeechLevel2:"サ変接続", conjugationForm:"*", allFeaturesArray:{"名詞", "サ変接続", "*", "*", "*", "*", "成功", "セイコウ", "セイコー"}, partOfSpeechLevel4:"*"}, {partOfSpeechLevel1:"名詞", baseForm:"発表", pronunciation:"ハッピョー", position:24, partOfSpeechLevel3:"*", reading:"ハッピョウ", surface:"発表", known:true, allFeatures:"名詞,サ変接続,*,*,*,*,発表,ハッピョウ,ハッピョー", conjugationType:"*", partOfSpeechLevel2:"サ変接続", conjugationForm:"*", allFeaturesArray:{"名詞", "サ変接続", "*", "*", "*", "*", "発表", "ハッピョウ", "ハッピョー"}, partOfSpeechLevel4:"*"}} set anArray to current application’s NSMutableArray’s arrayWithArray:aList set aRes to (my filterRecListByLabel1(anArray, "allFeaturesArray contains[cd] ’固有名詞’"))’s baseForm –リストに入れたレコードを、指定の属性ラベルの値で抽出 on filterRecListByLabel1(aRecList as list, aPredicate as string) set aArray to current application’s NSArray’s arrayWithArray:aRecList set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate return filteredArray end filterRecListByLabel1 |
More from my site
(Visited 254 times, 2 visits today)