Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

月: 2025年6月

Keynoteの各slideのtitleで、章番号を振り直す v2

Posted on 6月 5 by Takaaki Naganoya

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

★Click Here to Open This Script 

Posted in Object control regexp Text | Tagged 15.0savvy Keynote | Leave a comment

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 15, Sequoia
  • 指定のWordファイルをPDFに書き出す
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Numbersで選択範囲のセルの前後の空白を削除
  • メキシカンハットの描画
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AppleScriptによる並列処理
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • macOS 15でも変化したText to Speech環境
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • デフォルトインストールされたフォント名を取得するAppleScript
  • Keynote/Pagesで選択中の表カラムの幅を均等割
  • macOS 15 リモートApple Eventsにバグ?
  • AppleScript入門① AppleScriptってなんだろう?
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • Script Debuggerの開発と販売が2025年に終了
  • Keynoteで2階層のスライドのタイトルをまとめてテキスト化
  • NSObjectのクラス名を取得 v2.1

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (194) 14.0savvy (147) 15.0savvy (134) CotEditor (66) Finder (51) iTunes (19) Keynote (119) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (76) Pages (55) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC