オープン中のKeynote書類のうち、指定タイトルのスライド中の表の値を取得したり設定するAppleScriptです。
シート名(タイトル)、表Yラベル、表Xラベル
の3つのパラメータをもとに、Keynote書類中の指定スライドの表の中のセルにアクセスして、値を取得/設定します。
処理対象のKeynote書類はKeynoteでオープンしている必要があり、各スライド中の表は1つのみ存在していることを処理の前提条件としています。
一応、再利用性を高めるためにScriptオブジェクト化しています。これがベストな方法なわけではありませんが、こんなもんでしょう。
AppleScript名:Keynoteで指定の表をX LabelとY Labelの交差するセルに設定する v2 |
— Created 2018-12-20 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set targCellNameX to "Col2" set targCellNameY to "Row4" set storeVal to "9999" set keynoteSlide to "Page 2 Title" set keynoteSlideObj to getTitleStringFromFrontKeynoteDocumentFilterByString(keynoteSlide) of getKeynoteSlideKit –set aRes to getValueOnKeynoteTableByXYLabels(targCellNameY, targCellNameX, keynoteSlideObj) of getKeynoteSlideKit setValueOnKeynoteTableByXYLabels(targCellNameY, targCellNameX, storeVal, keynoteSlideObj) of getKeynoteSlideKit script getKeynoteSlideKit –Keynote タイトル指定してslideを取得する on getTitleStringFromFrontKeynoteDocumentFilterByString(targString) set repTargList to {string id 10, string id 11, string id 13} –LF,CR,VTab一括置換 tell application "Keynote" if (count every document) = 0 then return tell front document set tList to object text of default title item of every slide end tell end tell –タイトルごとにゴミ取り(改行文字の削除) set outList to {} set sCount to 1 set hitF to false repeat with i in tList set j1 to contents of i set jTmp to (paragraphs of j1) as string –しつこい改行文字(?)を除去するための処理 set j2 to replaceTextMultiple(jTmp, repTargList, "") of me as string if j2 is equal to targString then set hitF to true exit repeat end if set sCount to sCount + 1 end repeat if hitF = false then return false tell application "Keynote" tell front document return item sCount of (every slide) end tell end tell end getTitleStringFromFrontKeynoteDocumentFilterByString –リストを指定デリミタで区切ったテキストに変換 on listToStringUsingTextItemDelimiter(sourceList as list, textItemDelimiter as string) set anArray to current application’s NSArray’s arrayWithArray:sourceList set aString to anArray’s componentsJoinedByString:textItemDelimiter return (aString as string) end listToStringUsingTextItemDelimiter –任意のデータから特定の文字列を複数パターン一括置換 on replaceTextMultiple(origData as string, origTexts as list, repText as string) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to origTexts set origData to text items of origData set AppleScript’s text item delimiters to {repText} set origData to origData as text set AppleScript’s text item delimiters to curDelim return origData end replaceTextMultiple –行ラベルと列ラベルを個別に指定し、交点のセルに指定の値を入れる on setValueOnKeynoteTableByXYLabels(targCellNameY, targCellNameX, storeVal, keynoteSlideObj) tell application "Keynote" tell keynoteSlideObj tell table 1 tell row 1 set cList to value of every cell set xRes to getOffsetFromList(targCellNameX, cList) of me end tell tell column 1 set dList to value of every cell set yRes to getOffsetFromList(targCellNameY, dList) of me end tell tell row yRes tell cell xRes set its value to storeVal end tell end tell end tell end tell end tell end setValueOnKeynoteTableByXYLabels –行ラベルと列ラベルを個別に指定し、交点のセルに指定の値を入れる on getValueOnKeynoteTableByXYLabels(targCellNameY, targCellNameX, keynoteSlideObj) tell application "Keynote" tell keynoteSlideObj tell table 1 tell row 1 set cList to value of every cell set xRes to getOffsetFromList(targCellNameX, cList) of me end tell tell column 1 set dList to value of every cell set yRes to getOffsetFromList(targCellNameY, dList) of me end tell tell row yRes tell cell xRes set tmpFormat to format set its format to text set aRes to its value set its format to tmpFormat if aRes = missing value then return "" else return aRes end if end tell end tell end tell end tell end tell end getValueOnKeynoteTableByXYLabels on getOffsetFromList(aTarg, aList) using terms from scripting additions set aRes to offset of aTarg in aList end using terms from return aRes end getOffsetFromList —-Offset command overwrapper 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 end script |