CotEditorの最前面のドキュメントで選択中の、JavaScriptのrecord in listをAppleScriptのオブジェクトに変換するAppleScriptです。
こんな風にJavaScriptのプログラム中のrecord in listを選択しておいてScriptを実行すると、
AppleScriptのオブジェクトに変換します。
AppleScript名:CotEditor上で選択中のJavaScriptのrecord in listをASのオブジェクトに変換.scpt |
— Created 2015-07-20 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" tell application "CotEditor" tell front document set jsonText to contents of selection end tell end tell set a to repChar(jsonText, "[", "{") of me set b to repChar(a, "]", "}") of me set c to repChar(b, string id 10, "") of me set d to repChar(c, tab, "") of me try set e to run script d set f to convToStr(e) of textKit tell application "CotEditor" make new document tell front document set contents to f end tell end tell on error erM display dialog erM end try –文字置換 on repChar(origText as string, targChar as string, repChar as string) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to targChar set tmpList to text items of origText set AppleScript’s text item delimiters to repChar set retText to tmpList as string set AppleScript’s text item delimiters to curDelim return retText end repChar –リストでもレコードでもなんでも文字列化して返すキット script textKit on convToStr(aRec) set aClass to (class of aRec) as string if (aClass = "integer") or (aClass = "number") or (aClass = "real") or (aClass = "string") or (aClass = "text") or (aClass = "Unicode text") or (aClass = "boolean") then set aRes to aRec as string else if aClass is "list" then set aRes to listToString(aRec) else if aClass is "record" then set aRes to recToString(aRec) else try set aRes to aRec as string on error –アプリケーションのオブジェクトとかはエラーで返す return false end try end if return aRes end convToStr –レコードをStringに変換 –エラートラップを使って、わざとエラーを発生させ、エラーメッセージからレコードをstringに変換する on recToString(aRec) –レコードを無理矢理stringにcastして、エラーメッセージを取得する try set a to aRec as string –ここでエラー発生 on error aMes set a to aMes end try –エラーメッセージ文字列から、元のレコードの情報を組み立てる set b to trimStrFromTo(a, "{", "}") set b to "{" & b & "}" return b end recToString on trimStrFromTo(aStr, fromStr, toStr) –fromStrは前から探す if fromStr is not equal to "" then set sPos to (offset of fromStr in aStr) + 1 else set sPos to 1 end if –toStrは後ろから探す if toStr is not equal to "" then set b to (reverse of characters of aStr) as string set ePos to (offset of toStr in b) set ePos to ((length of aStr) – ePos) else set ePos to length of aStr end if set aRes to text sPos thru ePos of aStr return aRes end trimStrFromTo –リストおよびリストに入ったレコードをStringに変換 on listToString(aList) set listText to {"{"} set quotChar to ASCII character 34 set firstFlag to true repeat with i in aList set j to contents of i set aClass to (class of i) as string if (aClass = "integer") or (aClass = "number") or (aClass = "real") or (aClass = "boolean") then set the end of listText to (getFirst(firstFlag) of me & j as text) set firstFlag to false else if (aClass = "string") or (aClass = "text") or (aClass = "Unicode text") then set the end of listText to ((getFirst(firstFlag) of me & quotChar & j as text) & quotChar) set firstFlag to false else if aClass is "list" then set the end of listText to (getFirst(firstFlag) & listToString(j)) –ちょっと再帰処理 set firstFlag to false else if aClass is "record" then set the end of listText to (getFirst(firstFlag) & recToString(j)) set firstFlag to false end if end repeat set the end of listText to "}" set listText to listText as text return listText end listToString on getFirst(aFlag) if aFlag = true then return "" if aFlag = false then return ", " end getFirst end script |
More from my site
(Visited 106 times, 3 visits today)