レコード in リスト(配列に入れたレコード)のラベルを置換するAppleScriptです。
普通にプログラムを書いているととても必要な処理なので、Cocoaに最初から用意されているような気がするのですが、自分が知らないだけなんでしょうか?
# レコードのラベル部分を書き換えるのではなく、アクセス時の「valueForKeyPath」で指定するパス指定文字列のほうを書き換えたほうがまっとうな処理な気がします
AppleScript名:レコード in リストのラベルを置換する |
— – Created by: Takaaki Naganoya – Created on: 2019/03/09 — – Copyright © 2019 Piyomaru Software, All Rights Reserved — use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" set aList to {{gcName:"ディノスパーク札幌中央", address:"北海道 札幌市中央区 南三条西 1-8 ゲオディノス札幌中央ビル", loc_id:"94fQ-vXfZIWbNCIhWjMEEQ", latitude:43.0568243, longitude:141.3567126}, {gcName:"アドアーズ狸小路", address:"北海道 札幌市中央区 南三条西 4-12-1 アルシュビル5・6F", loc_id:"BaAw8srtioGkso463Jlrpg", latitude:43.0569575, longitude:141.352524}} set labelRepList to {{"gcName", "latitude", "longitude"}, {"placeName", "aLat", "aLong"}} set origLabel to contents of first item of labelRepList set toLabel to contents of second item of labelRepList set rList to repRecListLabels(aList, origLabel, toLabel) of me –> {{placeName:"ディノスパーク札幌中央", loc_id:"94fQ-vXfZIWbNCIhWjMEEQ", aLong:141.3567126, address:"北海道 札幌市中央区 南三条西 1-8 ゲオディノス札幌中央ビル", aLat:43.0568243}, {placeName:"アドアーズ狸小路", loc_id:"BaAw8srtioGkso463Jlrpg", aLong:141.352524, address:"北海道 札幌市中央区 南三条西 4-12-1 アルシュビル5・6F", aLat:43.0569575}} on repRecListLabels(aList as list, origLabel as list, toLabel as list) script spdRecList property newList : {} property aList : {} end script set (newList of spdRecList) to {} set (aList of spdRecList) to aList repeat with i in (aList of spdRecList) set aDict to (current application’s NSMutableDictionary’s dictionaryWithDictionary:i) set allKeys to (aDict’s allKeys()) as list set bDict to (current application’s NSMutableDictionary’s new()) repeat with ii in allKeys set jj to contents of ii set tmpVal to (aDict’s valueForKey:jj) if jj is in origLabel then using terms from scripting additions set aRes to offset of jj in origLabel end using terms from set aLabel to contents of item aRes of toLabel (bDict’s setObject:tmpVal forKey:aLabel) else (bDict’s setObject:tmpVal forKey:jj) end if end repeat set bDict to bDict as record set the end of (newList of spdRecList) to bDict end repeat return (newList of spdRecList) end repRecListLabels 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 as list) 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 as string, searchStr as string) 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 76 times, 1 visits today)