Keynote書類で選択中のスライド(ページ)において、default title itemからテキストを取り出し、正規表現で数字を抽出し、章番号(数字+章)を振り直すAppleScriptです。
本ScriptはmacOS 15.5+Keynote 14.4で動作確認を行なっていますが、バージョン依存する部分はほとんどないので、もっと前のバージョンでも動作することでしょう。本Scriptの動作には、BridgePlus AppleScript Libraryを必要とします。
このサンプルのKeynote書類では、ウィンドウ左端に表示させた「ナビゲータ」の階層表示の機能を利用して、一番上の階層(章トビラなど)だけを選択できるようになっています(そのように編集)。
▲スライドごとに階層構造になっているKeynote書類。章トビラ、記事トビラ、記事といった構造になっている
そのため、このナビゲータ上の階層を最上位のものだけ表示させると、最上位の章トビラだけを選択できます。
この状態で、章トビラのスライドだけを選択した状態で、本AppleScriptを実行すると、スライドのdefault tile item(タイトルが入るテキストボックス)に書かれた「1章」「2章」といった文字を読み取って、範囲内の最小の数値と最大の数値を検出し、冒頭から「1章」「2章」と章番号を割り振り直します。
Keynote書類の編集中に章構成に変更が発生し、章番号の割り振りをやり直す必要がある場合のために作成したものです。
AppleScript名:Keynoteの各slideのtitleで、章番号を振り直す v2.scpt |
— – Created by: Takaaki Naganoya – Created on: 2025/06/04 — – Copyright © 2021 Piyomaru Software, All Rights Reserved – 本バージョンから、Keynoteのスライドのdefault title itemへの描き戻しをサポート use AppleScript version "2.8" use framework "Foundation" use scripting additions use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html property NSStringTransformFullwidthToHalfwidth : a reference to current application’s NSStringTransformFullwidthToHalfwidth load framework –選択中のスライドのタイトルとスライドオブジェクトを取得 set {aList, aSel} to getTitleAndObjFromSelectedSlides() of me if {aList, aSel} = {false, false} then error "Keynote書類内のスライド選択状態に異常あり" –抽出したタイトルから章番号のみ取得し、最小値/最大値/枚数などの情報を取得 set nList to {} repeat with i in aList set j to contents of i set n to filterNumStr(j) of me set the end of nList to n end repeat set minRes to calcMin(nList) of me set maxRes to calcMax(nList) of me set countRes to maxRes – minRes + 1 set gapRes to calcGaps(nList) of me log {minRes, maxRes, countRes, gapRes} –最小値、最大値、枚数、連番からの数値抜け(リスト) –> {1, 30, 30, {13}} –章番号を振り直すデータを作成。選択範囲内の最小の章番号から1ずつインクリメントして章番号を付与 set n2List to {} set aCount to minRes repeat with i in aList if aCount < 10 then –1桁の数の場合には、全角数字を使用する set aNumStr to hanToZen(aCount as string) of me else copy aCount to aNumStr end if set the end of n2List to ((aNumStr as string) & "章") set aCount to aCount + 1 end repeat –return n2List –選択中のスライドの各タイトルに章番号を振り直し setEachTitlesToListedSlides(n2List, aSel) of me –Keynoteの各slideのタイトルを取得する on getTitleAndObjFromSelectedSlides() set aList to {} set selectedTitles to {} tell application "Keynote" tell front document set aSel to selection –エラーチェック if length of aSel ≤ 1 then display dialog "連番を振り直す対象のスライドが複数枚数選択されていないので処理終了します" buttons {"OK"} default button 1 with icon 1 return {false, false} end if set aFirstClass to class of contents of first item of aSel if aFirstClass is not equal to slide then display dialog "スライド単位で選択されていないので処理終了します" buttons {"OK"} default button 1 with icon 1 return {false, false} end if –スライドの逆順選択が発生していた場合には、listを逆順に入れ替えて正順(開始ページ→終了ページ)に修正 set fItem to slide number of first item of aSel set lItem to slide number of last item of aSel if fItem > lItem then set aSel to reverse of aSel –選択中のスライドのタイトル(default title item) repeat with i in aSel tell i try set tmpStr to object text of default title item on error set tmpStr to "" end try end tell set the end of aList to tmpStr end repeat return {aList, aSel} end tell end tell end getTitleAndObjFromSelectedSlides on setEachTitlesToListedSlides(newList, aSel) –連番を振り直したリストを元に、Keynoteの各slideのtitleを変更する set aCount to 1 tell application "Keynote" tell document 1 repeat with i in aSel tell i set object text of default title item to (contents of item aCount of newList) end tell set aCount to aCount + 1 end repeat end tell end tell end setEachTitlesToListedSlides on filterRealNumStr(aStr as string) set regStr to "\\d+\\.\\d+" set aRes to findStrByPattern(aStr, regStr) of me return aRes as real end filterRealNumStr on filterNumStr(aStr as string) set aLen to length of aStr set regStr to "\\d{1," & (aLen as string) & "}" set aRes to findStrByPattern(aStr, regStr) of me return aRes as {boolean, number} end filterNumStr on findStrByPattern(aText as string, regStr as string) set anNSString to current application’s NSString’s stringWithString:aText set aRange to anNSString’s rangeOfString:regStr options:(current application’s NSRegularExpressionSearch) if aRange = {location:0, length:0} then return "" set bStr to anNSString’s substringWithRange:aRange return bStr as string end findStrByPattern on calcMax(aList as list) set tmpFItem to first item of aList set aClass to class of tmpFItem set nArray to current application’s 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 calcMin(aList as list) set tmpFItem to first item of aList set aClass to class of tmpFItem set nArray to current application’s 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 calcGaps(aList as list) set nArray to (current application’s NSMutableArray’s arrayWithArray:aList) set maxRes to (nArray’s valueForKeyPath:"@max.self")’s intValue() set minRes to (nArray’s valueForKeyPath:"@min.self")’s intValue() –最小値から最大値までの連番リスト作成 set theIndexSet to current application’s NSIndexSet’s indexSetWithIndexesInRange:{minRes, maxRes} set theList to (current application’s SMSForder’s arrayWithIndexSet:theIndexSet) as list –補集合 set aSet to current application’s NSMutableSet’s setWithArray:theList set bSet to current application’s NSMutableSet’s setWithArray:nArray aSet’s minusSet:bSet return aSet’s allObjects() as list end calcGaps –半角→全角変換 on hanToZen(aStr as string) set aString to current application’s NSMutableString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(current application’s NSStringTransformFullwidthToHalfwidth) |reverse|:true) as string end hanToZen –全角→半角変換 on zenToHan(aStr as string) set aString to current application’s NSMutableString’s stringWithString:aStr return (aString’s stringByApplyingTransform:(current application’s NSStringTransformFullwidthToHalfwidth) |reverse|:false) as string end zenToHan |