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

Keynoteの現在のスライド上で選択中のテキストをもとに、後続の記事トビラページのタイトルに内容を設定 v2a

Posted on 3月 23 by Takaaki Naganoya

Keynote書類の現在表示中のスライド上で選択中のテキストアイテム(ボックス)の内容をもとに、後続の記事トビラページのタイトルを設定するAppleScriptです。Keynote 14.3+macOS 15.4betaで動作確認していますが、とくにバージョン依存した書き方などは行なっていません。

また、本来はiWork汎用オブジェクトの座標によるソート機能をライブラリとして独立させ、バンドル形式のScript書類に入れてあったのですが、Blog掲載のためにフラットなScriptに書き換えています。

本来、Keynoteのようにスライドのインデント(レベル変更)を行って階層構造を形成できるアプリでは、それぞれのスライド(ページ)のレベルを取得したり変更できることが望ましいのですが、Keynoteの機能セットの範囲内ではAppleScriptからそのような操作は行えません。そこで、各スライドのベースレイアウトに何を用いているかによって擬似的にレベルを判定しています。

KeynoteのAppleScript対応機能はiWork Apps中では屈指の対応度を誇っていますが、レイアウトした画像の内容データにアクセスできないのと、各スライドのレベルの取得/変更が行えない点がものすごく残念です。

電子書籍「Cocoa Scripting Course」の作成用に、以前にも作ったことがあるかもしれませんが、ふたたび作ってしまいました。ちょっと大きめの書き捨てScriptです。


▲Keynote書類の章トビラ上で章の記事内容を示すテキストボックスを選択した状態で本AppleScriptを実行。複数のボックスを選択してあっても、座標値でソートして順番を決定


▲各記事のトビラページに、章トビラから取得したテキストを設定。以下、繰り返し


▲章トビラ上のテキストをすべて記事トビラのタイトルに設定

AppleScript名:現在のスライド上で選択中のテキストをもとに、後続の記事トビラページのタイトルに内容を設定 v2a.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/03/23
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
–use iwoSortLib : script "iWorkObjSortLib"

tell application "Keynote"
  tell front document
    –章扉の上にあるテキストアイテム(各記事タイトルが入っているものと想定、複数可能)を取得
    
set aSelList to selection
    
set bSelList to {}
    
set sCount to count every slide
    
    
    
–画面上で選択しておいたオブジェクトのうち、text itemのみを抽出(念のため)
    
repeat with i in aSelList
      set j to contents of i
      
set aClass to class of j
      
if aClass is text item then
        set the end of bSelList to j
      end if
    end repeat
    
    
set cSelList to sortIWorkObjectsByPositionAndRetObjRef(bSelList, {"positionX", "positionY"}, {true, true}) of iWorkObjSort
    
    
    
–章トビラ上のテキストアイテム(複数可)をループしつつ、中のテキストを行ごとに分解してリスト化
    
–(連結前にあらかじめX座標をもとにソートしておいたほうがいい??)
    
set textList to {}
    
repeat with i in cSelList
      set tmpCon to object text of i
      
set tmpList to paragraphs of tmpCon
      
set textList to textList & tmpList
    end repeat
    
–return textList
    
    
–章トビラのページの情報を取得
    
tell current slide
      set curSlideNum to slide number
      
set curSlideLayout to name of base layout
    end tell
    
    
–章トビラの次のページの情報を取得(ここが必ず記事トビラであるという前提のもとに処理)
    
tell slide (curSlideNum + 1)
      set nextSlideLayout to name of base layout
    end tell
    
    
set targSlideList to {}
    
repeat with i from (curSlideNum + 1) to sCount
      tell slide i
        set tSTheme to name of base layout
        
if tSTheme = curSlideLayout then
          –章トビラを検出したら処理終了
          
exit repeat
        else if tSTheme = nextSlideLayout then
          –扉+1ページのスライド(記事カバー)を検出したら記録
          
set the end of targSlideList to i
        end if
      end tell
    end repeat
    
–return targSlideList
    
    
set iCount to 1
    
repeat with i in targSlideList
      try
        set tmpT to contents of item iCount of textList
        
tell slide i
          set object text of default title item of it to tmpT
        end tell
        
set iCount to iCount + 1
      on error
        return
      end try
    end repeat
  end tell
end tell

–ライブラリとしてバンドル形式のAppleScript書類に組み込んでいたものをBlog掲載用に展開した
script iWorkObjSort
  property parent : AppleScript
  
use AppleScript
  
use framework "Foundation"
  
use framework "AppKit"
  
use scripting additions
  
  
script spd
    property aaSel : {}
  end script
  
  
–Keynote上のiWork ObjをXY座標でソートして結果を返す(App Obj情報はitem noだけ)
  
on sortIWorkObjectsByPosition(aaSel, sortLabelLIst, sortDirectionList)
    tell application "Keynote"
      set aVer to version
      
if aVer < "12.0" then return
      
      
tell front document
        set posList to {}
        
set aCount to 1
        
        
repeat with ii in aaSel
          set jj to contents of ii
          
set bClass to class of jj
          
          
tell jj
            set {posX, posY} to position
            
            
try
              set tmpStr to object text as string
            on error
              set tmpStr to ""
            end try
          end tell
          
          
set the end of posList to {positionX:posX, positionY:posY, objID:aCount, myStr:tmpStr}
          
          
set aCount to aCount + 1
        end repeat
        
        
–座標データをもとにソート
        
set sortedList to sortRecListByLabel(posList, sortLabelLIst, sortDirectionList) of me
        
      end tell
    end tell
    
    
return sortedList
  end sortIWorkObjectsByPosition
  
  
  
–Keynote上のiWork ObjをXY座標でソートして結果を返す(App Objだけ返す)
  
on sortIWorkObjectsByPositionIncludingObjRef(aaSel, sortLabelLIst, sortDirectionList)
    tell application "Keynote"
      set aVer to version
      
if aVer < "12.0" then return
      
      
tell front document
        set posList to {}
        
set aCount to 1
        
        
repeat with ii in aaSel
          set jj to contents of ii
          
set bClass to class of jj
          
          
tell jj
            set {posX, posY} to position
            
            
try
              set tmpStr to object text as string
            on error
              set tmpStr to ""
            end try
          end tell
          
          
set the end of posList to {positionX:posX, positionY:posY, objID:aCount, myStr:tmpStr}
          
          
set aCount to aCount + 1
        end repeat
        
        
–座標データをもとにソート
        
set sortedList to sortRecListByLabel(posList, sortLabelLIst, sortDirectionList) of me
        
        
–データを返す配列にiWork Object への参照を含める
        
set sCount to 1
        
repeat with i from 1 to (length of aaSel)
          set tmpID to objID of contents of item i of sortedList
          
set tmpObj to contents of item tmpID of aaSel
          
set (item tmpID of sortedList) to (item tmpID of sortedList) & {objRef:tmpObj}
        end repeat
      end tell
    end tell
    
    
return sortedList
  end sortIWorkObjectsByPositionIncludingObjRef
  
  
  
–Keynote上のiWork ObjをXY座標でソートして結果を返す(App Obj入りのリストを返す)
  
on sortIWorkObjectsByPositionAndRetObjRef(aaSel, sortLabelLIst, sortDirectionList)
    tell application "Keynote"
      set aVer to version
      
if aVer < "12.0" then return
      
      
tell front document
        set posList to {}
        
set aCount to 1
        
        
repeat with ii in aaSel
          set jj to contents of ii
          
set bClass to class of jj
          
          
tell jj
            set {posX, posY} to position
            
            
try
              set tmpStr to object text as string
            on error
              set tmpStr to ""
            end try
          end tell
          
          
set the end of posList to {positionX:posX, positionY:posY, objID:aCount, myStr:tmpStr}
          
          
set aCount to aCount + 1
        end repeat
        
        
–座標データをもとにソート
        
set sortedList to sortRecListByLabel(posList, sortLabelLIst, sortDirectionList) of me
        
        
–データを返す配列にiWork Object への参照を含める
        
set sCount to 1
        
set retList to {}
        
repeat with i from 1 to (length of aaSel)
          set tmpID to objID of contents of item i of sortedList
          
set tmpObj to contents of item tmpID of aaSel
          
set the end of retList to tmpObj
        end repeat
      end tell
    end tell
    
    
return retList
  end sortIWorkObjectsByPositionAndRetObjRef
  
  
  
–リストに入れたレコードを、指定の属性ラベルの値でソート
  
on sortRecListByLabel(aRecList as list, aLabelStr as list, ascendF as list)
    set aArray to current application’s NSArray’s arrayWithArray:aRecList
    
    
set aCount to length of aLabelStr
    
set sortDescArray to current application’s NSMutableArray’s new()
    
repeat with i from 1 to aCount
      set aLabel to (item i of aLabelStr)
      
set aKey to (item i of ascendF)
      
set sortDesc to (current application’s NSSortDescriptor’s alloc()’s initWithKey:aLabel ascending:aKey)
      (
sortDescArray’s addObject:sortDesc)
    end repeat
    
    
return (aArray’s sortedArrayUsingDescriptors:sortDescArray) as list
  end sortRecListByLabel
  
end script

★Click Here to Open This Script 

More from my site

  • 執筆中:Cocoa Scripting Course #10 NSAttributedString執筆中:Cocoa Scripting Course #10 NSAttributedString
  • Cocoa Scripting Course用、タイトルの丸つき数字から表を編集 v2(複数スライド選択用)Cocoa Scripting Course用、タイトルの丸つき数字から表を編集 v2(複数スライド選択用)
  • Cocoa Scripting Course #9, File Processingを刊行Cocoa Scripting Course #9, File Processingを刊行
  • Cocoa Scripting Course #8, File path Processingを刊行Cocoa Scripting Course #8, File path Processingを刊行
  • Cocoa Scripting Course 続刊計画Cocoa Scripting Course 続刊計画
  • Cocoa Scripting Course #7, NSColorを刊行Cocoa Scripting Course #7, NSColorを刊行
(Visited 1 times, 1 visits today)
Posted in list Sort | Tagged 13.0savvy 14.0savvy 15.0savvy Keynote | Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

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

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • CotEditorで2つの書類の行単位での差分検出
  • macOS 15, Sequoia
  • 指定のWordファイルをPDFに書き出す
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • Adobe AcrobatをAppleScriptから操作してPDF圧縮
  • Numbersで選択範囲のセルの前後の空白を削除
  • メキシカンハットの描画
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AdobeがInDesign v19.4からPOSIX pathを採用
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • Cocoa Scripting Course 続刊計画
  • AppleScriptによる並列処理
  • macOS 14.xでScript Menuの実行速度が大幅に下がるバグ
  • NaturalLanguage.frameworkでNLEmbeddingの処理が可能な言語をチェック
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • Keynote/Pagesで選択中の表カラムの幅を均等割
  • Keynote、Pages、Numbers Ver.14.0が登場
  • macOS 15 リモートApple Eventsにバグ?
  • デフォルトインストールされたフォント名を取得するAppleScript

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1392) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (190) 14.0savvy (142) 15.0savvy (120) CotEditor (66) Finder (51) iTunes (19) Keynote (116) 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 (54) 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
  • 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年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