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書類上の選択中の2つのテキストアイテムで、左を比較元、右を比較先として行単位の差分を比較先に赤くマーク

Posted on 11月 21 by Takaaki Naganoya

Keynote書類上で2つのテキストアイテムを選択し、左を比較元、右を比較先として行単位で差分を計算し、変更や追加が行われた行を赤く着色するAppleScriptです。

ほとんどの部分をChatGPTに書かせていますが、相当にやりとりしてダメ出ししないと書いてくれません。ほかにも、2D Bin Packing(任意の矩形図形xn個を指定エリアに詰め込む演算)のプログラムなどもAppleScriptで実装し直しておきたいところです。


▲Keynote書類上で2つのテキストアイテムを選択。座標値から左右を判定し、左側を比較元、右側を比較先として処理する


▲変更や新規追加のあった行を赤く着色する

こういうScriptを整備していないと、作業の負荷を下げられなくて大変です。

AppleScript名:Keynote書類上の選択中の2つのテキストアイテムで、左を比較元、右を比較先として行単位の差分を比較先に赤くマーク.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/11/21
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

— Keynote 側の処理:選択順で source / target を決め、target 側の差分行を赤くする
tell application "Keynote"
  tell front document
    set selItems to selection
    
if (count of selItems) is not 2 then
      display dialog "2つのテキストボックスを選択してください。(選択順が重要です)" buttons {"OK"} default button 1
      
return
    end if
    
    
set sourceTI to item 1 of selItems — 基準
    
set targetTI to item 2 of selItems — 変更を反映する対象
    
    
if (class of sourceTI) is not text item or (class of targetTI) is not text item then
      display dialog "選択されたオブジェクトがテキストボックスではありません。" buttons {"OK"} default button 1
      
return
    end if
    
    
–X座標値が小さい(左に存在する)ものを比較元とする
    
set {sPosX, sPosY} to position of sourceTI
    
set {tPosX, tPosY} to position of targetTI
    
if tPosX < sPosX then copy {sourceTI, targetTI} to {targetTI, sourceTI}
    
    
set sourceText to (object text of sourceTI) as string
    
set targetText to (object text of targetTI) as string
  end tell
end tell

set sourceLines to paragraphs of sourceText
set targetLines to paragraphs of targetText

— LCS を計算して target 側で「保持すべき行」のインデックスを決める
set pairs to computeLCS(sourceLines, targetLines)
set keepIdx to {}
repeat with p in pairs
  set end of keepIdx to item 2 of p — pair is {i, j} -> keep j (target index)
end repeat

— target の全テキストを一旦黒に戻す
tell application "Keynote"
  tell front document
    tell object text of targetTI
      set its color to {0, 0, 0}
    end tell
  end tell
end tell

— keepIdx に含まれない target の行を赤色にする
repeat with idx from 1 to (count targetLines)
  if keepIdx does not contain idx then
    set thisLine to item idx of targetLines
    
set startPos to offsetOfLine(idx, targetText)
    
set endPos to startPos + (length of thisLine)
    
if endPos ≥ startPos then
      tell application "Keynote"
        tell front document
          tell object text of targetTI
            set color of characters startPos thru endPos to {65535, 0, 0}
          end tell
        end tell
      end tell
    end if
  end if
end repeat

— LCS(最長共通部分列)を AppleScript のリストで実装(行単位)
— 戻り値: {{i1, j1}, {i2, j2}, …} という形式のペアリスト(1-based indices)
on computeLCS(listA, listB)
  set lenA to count listA
  
set lenB to count listB
  
  
— DP テーブル初期化: (lenA+1) 行 × (lenB+1) 列、全て 0
  
set dp to {}
  
repeat with r from 0 to lenA
    set row to {}
    
repeat with c from 0 to lenB
      set end of row to 0
    end repeat
    
set end of dp to row
  end repeat
  
  
— DP 計算(注意:dp の行は 0..lenA を 1..(lenA+1) 順で保持)
  
repeat with i from 1 to lenA
    
    
repeat with j from 1 to lenB
      set row_im1 to item i of dp — dp[i-1]
      
set row_i to item (i + 1) of dp — dp[i]
      
      
if (item i of listA) is equal to (item j of listB) then
        — dp[i][j] = dp[i-1][j-1] + 1
        
set prevVal to item j of row_im1 — dp[i-1][j-1]
        
set newVal to prevVal + 1
        
set item (j + 1) of row_i to newVal — store into dp[i][j]
        
        
— 更新した row_i を dp に戻す
        
set item (i + 1) of dp to row_i
      else
        
        
— dp[i][j] = max( dp[i-1][j], dp[i][j-1] )
        
set valUp to item (j + 1) of row_im1 — dp[i-1][j]
        
set valLeft to item j of row_i — dp[i][j-1]
        
        
if valUp ≥ valLeft then
          set item (j + 1) of row_i to valUp
        else
          set item (j + 1) of row_i to valLeft
        end if
        
set item (i + 1) of dp to row_i
        
      end if
    end repeat
  end repeat
  
  
— LCS を復元(後ろ向きに辿る)
  
set i to lenA
  
set j to lenB
  
set lcsPairs to {}
  
  
repeat while (i > 0 and j > 0)
    if (item i of listA) is equal to (item j of listB) then
      — 一致ペアを先頭に追加({{i, j}} & lcsPairs)
      
set lcsPairs to ({{i, j}} & lcsPairs)
      
set i to i – 1
      
set j to j – 1
    else
      — dp[i-1][j] と dp[i][j-1] を比較して移動方向を決める
      
set valUp to item (j + 1) of item i of dp — dp[i-1][j]
      
set valLeft to item j of item (i + 1) of dp — dp[i][j-1]
      
      
if valUp ≥ valLeft then
        set i to i – 1
      else
        set j to j – 1
      end if
      
    end if
  end repeat
  
  
return lcsPairs
end computeLCS

— 行の開始オフセット(文字単位、1-based)
on offsetOfLine(n, fullText)
  if n ≤ 1 then return 1
  
set paras to paragraphs of fullText
  
set pos to 1
  
repeat with idx from 1 to (n – 1)
    set pos to pos + (length of (item idx of paras)) + 1 — 改行分 +1
  end repeat
  
return pos
end offsetOfLine

★Click Here to Open This Script 

(Visited 5 times, 5 visits today)
Posted in Object control Text | Tagged 15.0savvy 26.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

  • macOS 26, Tahoe
  • KagiのWebブラウザ、Orion
  • Script Debuggerの開発と販売が2025年に終了
  • macOS 15 リモートApple Eventsにバグ?
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • NSObjectのクラス名を取得 v2.1
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • 2024年に書いた価値あるAppleScript
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行
  • (確認中)AppleScript Dropletのバグっぽい動作が解消?
  • AppleScript Dropletのバグっぽい動作が「復活」(macOS 15.5β)
  • macOS 26, 15.5でShortcuts.app「AppleScriptを実行」アクションのバグが修正される
  • Script Debuggerがフリーダウンロードで提供されることに
  • 指定フォルダ以下の画像のMD5チェックサムを求めて、重複しているものをピックアップ
  • Apple、macOS標準搭載アプリ「写真」のバージョン表記を間違える
  • 執筆中:AppleScript最新リファレンスver2.8対応(macOS 15対応アップデート)
  • Keynoteで選択中のtext itemの冒頭のフォントを太くする v2
  • macOS 15.5beta5(24F74)でaliasのキャスティングバグが修正された???
  • 複数の重複検出ルーチンを順次速度計測
  • 余白トリミング実験 v3

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (204) 14.0savvy (159) 15.0savvy (159) 26.0savvy (24) CotEditor (66) Finder (52) Keynote (120) 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 (56) 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
  • Newt On Project
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • 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
  • Scripting Additions
  • 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年11月
  • 2025年10月
  • 2025年9月
  • 2025年8月
  • 2025年7月
  • 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