Keynoteの最前面の書類で表示中のスライド(ページ)で選択中の表の、指定行のテキストのセルの文字列長の分布をグラフ出力するAppleScriptです。
# Blogの英訳本の企画を動かしていますが、たしかに日本語のタイトルを(日本人が)見てもけっこうわかりにくいのかも?
# タイトルをGoogle翻訳に突っ込むと、それなりに「そんな感じじゃね?」という英語が出てきますが、、、
こういう、電子書籍の紹介文を掲載しているページがあって、そこに書いてある紹介文の長さの分布を計算してみるために、ありものの部品を組み合わせて作ったものです。
「だいたい3行ぐらい」というふんわりとしたルールのもとに18冊分ほど書いてきましたが、本の冊数が増えてページを追加するにあたって、文字列長の分布を計算してみようと思い立ってScriptを書いてみました。
043 # 044 ## 045 046 ### 047 048 ## 049 ### 050 # 051 # 052 # 053 054 # 055 056 # 057 # 058 059 060 061 062 063 064 065 066 067 068 #
分布はわかりましたが、似たような処理を行うことが多いので、何か簡単なGUIをつけておいたほうがいいような気がするような、、、
AppleScript名:選択中の表のデータを取得して、特定の行のデータの文字列長を集計.scpt |
— – Created by: Takaaki Naganoya – Created on: 2022/07/04 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSMutableArray : a reference to current application’s NSMutableArray set aList to get2DListFromKeynoteTableSelection() of me if aList = "" then return false –取得した各セルの文字列長をすべて取得する set aLen to length of aList set aaLen to length of item 1 of aList set nList to {} repeat with i from 3 to (aLen) by 3 repeat with ii from 1 to aaLen set the end of nList to length of (contents of item ii of item i of aList) end repeat end repeat –文字列長の度数分布を計算 set minRes to calcMin(nList) of me set maxRes to calcMax(nList) of me set itemCount to maxRes – minRes set n2List to makeBlankListByItem(itemCount + 1, 0) of me repeat with i in nList set j to contents of i set tmpNum to contents of item (j – minRes + 1) of n2List set tmpNum to tmpNum + 1 set item (j – minRes + 1) of n2List to tmpNum end repeat –文字列長の度数分布のグラフを文字出力 set tList to {} set aCount to minRes repeat with i in n2List set gText to returnRepeatedText(i, "#") of me set tNumStr to makeFN(aCount, 3) of me set totalText to tNumStr & " " & gText set the end of tList to totalText set aCount to aCount + 1 end repeat return retArrowText(tList, return) of me –リストを任意のデリミタ付きでテキストに on retArrowText(aList, aDelim) set aText to "" set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set aText to aList as text set AppleScript’s text item delimiters to curDelim return aText end retArrowText on returnRepeatedText(aNum, aChar) set aText to "" repeat aNum times set aText to aText & aChar end repeat return aText end returnRepeatedText on makeBlankListByItem(itemNum, blankItem) set tmpList to {} repeat itemNum times set the end of tmpList to blankItem end repeat return tmpList end makeBlankListByItem on calcMin(aList as list) set tmpFItem to first item of aList set aClass to class of tmpFItem set nArray to (NSMutableArray’s arrayWithArray:aList) if aClass = real then set maxRes to (nArray’s valueForKeyPath:"@min.self")’s doubeValue() else set maxRes to (nArray’s valueForKeyPath:"@min.self")’s intValue() end if return maxRes end calcMin on calcMax(aList as list) set tmpFItem to first item of aList set aClass to class of tmpFItem set nArray to (NSMutableArray’s arrayWithArray:aList) if aClass = real then set maxRes to (nArray’s valueForKeyPath:"@max.self")’s doubeValue() else set maxRes to (nArray’s valueForKeyPath:"@max.self")’s intValue() end if return maxRes end calcMax on makeFN(aNum, aDigit) set aText to "00000000000" & (aNum as text) set aLen to length of aText set aRes to text (aLen – aDigit + 1) thru -1 of aText return aRes end makeFN –Keynoteで選択中の表のすべてのセルのデータを2Dで返す on get2DListFromKeynoteTableSelection() tell application "Keynote" tell front document tell current slide try set theTable to first table whose class of selection range is range on error return "" –何も選択されてなかった場合 end try tell theTable set selList to value of every cell –すべてののデータを取得 set selHeight to count every row set selWidth to count every column end tell end tell end tell end tell set aLen to length of selList set aaLen to selHeight set bList to {} repeat with i from 1 to aaLen set aHoriList to {} repeat with ii from 1 to selWidth set j1 to ii + (i – 1) * selWidth set tmpCon to contents of item j1 of selList set aClass to class of tmpCon if aClass = number or aClass = integer or aClass = real then set tmpCon to tmpCon as integer end if set the end of aHoriList to tmpCon end repeat set the end of bList to aHoriList end repeat return bList end get2DListFromKeynoteTableSelection –テキストを指定デリミタでリスト化 on parseByDelim(aData, aDelim) set aText to current application’s NSString’s stringWithString:aData set aList to aText’s componentsSeparatedByString:aDelim return aList as list end parseByDelim –1D/2D Listのユニーク化 on uniquifyList(aList as list) set aArray to current application’s NSArray’s arrayWithArray:aList set bArray to aArray’s valueForKeyPath:"@distinctUnionOfObjects.self" return bArray as list end uniquifyList |