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

iWork Appsがバージョン12.0にアップデートでselection動作正常化

Posted on 4月 9, 2022 by Takaaki Naganoya

昨日、iWork Apps(Keynote、Pages、Numbers)がVer.12.0にバージョンアップしていました。

AppleScript系ではgetコマンドが追加(Keynote、Pages)されたぐらいですが、これによって「selection」の動作がまともになりました。

前バージョンのアップデートで「スライド単位の選択」がselectionで取れなくなったことで、「何か変更しようとしている最中なんだろう」とは思っていました。予想どおりです。selection動作の空白期間を置かず、前バージョンの段階で実装されていたらもっとよかったのですが。

AppleScript対応アプリケーション、スクリプタブルなアプリケーションの世界観は、小さい世界観のものから大きい世界観のものまでいろいろです。

小さい世界観1(一番小さい):
アプリケーション起動、バージョン確認、書類のオープン、書類の印刷、アプリケーションの設定情報の確認

小さい世界観2:
小さい世界観1+アプリケーション固有のコマンドの実行

小さい世界観3:
小さい世界観2+書類やデータ作成のための機能、選択中のデータをおおまかに取得する機能
Keynote v11.xはここ

小さい世界観4:
小さい世界観3+書類上のオブジェクト作成/改変機能、選択中のデータやオブジェクトへの参照を取得する機能
Keynote v12.xはここ? まだ、Chart作成などで未サポートのグラフ形式などもあり、不完全

大きい世界観:
小さい世界観4+書類上のオブジェクトの多数を作成/改変する機能
Adobe InDesign、Word、Excel、PowerPoint、Pixelmator Proなど。かつての(v5ぐらいの)Pagesはここにいた

Keynoteでselectionを試してみたところ、何か書類上のオブジェクトを選択してselectionを実行すると、オブジェクトへの参照を(複数、リスト形式で)取れます。オブジェクトを選択した状態でなければ、書類上のどのスライドを表示中かというスライドへの参照が取得できるようです。

Keynoteにおける処理の自由度は大幅に上がりました。選択中のテキストボックスを取得できれば、その中に含まれるテキストなり書式つきテキストを取得して、書き直し、元のテキストボックスに書き戻すことができます。

選択中のイメージを取得できれば、その画像のパスを求めるかあるいはファイル名を求めて、オリジナルの画像ファイル(書類内に格納された画像、外部に存在する画像かはやってみないと)を画像フィルタ処理などを行って書き戻すといった処理が行いやすくなります。以前までのバージョンでは、選択した画像をいったんコピーしてクリップボードに格納し、そのクリップボード内の画像に対して画像処理していました。

Pagesでも、さまざまなオブジェクトへの参照がselectionによって取得できますが、あいかわらず「ページ」という概念があるんだかないんだか不明で、アプリケーションの根幹にかかわる部分なのでいまひとつ気軽に言えないところではあります。

Posted in news | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote Numbers Pages | Leave a comment

Keynoteの最前面の書類の現在のスライド上の表オブジェクトの重なり合い(2つ以上対応)を検出 v3

Posted on 4月 5, 2022 by Takaaki Naganoya

Keynote書類の現在のスライド(ページ)上に存在する表オブジェクトの重なり合いを検出するAppleScriptです。

前バージョンでは、複数の表オブジェクトが配置されていると、表オブジェクトの位置情報と大きさから生成したNSRect同士の重なりチェックを行なった際に、同じ表同士の重なりチェックも行なっていたので、

複数の表が存在していると、もれなく「重なっている」という警告が出てくるものでした。

本バージョンでは、同じ表同士を重なりチェックしないようにとか、1×3と3×1という「同じオブジェクト同士の重なりチェックを2度行っている」ようなものを排除しています。

今度の順列組み合わせパターン生成ルーチン「retPairPermutationsWithoutSelfCollision」では、これらのパターンを除去しています。

{{1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}, {3, 4}, {3, 5}, {4, 5}}

↑余計な組み合わせパターンは生成されなくなりました。

実際に本Scriptを用いて(複数ページ対応板)、121ページ分の表の重なり合わせチェックを行なってみたところ、M1 Mac miniで3.4秒程度です。

–> Watch Demo Movie


▲重なり合わせ判定:なし


▲重なり合わせ判定:あり


▲重なり合わせ判定:なし


▲重なり合わせ判定:あり(400%拡大表示)


▲重なり合わせ判定:あり(75%拡大表示)

本Scriptの完全版を「AppleScript基礎テクニック集(1)〜間接指定」に収録しています。

AppleScript名:現在のスライド上の表オブジェクトの重なり合い(2つ以上対応)を検出 v3.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/04/03
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "Keynote"
  tell front document
    set docHeight to height
    
    
tell current slide
      set tList to every table
      
      
if length of tList < 2 then return false –複数の「表」がなかったら衝突は発生していない
      
      
–表の座標とサイズによりNSRectangleを作成する
      
set bList to {}
      
      
repeat with i in tList
        set j to contents of i
        
        
tell j
          set {xPos, yPos} to position
          
set aWidth to width
          
set aHeight to height
          
          
set aRect to makeRect(xPos, yPos, aWidth, aHeight) of me
        end tell
        
        
set the end of bList to aRect
      end repeat
      
      
–作成したNSRectangleの順列組み合わせペア・パターンを作成して、NSRectangle同士の衝突判定を順次行う
      
set aLen to length of bList
      
set permuList to retPairPermutationsWithoutSelfCollision(aLen) of me
      
set corruptList to {}
      
      
repeat with i in permuList
        copy i to {i1, i2}
        
set rectRes to detectRectanglesCollision(item i1 of bList, item i2 of bList) of me
        
if rectRes = true then return true –表同士の衝突が発生している
      end repeat
      
      
return false –表同士の衝突は発生していない
    end tell
  end tell
end tell

on makeRect(xPos, yPos, aWidth, aHeight)
  return current application’s NSMakeRect(xPos, yPos, aWidth, aHeight)
end makeRect

–NSRect同士の衝突判定
on detectRectanglesCollision(aRect, bRect)
  set a1Res to (current application’s NSIntersectionRect(aRect, bRect)) as {record, list}
  
set tmpClass to class of a1Res
  
  
if tmpClass = record then
    –macOS 10.10, 10.11, 10.12
    
return not (a1Res = {origin:{x:0.0, y:0.0}, |size|:{width:0.0, height:0.0}})
  else if tmpClass = list then
    –macOS 10.13 or later
    
return not (a1Res = {{0.0, 0.0}, {0.0, 0.0}})
  end if
end detectRectanglesCollision

–1から任意の数までのアイテムで、2個ずつの組み合わせのすべての順列組み合わせパターンを計算して返す
–ただし、1×1とか2×2などの同一アイテム同士の掛け合わせを除去するものとする
on retPairPermutationsWithoutSelfCollision(aMax)
  set aList to {}
  
  
repeat with x from 1 to aMax
    repeat with xx from 1 to aMax
      if x is not equal to xx then
        
        
set tmpItem to {x, xx}
        
set a2List to sort1DNumList(tmpItem, true) of me
        
        
if {a2List} is not in aList then
          set the end of aList to a2List
        end if
      end if
      
    end repeat
  end repeat
  
  
return aList
end retPairPermutationsWithoutSelfCollision

–1D List(数値)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート
on sort1DNumList(theList as list, aBool as boolean)
  tell current application’s NSSet to set theSet to setWithArray_(theList)
  
tell current application’s NSSortDescriptor to set theDescriptor to sortDescriptorWithKey_ascending_(missing value, aBool)
  
set sortedList to theSet’s sortedArrayUsingDescriptors:{theDescriptor}
  
return (sortedList) as list
end sort1DNumList

★Click Here to Open This Script 

Posted in list | Tagged 12.0savvy Keynote NSRect | 1 Comment

Keynoteの最前面の書類の現在のスライドで、表オブジェクトの重なり合い(2つ以上対応)を検出 v2

Posted on 4月 3, 2022 by Takaaki Naganoya

Keynote書類の現在のスライド(ページ)上に存在する表オブジェクトの重なり合いを検出するAppleScriptです。

Cocoa Scripting Course #4のチェックを行なっていたら、表オブジェクトが重なり合っていたページがあってヘコみました(再チェック中です)。

これを自動でチェックするためのAppleScriptを書いてみました。とりあえず、現在表示しているスライド(ページ)を対象に。最終的には、開始と終了のスライドの番号を与えておくと、その間のスライドをすべてチェックできるとよいでしょう。

CocoaのNSRect同士であれば、重なり合いを検出できるので大変便利です。Keynote上の表オブジェクトから、位置情報と大きさの情報を取得すれば、簡単に目的のデータ(配列に入れたNSRect)を作れることでしょう。

ただし、NSIntersectionRectでNSRect同士の衝突検出を行えるとしても、1対1でしかチェックできないようなので、検出された表同士のすべての衝突チェック組み合わせを行うべく、順列組み合わせペア・パターンを計算する部品を急遽作って(以前作った、Permutation計算とは仕様が違うので)、2つずつ表(から作ったNSRect)を取り出して矩形座標の衝突判定を行なっています。

Keynoteの座標系は「左上」が原点座標で、Cocoaの座標系は左下のはずで….macOS 12で左上になったという話もありますが、実戦投入してみたら衝突が発生していないスライドでも衝突が検出されていたので、このあたり修正する必要があるかもしれません。

Keynoteのスライド上の表の配置状況をCocoaのNSRectで表現し、デバッグ用に画像で表示させてみました。重なりをシミュレーションするだけなら、とくに問題はなさそうです。

→ 原因がわかりました。表の座標とサイズから作ったNSRect同士を重なり合い計算するための、順列組み合わせパターン計算ルーチンで1×1とか2×2とかの「同じNSRect同士」の重なりを計算するようなパターンまで出力していたのが原因です。

つまり、現状では「複数の表オブジェクトが入っているスライド」を検出するだけの計算を行なってしまっているようです。

--> {{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}, {5, 1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}}

と計算してしまうところを、

--> {{1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 1}, {2, 3}, {2, 4}, {2, 5}, {3, 1}, {3, 2}, {3, 4}, {3, 5}, {4, 1}, {4, 2}, {4, 3}, {4, 5}, {5, 1}, {5, 2}, {5, 3}, {5, 4}}

のように、同じアイテム同士の組み合わせになるパターンだけを除外してあげればよかったわけです。自分で資料を作ってみてわかりました。

あとは、3×5と5×3は計算としては同じ意味なので、こういう重複パターンを除去してあげる必要があるのですが…重なりの「個数」を数えるのであるのであればともかく、重なりを「検出」するだけなら重複除去まではする必要がないでしょう。

AppleScript名:現在のスライド上の表オブジェクトの重なり合い(2つ以上対応)を検出 v2.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2022/04/03
—
–  Copyright © 2022 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "Keynote"
  tell front document
    tell current slide
      set tList to every table
      
      
if length of tList < 2 then return false –複数の「表」がなかったら衝突は発生していない
      
      
–表の座標とサイズによりNSRectangleを作成する
      
set bList to {}
      
      
repeat with i in tList
        set j to contents of i
        
        
tell j
          set {xPos, yPos} to position
          
set aWidth to width
          
set aHeight to height
          
set aRect to makeRect(xPos, yPos, aWidth, aHeight) of me
        end tell
        
        
set the end of bList to aRect
      end repeat
      
      
–作成したNSRectangleの順列組み合わせペア・パターンを作成して、NSRectangle同士の衝突判定を順次行う
      
set aLen to length of bList
      
set permuList to retPairPermutations(aLen) of me
      
set corruptList to {}
      
      
repeat with i in permuList
        copy i to {i1, i2}
        
set rectRes to detectRectanglesCollision(item i1 of bList, item i2 of bList) of me
        
if rectRes = true then return true –表同士の衝突が発生している
      end repeat
      
      
return false –表同士の衝突は発生していない
    end tell
  end tell
end tell

on makeRect(xPos, yPos, aWidth, aHeight)
  return current application’s NSMakeRect(xPos, yPos, aWidth, aHeight)
end makeRect

–NSRect同士の衝突判定
on detectRectanglesCollision(aRect, bRect)
  set a1Res to (current application’s NSIntersectionRect(aRect, bRect)) as {record, list}
  
set tmpClass to class of a1Res
  
  
if tmpClass = record then
    –macOS 10.10, 10.11, 10.12
    
return not (a1Res = {origin:{x:0.0, y:0.0}, |size|:{width:0.0, height:0.0}})
  else if tmpClass = list then
    –macOS 10.13 or later
    
return not (a1Res = {{0.0, 0.0}, {0.0, 0.0}})
  end if
end detectRectanglesCollision

–1から任意の数までのアイテムで、2個ずつの組み合わせのすべての順列組み合わせパターンを計算して返す
on retPairPermutations(aMax)
  set aList to {}
  
  
repeat with x from 1 to aMax
    repeat with xx from 1 to aMax
      set tmpItem to {x, xx}
      
if tmpItem is not in aList then
        set the end of aList to tmpItem
      end if
    end repeat
  end repeat
  
  
return aList
end retPairPermutations

★Click Here to Open This Script 

Posted in list | Tagged 12.0savvy Keynote NSRect | 1 Comment

Keynoteの表の選択中のセルに入っている丸つき数字をインクリメント/デクリメント

Posted on 12月 16, 2021 by Takaaki Naganoya

Keynoteの最前面の書類の現在のスライド上にある表の選択中のセルに入っている丸つき数字(①②③…… )をインクリメント(+1)、デクリメント(ー1)するAppleScriptです。

–> Watch Demo Movie

書籍などに掲載する資料で、参照番号をいじくるツールがどうしても必要になって作ったものです。

macOS標準搭載のスクリプトメニューに入れて実行することを前提にしています。丸つき数字は、

①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿

のみを前提にして処理しています。その他の、

❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴
➀➁➂➃➄➅➆➇➈➉
➊➋➌➍➎➏➐➑➒➓
⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾

については無視しています。

Keynote v11.2+macOS 12.1betaで作成&動作確認していますが、Keynoteのバージョンにはとくに(バグのあるバージョンでなれけば)依存している機能はありません。


▲選択範囲のセルに入っている丸つき数字のインクリメント(①②③→②③④)


▲選択範囲のセルに入っている丸つき数字のデクリメント(②③④→①②③)

AppleScript名:選択中の表の指定行・列のマル付き数字のインクリメント(+1).scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/16
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7" — macOS 10.13 or later
use framework "Foundation"
use scripting additions

–現在選択中の表オブジェクトを取得
set curTable to returnSelectedTableOnCurrentSlide() of me
if curTable = false then return

–現在選択中の表オブジェクト中の選択範囲中のセルをすべて取得(1D List)
using terms from application "Keynote"
  tell curTable
    set cellList to every cell of selection range
  end tell
end using terms from

–list中の各アイテムの冒頭に順次丸つき数字を追加する
using terms from application "Keynote"
  tell curTable
    repeat with i in cellList
      –選択範囲のセルの値を取り出す
      
set aVal to value of i
      
set vList to characters of (aVal as string)
      
      
–1文字ずつに分割してループ
      
set tmpOut to ""
      
repeat with ii in vList
        set jj to contents of ii
        
        
–丸付き数字の検出
        
set tmpR to holdNumberWithSignOnly(jj) of me
        
if tmpR is equal to jj then
          –丸付き数字を数値にデコード
          
set tmpNum to decodeNumFromNumWithSign(tmpR) of me
          
          
–数値をインクリメント
          
if tmpNum ≤ 50 then
            set tmpNum to tmpNum + 1 –インクリメント
          end if
          
          
–数値を丸付き数字にエンコード
          
set tmpR to convNumToNumWithSign(tmpNum) of me
          
set tmpOut to tmpOut & tmpR
        else
          set tmpOut to tmpOut & jj
        end if
      end repeat
      
      
set value of i to tmpOut
    end repeat
  end tell
end using terms from

–1~50の範囲の数値を丸つき数字に変換して返す
on convNumToNumWithSign(aNum as number)
  if (aNum ≤ 0) or (aNum ≥ 50) then return ""
  
set aStr to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set bChar to character aNum of aStr
  
return bChar
end convNumToNumWithSign

–指定文字列から丸つき数字のみ抽出する
on holdNumberWithSignOnly(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[^\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end holdNumberWithSignOnly

–丸つき数字を数値にデコードする v2(簡略版)
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
  
if numStr1 contains aStr then
    using terms from scripting additions
      set bNum to offset of aStr in numStr1
    end using terms from
    
return bNum
  end if
  
return false
end decodeNumFromNumWithSign

–現在のスライド上で選択中の表オブジェクトへの参照を取得する
on returnSelectedTableOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
return theTable
        
      end tell
    end tell
  end tell
end returnSelectedTableOnCurrentSlide

★Click Here to Open This Script 

AppleScript名:選択中の表の指定行・列のマル付き数字のデクリメント(ー1).scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/16
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7" — macOS 10.13 or later
use framework "Foundation"
use scripting additions

–現在選択中の表オブジェクトを取得
set curTable to returnSelectedTableOnCurrentSlide() of me
if curTable = false then return

–現在選択中の表オブジェクト中の選択範囲中のセルをすべて取得(1D List)
using terms from application "Keynote"
  tell curTable
    set cellList to every cell of selection range
  end tell
end using terms from

–list中の各アイテムの冒頭に順次丸つき数字を追加する
using terms from application "Keynote"
  tell curTable
    repeat with i in cellList
      –選択範囲のセルの値を取り出す
      
set aVal to value of i
      
set vList to characters of (aVal as string)
      
      
–1文字ずつに分割してループ
      
set tmpOut to ""
      
repeat with ii in vList
        set jj to contents of ii
        
        
–丸付き数字の検出
        
set tmpR to holdNumberWithSignOnly(jj) of me
        
if tmpR is equal to jj then
          –丸付き数字を数値にデコード
          
set tmpNum to decodeNumFromNumWithSign(tmpR) of me
          
          
–数値をデクリメント
          
if tmpNum > 1 then
            set tmpNum to tmpNum – 1 –デクリメント
          end if
          
          
–数値を丸付き数字にエンコード
          
set tmpR to convNumToNumWithSign(tmpNum) of me
          
set tmpOut to tmpOut & tmpR
        else
          set tmpOut to tmpOut & jj
        end if
      end repeat
      
      
set value of i to tmpOut
    end repeat
  end tell
end using terms from

–1~50の範囲の数値を丸つき数字に変換して返す
on convNumToNumWithSign(aNum as number)
  if (aNum ≤ 0) or (aNum ≥ 50) then return ""
  
set aStr to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set bChar to character aNum of aStr
  
return bChar
end convNumToNumWithSign

–指定文字列から丸つき数字のみ抽出する
on holdNumberWithSignOnly(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[^\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end holdNumberWithSignOnly

–丸つき数字を数値にデコードする v2(簡略版)
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
  
if numStr1 contains aStr then
    using terms from scripting additions
      set bNum to offset of aStr in numStr1
    end using terms from
    
return bNum
  end if
  
return false
end decodeNumFromNumWithSign

–現在のスライド上で選択中の表オブジェクトへの参照を取得する
on returnSelectedTableOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
return theTable
        
      end tell
    end tell
  end tell
end returnSelectedTableOnCurrentSlide

★Click Here to Open This Script 

Posted in list Text | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote | Leave a comment

選択中の表の指定行・列のマル付き数字リナンバー v3

Posted on 12月 4, 2021 by Takaaki Naganoya

Keynoteの現在オープン中の書類の、現在表示中のスライド(ページ)上にある、選択中の表オブジェクトの、選択中のセルに入っているマルつき数字(①②③….)を左から順番にリナンバーするAppleScriptです。

以前のバージョン(v2)では処理対象行や列をダイアログ上で選択するようになっていましたが、本バージョンではKeynote上で処理対象のセルを選択しておくことで処理範囲をScript側に知らせるようにしました。

選択範囲のセルのデータを取得し、それらに入っているマルつき数字の最小値を計算し、ユーザーに開始値としてダイアログで確認を行います。前バージョンと違ってユーザーに確認するのはこの点だけです。

すぐに選択範囲のセルのテキスト冒頭に入っているマルつき数字をリナンバーします。本Scriptにおけるマルつき数字の範囲は1から50です(白地のマルつき数字に限る)。

AppleScript名:選択中の表の指定行・列のマル付き数字リナンバー v3.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/03
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7" — macOS 10.13 or later
use framework "Foundation"
use scripting additions

set aOffset to 0

–現在選択中の表オブジェクトを取得
set curTable to returnSelectedTableOnCurrentSlide() of me
if curTable = false then return

–現在選択中の表オブジェクト中の選択範囲(range)を取得
set aRange to returnSelectedRangeOnCurrentSlide() of me
if aRange = false then
  display dialog ""
  
return
end if

–現在選択中の表オブジェクト中の選択範囲中のセルをすべて取得(1D List)
using terms from application "Keynote"
  tell curTable
    set cellList to every cell of range aRange
  end tell
end using terms from

–現在選択中の表オブジェクト中の選択範囲中のセルの値をすべて取得(1D List)
set aList to returnSelectedTableCellDataOnCurrentSlide() of me
set bList to cleanUp1DList(aList, missing value) of me

—データ中に丸つき数字が存在した場合には、最小のものを取得
set aOffset to (getMinimumNumFromNumberWithSign(bList) of me)

–一応、ユーザーに推測した開始値でよいか確認を取る
set dRes to text returned of (display dialog "丸つき数字の開始値:" default answer (aOffset as string))
try
  set aOffset to (dRes as number) – 1
on error
  set aOffset to 0
end try

–Keynoteの表のセルから取得したデータから丸つき数字を除去する
set cList to removeNumberWithSignFromList(bList) of me

–list中の各アイテムの冒頭に順次丸つき数字を追加する
set dList to {}
set aCount to 1

repeat with i in cList
  set j to convNumToNumWithSign(aCount + aOffset) of me
  
set jj to contents of i
  
  
set the end of dList to (j & jj)
  
  
set aCount to aCount + 1
end repeat

–リナンバー対象のセルをリナンバーした丸つき数字+内容で置換
set aCount to 1
using terms from application "Keynote"
  tell curTable
    repeat with i in cellList
      set value of i to contents of item aCount of dList
      
set aCount to aCount + 1
    end repeat
  end tell
end using terms from

–1~50の範囲の数値を丸つき数字に変換して返す
on convNumToNumWithSign(aNum as number)
  if (aNum ≤ 0) or (aNum > 50) then return ""
  
set aStr to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set bChar to character aNum of aStr
  
return bChar
end convNumToNumWithSign

–1D List上で指定データを検索してヒットしたアイテム番号を返す
on search1DList(aList, aTarg)
  set anArray to current application’s NSMutableArray’s arrayWithArray:aList
  
set anIndex to anArray’s indexOfObject:aTarg
  
if (anIndex = current application’s NSNotFound) or (anIndex > 9.99999999E+8) then
    return false
  end if
  
return (anIndex as integer) + 1 –convert index base (0 based to 1 based)
end search1DList

–1D listのクリーニング
on cleanUp1DList(aList as list, cleanUpItems as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
if j is not in cleanUpItems then
      set the end of bList to j
    else
      set the end of bList to ""
    end if
  end repeat
  
return bList
end cleanUp1DList

–text in listから丸つき数字を除去する
on removeNumberWithSignFromList(aList as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
set j2 to removeNumberWithSign(j) of me
    
set the end of bList to j2
  end repeat
  
return bList
end removeNumberWithSignFromList

–文字列から丸つき数字を除去する
on removeNumberWithSign(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end removeNumberWithSign

–1D Listに入っているテキストから丸つき数字を抽出して数値化し、最小のものを求める
on getMinimumNumFromNumberWithSign(aList)
  set nList to {}
  
  
repeat with i in aList
    set j to contents of i
    
–与えられたテキストのうち、丸つき数字(白)の
    
set j2 to holdNumberWithSignOnly(j) of me
    
set n2List to characters of j2 –複数の丸つき数字が入っている場合に対処
    
    
repeat with ii in n2List
      set jj to contents of ii
      
set tmpNum to decodeNumFromNumWithSign(jj) of me
      
set the end of nList to tmpNum
    end repeat
    
  end repeat
  
  
set anArray to current application’s NSArray’s arrayWithArray:nList
  
set cRes to (anArray’s valueForKeyPath:"@min.self") as integer
  
return cRes
end getMinimumNumFromNumberWithSign

–指定文字列から丸つき数字のみ抽出する
on holdNumberWithSignOnly(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[^\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end holdNumberWithSignOnly

–丸つき数字を数値にデコードする v2
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set numStr2 to "❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴"
  
set numStr3 to "➀➁➂➃➄➅➆➇➈➉"
  
set numStr4 to "➊➋➌➍➎➏➐➑➒➓"
  
set numStr5 to "⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾"
  
  
set nList to {numStr1, numStr2, numStr3, numStr4, numStr5}
  
  
repeat with i in nList
    set numTemp to contents of i
    
if numTemp contains aStr then
      using terms from scripting additions
        set bNum to offset of aStr in numTemp
      end using terms from
      
return bNum
    end if
  end repeat
  
return false
end decodeNumFromNumWithSign

–現在のスライド上で選択中の表オブジェクトへの参照を取得する
on returnSelectedTableOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
return theTable
        
      end tell
    end tell
  end tell
end returnSelectedTableOnCurrentSlide

–現在のスライド上で選択中の表オブジェクト内の選択中のセルの値を取得する
on returnSelectedTableCellDataOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
tell theTable
          set vList to value of every cell of selection range
          
set cCount to count of column of selection range
          
set rCount to count of row of selection range
          
          
if rCount > 1 then return false –複数行選択されていた場合にはエラーを返す
          
          
return vList
        end tell
      end tell
    end tell
  end tell
end returnSelectedTableCellDataOnCurrentSlide

–現在のスライド上で選択中の表オブジェクト内の選択中の範囲(range)のnameを取得する
on returnSelectedRangeOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
tell theTable
          set aPref to properties of selection range
          
set aRangeStr to name of aPref
          
return aRangeStr
        end tell
      end tell
    end tell
  end tell
end returnSelectedRangeOnCurrentSlide

★Click Here to Open This Script 

Posted in list Number | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote | Leave a comment

選択中の表の指定行・列のマル付き数字リナンバー v2

Posted on 11月 27, 2021 by Takaaki Naganoya

Keynote書類の現在表示中のスライド(ページ)中の選択中の「表」において、指定の行、指定のヘッダー列以降のセルに対して、(1)丸つき数字の削除 (2)丸つき数字の最小値の計算 を行い、丸つき数字の番号降り直しの開始値をユーザーに確認したうえで、丸つき数字部分のみのリナンバーを行います。

本ScriptはmacOS 12+Keynote v11.2で作成・動作確認を行なってあります。それほどOSおよびKeynoteのバージョンに依存するような処理は行なっていないため、上記以外のOSおよびKeynoteのバージョンの組み合わせでも動くと思います。

–> Watch Demo Movie

初期状態では、

と、丸つき数字のナンバリングが項目の入れ替えなどで正しく並んでいない状態を想定しています。

リナンバー対象の表を選択した状態で本Scriptを実行すると、

リナンバー対象行の問い合わせを行います。一番左の列のデータを抽出して選択させます。この場合には、「名称」を選択します。

次に、リナンバー対象データの列指定を行います。指定列「以降」から「末尾」までをリナンバー処理対象とみなします。この場合には「名称」を選択します。

最後に、丸つき数字のリナンバー開始数値を確認します。

すでに、対象範囲のデータを取り出して丸つき数字の最小値については計算してあるので、その確認のためにダイアログ入力を求めます。推測した数値でよければOKを、修正の必要があれば数値を変更したうえでOKボタンをクリック。

適切に処理が行われれば、丸つき数字部分のリナンバーが完了することでしょう。

AppleScript名:選択中の表の指定行・列のマル付き数字リナンバー v2.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/11/26
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7" — macOS 10.13 or later
use framework "Foundation"
use scripting additions

set aOffset to 0

–選択中のKeynote書類上の表への参照を取得する
set aTable to returnSelectedTableOnCurrentSlide() of me

if aTable = false then
  display notification "No selected table on Keynote document"
  
return
end if

–行選択
using terms from application "Keynote"
  tell aTable
    tell column 1
      set aList to value of every cell
    end tell
  end tell
end using terms from

set a2List to cleanUp1DList(aList, missing value) of me
set aRes to choose from list a2List with prompt "リナンバー対象の行選択"
set a2Res to search1DList(a2List, first item of aRes) of me

–列選択
using terms from application "Keynote"
  tell aTable
    tell row a2Res
      set bList to value of every cell
    end tell
  end tell
end using terms from

set b2List to cleanUp1DList(bList, missing value) of me
set bRes to choose from list b2List with prompt "リナンバー対象の列選択(ヘッダー末尾列)"
set b2Res to search1DList(b2List, first item of bRes) of me

–リナンバー対象のデータを取得(対象データのみ抽出)
using terms from application "Keynote"
  tell aTable
    tell row a2Res
      set bList to value of cells (b2Res + 1) thru -1
    end tell
  end tell
end using terms from

—データ中に丸つき数字が存在した場合には、最小のものを取得
set aOffset to (getMinimumNumFromNumberWithSign(bList) of me)

–一応、ユーザーに推測した開始値でよいか確認を取る
set dRes to text returned of (display dialog "丸つき数字の開始値:" default answer (aOffset as string))
try
  set aOffset to (dRes as number) – 1
on error
  set aOffset to 0
end try

–Keynoteの表のセルから取得したデータから丸つき数字を除去する
set cList to removeNumberWithSignFromList(bList) of me

–list中の各アイテムの冒頭に順次丸つき数字を追加する
set dList to {}
set aCount to 1

repeat with i in cList
  set j to convNumToNumWithSign(aCount + aOffset) of me
  
set jj to contents of i
  
  
set the end of dList to (j & jj)
  
  
set aCount to aCount + 1
end repeat

–リナンバー対象のセルをリナンバーした丸つき数字+内容で置換
set aCount to 1
using terms from application "Keynote"
  tell aTable
    tell row a2Res
      repeat with i from (b2Res + 1) to ((length of bList) + 1)
        set value of cell i to contents of item aCount of dList
        
set aCount to aCount + 1
      end repeat
    end tell
  end tell
end using terms from

–1~50の範囲の数値を丸つき数字に変換して返す
on convNumToNumWithSign(aNum as number)
  if (aNum ≤ 0) or (aNum > 50) then return ""
  
set aStr to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set bChar to character aNum of aStr
  
return bChar
end convNumToNumWithSign

–現在のスライド上で選択中の表オブジェクトへの参照を取得する
on returnSelectedTableOnCurrentSlide()
  tell application "Keynote"
    tell front document
      tell current slide
        try
          set theTable to first table whose class of selection range is range
        on error
          return false –何も選択されてなかった場合
        end try
        
        
return theTable
        
      end tell
    end tell
  end tell
end returnSelectedTableOnCurrentSlide

–1D List上で指定データを検索してヒットしたアイテム番号を返す
on search1DList(aList, aTarg)
  set anArray to current application’s NSMutableArray’s arrayWithArray:aList
  
set anIndex to anArray’s indexOfObject:aTarg
  
if (anIndex = current application’s NSNotFound) or (anIndex > 9.99999999E+8) then
    return false
  end if
  
return (anIndex as integer) + 1 –convert index base (0 based to 1 based)
end search1DList

–1D listのクリーニング
on cleanUp1DList(aList as list, cleanUpItems as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
if j is not in cleanUpItems then
      set the end of bList to j
    else
      set the end of bList to ""
    end if
  end repeat
  
return bList
end cleanUp1DList

–text in listから丸つき数字を除去する
on removeNumberWithSignFromList(aList as list)
  set bList to {}
  
repeat with i in aList
    set j to contents of i
    
set j2 to removeNumberWithSign(j) of me
    
set the end of bList to j2
  end repeat
  
return bList
end removeNumberWithSignFromList

–文字列から丸つき数字を除去する
on removeNumberWithSign(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end removeNumberWithSign

–1D Listに入っているテキストから丸つき数字を抽出して数値化し、最小のものを求める
on getMinimumNumFromNumberWithSign(aList)
  set nList to {}
  
  
repeat with i in aList
    set j to contents of i
    
–与えられたテキストのうち、丸つき数字(白)の
    
set j2 to holdNumberWithSignOnly(j) of me
    
set n2List to characters of j2 –複数の丸つき数字が入っている場合に対処
    
    
repeat with ii in n2List
      set jj to contents of ii
      
set tmpNum to decodeNumFromNumWithSign(jj) of me
      
set the end of nList to tmpNum
    end repeat
    
  end repeat
  
  
set anArray to current application’s NSArray’s arrayWithArray:nList
  
set cRes to (anArray’s valueForKeyPath:"@min.self") as integer
  
return cRes
end getMinimumNumFromNumberWithSign

–指定文字列から丸つき数字のみ抽出する
on holdNumberWithSignOnly(aStr as text)
  set aNSString to current application’s NSString’s stringWithString:aStr
  
return (aNSString’s stringByReplacingOccurrencesOfString:"[^\\U000024EA-\\U000024EA\\U00002460-\\U00002473\\U00003251-\\U000032BF\\U000024FF-\\U000024FF\\U00002776-\\U0000277F\\U000024EB-\\U000024F4\\U00002780-\\U00002789\\U0000278A-\\U00002793\\U000024F5-\\U000024FE]" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, aNSString’s |length|()}) as text
end holdNumberWithSignOnly

–丸つき数字を数値にデコードする v2
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set numStr2 to "❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴"
  
set numStr3 to "➀➁➂➃➄➅➆➇➈➉"
  
set numStr4 to "➊➋➌➍➎➏➐➑➒➓"
  
set numStr5 to "⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾"
  
  
set nList to {numStr1, numStr2, numStr3, numStr4, numStr5}
  
  
repeat with i in nList
    set numTemp to contents of i
    
if numTemp contains aStr then
      using terms from scripting additions
        set bNum to offset of aStr in numTemp
      end using terms from
      
return bNum
    end if
  end repeat
  
return false
end decodeNumFromNumWithSign

★Click Here to Open This Script 

Posted in list Number Text | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote | 1 Comment

iWorks Apps v11.2が登場

Posted on 9月 30, 2021 by Takaaki Naganoya

Apple純正のiWorks App(Keynote、Pages、Numbers)の新バージョンv11.2がリリースされました。

バージョン番号が「11.1」から「11.2」に変わっただけですが、内部的にも…各アプリケーションともアイコン自体が変更になり、大きな節目のバージョンであることが伺われます。

iOS、iPadOSが新製品とともに9月にアップデートし、iWorkアプリケーションがアップデートした後にmacOSのアップデートが行われるわけで、実にリリース間近であることを感じさせます。Windows 11の発表日がどの程度注目を浴びているかはわかりませんが、10月5日より前に発表することは…ないのではないかと(ノーアイデアです)。

これらのアプリケーションのAppleScript用語辞書については、「get」コマンドが追加されたことを確認していますが、このコマンドがないと実現できないことが不明(普通は省略されるコマンド)なので、何か後で追加するための「布石」と見るのが正しい見方でしょう。

Keynote v11.2の変更点:
・スライド上のライブビデオ
・複数発表者スライドショー
・レーダーグラフ(レーダーチャートのこと)

Pages v11.2の変更点:
・ブックの公開の改善
・素早くテキストを翻訳
・レーダーグラフ(レーダーチャートのこと)

Numbers v11.2の変更点:
・強力なピボットテーブル
・レーダーグラフ(レーダーチャートのこと)
・フィルタの改善

そして、AppleScript用語辞書に「レーダーグラフ」の作成機能は反映されていないため、まだAppleScriptから追加することはできません。通例だと、マイナーアップデート2回後ぐらいに反映されるのではないでしょうか?

Pages v11.2でトンでもない変更が加わっていることが判明しました。なんと、selectionを取得できなくなっています。

これは、Pagesの数少ない長所だったのですが、selectionを取得できないととても困ります、、、、、

一応、フィードバックしておきましたが、直るかどうかは不明です。Pagesの唯一の美点であった、selectionを返せ!!!

Keynote v11.2でも、selectionが使えなくなっていました(スライド単位の選択も無効に)。

Posted in news | Tagged 10.15savvy 11.0savvy 12.0savvy Keynote Numbers Pages | Leave a comment

macOS 11.5+Keynote 11.1でGUI Scriptingに障害?

Posted on 8月 1, 2021 by Takaaki Naganoya

GUI Scriptingを(メニューやキーボードショートカットについては)ラッピングして、ファイル名に「アプリケーション名@メニュー項目名>メニュー項目」などと書くだけで該当するメニュー項目の実行を行える「Piyo Menu Clicker v1.0 for Stream Deck」のリリース候補版を試していたら、macOS 11.5+Keynote 11.1でエラーが出まくりました。

# その後の調査により、よりによってStream Deckの制御ソフトウェア「Stream Deck.app」が起動しているとこの問題が発生することがわかりました。Stream Deck用の制御Scriptを使おうとしたらStream Deckのソフトウェア自体が実行を邪魔していたとは….

–> Browse Demo Movie

メニューバーのメニュー項目を指し示そうとして、

AppleScript名:Keynoteで新規書類を実行.scpt
activate application "Keynote"
tell application "System Events"
  tell process "Keynote"
    click menu item 1 of menu 1 of menu bar item 3 of menu bar 1
  end tell
end tell

★Click Here to Open This Script 

のようなお気軽な内容を試してみたところ、エラーに。メニューが存在していて、GUI側からマウスで普通に操作できるのですが……

他のPagesであるとかSafariなどで同様の記述を行なってもエラーになりません。Keynoteだけ問題が発生しているようです。

AppleScript名:Pagesで新規書類を作成.scpt
activate application "Pages"
tell application "System Events"
  tell process "Pages"
    click menu item 1 of menu 1 of menu bar item 3 of menu bar 1
  end tell
end tell

★Click Here to Open This Script 

AppleScript名:Safariで新規ウィンドウを表示.scpt
activate application "Safari"
tell application "System Events"
  tell process "Safari"
    click menu item 1 of menu 1 of menu bar item 3 of menu bar 1
  end tell
end tell

★Click Here to Open This Script 

Keynoteのmenu bar 1に対してmenu bar itemのtitle(メニュー項目のテキスト)を求めると、「Apple」しか返ってきません。

tell application "System Events"
  tell process "Keynote"
    tell menu bar 1
      set mList to title of every menu bar item
      
–> {"Apple"}      
    end tell
  end tell
end tell

★Click Here to Open This Script 

iOSアプリをM1 Mac上で起動した場合にこのような挙動になるのか調べてみても、ちゃんとiOSアプリもメニュー項目を取得できます。iOSアプリについては(苦労はするものの)AppleScriptから強引に操作することは可能なので、Keynoteよりは出来がいいといえます。

追記:
手元にあるバージョンの異なるmacOS環境でこの処理を試してみました。

 macOS 12.0beta+Keynote v11.1→異常
 macOS 11.5+Keynote v11.1→異常
 macOS 10.15.7+Keynote v11.1→正常
 macOS 10.14.6+Keynote v10.1→異常
 macOS 10.13.6+Keynote v9.1→正常

追記2:
異常が出ていた環境では、すべてStream Deckの制御ソフトウェアをインストールして起動していました。Stream Deck.appを終了させると問題が発生しないことを確認しました。Stream Deck Softwareのせいでした。

Stream Deckから各種ソフトウェアを操作するために作ったソフトウェアが、Stream Deckソフトウェアによって実行を阻害されているという事実の前に言葉もありません。

elgatoのWebフォームからレポートを送っていますが、elgatoにはいろいろ送っても、一度も返事をもらったことがないのでなんとも(日本代理店のソフトバンクC&Sとは確認のためにやりとりしていますが…)。

厳密に言うと、Stream Deckのオンラインストアから入手可能な「Keynote」プラグイン(Keynoteのプレゼン再生コントロールを行うelgato製のプラグイン)をインストールするとGUI ScriptingのKeynoteに対する実行が阻害されます。

–> Elgato Keynote plugin checking demo


▲まさか、elgato純正のプラグインがKeynoteへのGUI Scriptingの実行(メニューへのアクセス)を妨害しているとは思いませんでした。このプラグインはインストールしてはいけないものだと思います。機能もたいしたことはないし、害悪でしかありません。即刻Storeから撤去してほしいものです

Posted in Bug GUI Scripting | Tagged 11.0savvy Keynote Stream Deck | 5 Comments

Keynote, Pages, Numbersが v11.10にアップデート

Posted on 6月 3, 2021 by Takaaki Naganoya

Keynote, Pages, Numbersがv11.10にアップデートしました。Keynoteのみ、AppleScript用語辞書に変更点があります。

Keynoteの変更点はマスタースライドのオブジェクト。従来は「master slide」というオブジェクトだったのが、「slide layout」に変更になっています。理由……についてはよくわかりません。いまさらこんな箇所をいじくる意味がわかりません。

Keynoteの画面を調べてみたら、画面上のマスタースライドの呼称が「スライドレイアウト」に変更になったため、これを反映させたようです。

別にGUI画面上の呼称をオブジェクト名に反映させて変更しなくてもいいんじゃないかと考えますが、やはり意味がよくわかりません。前バージョンまでのくだらないミス(実際に動かしたら分かるレベルのお寒いミス)を連発していた担当者から、別の担当者に変わったような雰囲気がします(知らんけど)。

そんな細かいところは個人的にはドーでもいいので、selection objectとかselected object(スライド上の選択中のオブジェクト)を取得できるようになってほしいです。これができないためにKeynoteで無駄な処理を書かされることが非常に多いので。

こっそり変更したかったのか、一応、macOS 11.5上で「master slide」と入力して構文確認すると、同義語(synonym)として処理され、「slide layout」に自動で書き換えられます。


▲構文確認前


▲構文確認後。「master slide」と入力した箇所が「slide layout」に自動で書き換えられる


▲Keynote v11.01時のAppleScript用語辞書に掲載されていたサンプルAppleScript


▲Keynote v11.10時のAppleScript用語辞書に掲載されていたサンプルAppleScript。こういう部分でミスしてこないあたり、本当に担当者が変わったようにしか見えない

Posted in sdef | Tagged 10.15savvy 11.0savvy Keynote | Leave a comment

Keynoteで現在のスライドのタイトルを指定の文字ではじまるテキストアイテムにコピーする

Posted on 5月 3, 2021 by Takaaki Naganoya

Keynoteの最前面の書類の現在表示中のスライド(ページ)のタイトルを、指定の文字ではじまるテキストアイテムにコピーするAppleScriptです。

Music.app本にプログラムリストを2x Retina表示のスクリーンキャプチャで掲載してみたところ、書き出したPDFのファイルサイズが巨大になりすぎたり、後処理で圧縮したら一律に画像が荒くなったりで、画像で掲載することをあきらめてKeynoteのオブジェクトでそれっぽく組み立てて掲載することにしました。

その際に、Keynoteオブジェクトで作ったプログラムリストっぽい何かのタイトル部分を一律にダミー文字を入れておいたので、各ページのタイトルを入れてみることにしました。

AppleScript名:現在のスライドのタイトルを指定の文字ではじまるテキストアイテムにコピーする.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/05/03
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "Keynote"
  tell front document
    tell current slide
      set tList to every text item whose object text starts with "あああああ" –抽出条件
      
set titleStr to (object text of default title item) as string
      
      
repeat with i in tList
        set j to contents of i
        
set object text of j to titleStr
      end repeat
      
      
(*
      –サイズを自動で調整してみる???
      repeat with i in tList
        set {xPos, yPos} to position of i
        set aHeight to height of i
        set aWidth to width of i
        
        set oList to every shape
        set pList to position of every shape
      end repeat
      *)

    end tell
  end tell
end tell

★Click Here to Open This Script 

Posted in Text | Tagged 10.14savvy 10.15savvy 11.0savvy Keynote | Leave a comment

Keynoteの表の背景色がない箇所を白く塗る

Posted on 4月 29, 2021 by Takaaki Naganoya

Keynoteでオープン中の最前面の書類の現在表示中のスライド(ページ)の上にある表の背景色が塗られていないセルを白く塗るAppleScriptです。

一応、非同期処理モードを使うことでスピードを稼いでいますが、この処理は速くありません。非同期処理モードの宣言部分を外した姿が本当のスピードです。

全部一括で塗るとかRangeを指定して塗るといった処理ができるとスピードを稼げると思いますが、「辻褄合わせ」とか「例外」的な処理なので、(Apple側の対応は)あまり期待できないでしょう。


▲処理前


▲処理後

AppleScript名:Keynoteの表の背景色がない箇所を白く塗る
tell application "Keynote"
  tell front document
    tell current slide
      tell table 1
        set cList to every cell
        
repeat with i in cList
          tell i
            set tmpColor to background color
            
if tmpColor = missing value then
              ignoring application responses
                set background color to {65535, 65535, 65535}
              end ignoring
            end if
          end tell
        end repeat
      end tell
    end tell
  end tell
end tell

★Click Here to Open This Script 

Posted in Color list | Tagged 10.14savvy 10.15savvy 11.0savvy Keynote | Leave a comment

Keynoteで現在表示中のスライド上にあるすべての表のカラム幅を自動調整

Posted on 4月 28, 2021 by Takaaki Naganoya

Keynoteで現在オープン中の書類の表示中のスライド(ページ)に存在する表のカラム幅を自動調整するAppleScriptです。


▲スライド上のカラム幅が不揃いの表を….


▲1列目はそのままの幅で、残りは均等割で調整します

AppleScript名:Keynoteで現在表示中のスライド上にあるすべての表のカラム幅を自動調整
— Created 2017-10-06 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

tell application "Keynote"
  tell front document
    tell current slide
      set tList to every table
      
      
repeat with i in tList
        set j to contents of i
        
prepareCoumnWidthInATable(j) of me
      end repeat
    end tell
  end tell
end tell

on prepareCoumnWidthInATable(aTable)
  tell application "Keynote"
    tell aTable
      set cCount to count every column
      
set cWidth to width of every column
      
set aWidth to width –table width
      
      
set aveWidth to (aWidth – (first item of cWidth)) / (cCount – 1)
      
      
tell columns 2 thru cCount
        set width to aveWidth
      end tell
    end tell
  end tell
end prepareCoumnWidthInATable

★Click Here to Open This Script 

Posted in Number | Tagged 10.14savvy 10.15savvy 11.0savvy Keynote | Leave a comment

Keynote v11.0.1がリリースされる

Posted on 3月 31, 2021 by Takaaki Naganoya

Keynoteがv11.0からv11.0.1にアップデートされました。

AppleScript用語辞書(sdef)の差分をとって確認してみたところ、とくに変更点は見つかりません。

Posted in news | Tagged 10.15savvy 11.0savvy Keynote | Leave a comment

iWork Appsがv11.0にアップデート、Keynoteの恥ずかしいバグが直る

Posted on 3月 24, 2021 by Takaaki Naganoya

iWork Apps(Keynote、Numbers、Pages)がバージョン11.0にアップデートされました。

3アプリ共通でAppleScriptから書類のパスワード設定確認、パスワード設定の機能が追加されています。

また、Keynote v10.3.5、v10.3.8とmovie書き出し時のCodec指定オプションに「h.264」とAppleScriptの処理系ではエラーになる記号を含む予約語が入っていたのですが、これがv11.0で「h264」と修正されました。

# 記号を含む予約語は禁止されています。「C++」とか(Fine Reader OCR Proに入っていますわー)

実際に動作確認してみないとまともに動くかどうかは不明ですが、用語辞書上では修正されています。報告して半年以内で修正されたので、修正も何もされないよりはマシでしょう。正直なところ、こんな程度の低いバグは事前にチェックして解消してほしいところではあります(Mac App Storeに一般開発者が出したらリジェクトされるレベル?)。

ちょいちょいAppleScriptの瑣末な機能を追加していただけるのはありがたいのですが、ページ内の選択中のオブジェクトへのアクセス(selected itemsとか)を用意してほしいところです。現状ではselectionで取れるのはページ(slide)単位だけです。

また、text itemの縦書き(Vertical)属性にもアクセス(Read/Write)できてほしい気がします。Keynote書類上にWord Cloudを作成するときにScriptから素直にオブジェクト回転ができずに困ります(GUI Scriptingで強引にやってるんですけれども)。


▲Keynote v11:h.264→h264と修正


▲Keynote v11:documentに「password protected」属性(Read Only)と、「set password」コマンドが追加

Keynote v11.0上で動作確認ずみ。ただし、いったんパスワードを設定した書類は、GUI側からしかパスワード解除できない点に注意。また、AppleScriptからのKeynote書類オープン時にパスワードを指定してオープンすることもできないので、当該書類のパスワードをKeychainに保存しないと書類オープン時のパスワード入力をパスできない。こちらはちゃんと(h.264と違って)動作確認したらしい。でも、油断はできない。それがAppleクオリティ。


▲Numbers v11:documentに「password protected」属性(Read Only)と、「set password」コマンドが追加


▲Pages v11:documentに「password protected」属性(Read Only)と、「set password」コマンドが追加


▲Pages v11:exportコマンドのexport optionsに「include comments」属性と、「include annotations」属性が追加

Posted in Bug Update | Tagged 10.15savvy 11.0savvy Keynote Numbers Pages | Leave a comment

Keynoteで選択中のオブジェクトをテキストとサイズを元に特定する

Posted on 3月 16, 2021 by Takaaki Naganoya

Keynoteの最前面の書類の現在表示中のスライドで選択中のオブジェクトの情報を、テキスト(object text)と幅(width)、高さ(height)から特定するAppleScriptです。

KeynoteのAppleScript用語辞書に「selected item」といった予約語がなく、現在のスライド上の「選択中のオブジェクト」を取得する機能が存在していません。一応、「selection」という予約語はあるのですが、これが有効なのは「選択中のスライド」(複数選択可)なので、本来やりたい「選択中のテキストアイテムからリンク情報を消去」といった処理ができません。

その下調べで作ってみました。

以前、選択中のオブジェクトのテキスト(object text)を取得するScriptを書いたことはあったのですが、それはあくまでテキストを取得するだけで、元のスライド上のオブジェクトのどれが該当するかという処理は行なっていませんでした。

そこで、選択中のオブジェクト(だいたいテキストアイテムが目標)をテキスト(object text)、オブジェクトの幅と高さで比較して特定するようにしてみました。例によってCopy & Pasteで新規書類にペーストして情報を調べるため、GUI Scriptingがオンになっている必要があります。

本Scriptの実行前にKeynote書類上のオブジェクトを選択しておく必要があります。

AppleScript名:選択中のオブジェクトをテキストとサイズを元に特定する.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/01/27
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

–選択中のオブジェクト(Text Frameを想定)をコピペで取得する
set kList to getEveryTextFromCurrentKeynoteSlide() of me

set out2List to {}

–取得した情報をもとに元書類からデータを特定する
tell application "Keynote"
  tell front document
    tell current slide
      repeat with i in kList
        copy i to {tmpTitle, tmpW, tmpH}
        
try
          set bList to (every iWork item whose object text is equal to tmpTitle and width of it is equal to tmpW and height of it is equal to tmpH)
          
set tmpItem to first item of bList
          
set the end of out2List to tmpItem
        on error
          log {"Something Wrong with an item", tmpTitle, tmpW, tmpH}
        end try
      end repeat
    end tell
  end tell
end tell

return out2List
–> {text item 4 of slide 179 of document id "E3EFE83C-A691-4D99-8939-6928892CBED5"}

–選択中のオブジェクトをコピーして新規書類にペーストしてオブジェクト情報(テキスト、幅、高さ)を返す
on getEveryTextFromCurrentKeynoteSlide()
  tell application "Keynote"
    activate
    
    
set dCount to count every document
    
if dCount = 0 then
      display notification "There is no Keynote document"
      
return {}
    end if
    
    
tell front document
      set sCount to count every slide
    end tell
    
if sCount = 0 then
      display notification "There is no Slide in Keynote document"
      
return {}
    end if
    
  end tell
  
  
–Copy
  
tell application "System Events"
    keystroke "c" using {command down}
  end tell
  
  
  
tell application "Keynote"
    activate
    
set nDoc to make new document
    
tell nDoc
      set aMaster to master slide "空白"
      
–set aMaster to master slide "Blank"
      
      
tell current slide
        set base slide to aMaster
      end tell
    end tell
    
  end tell
  
  
–Paste
  
tell application "System Events"
    keystroke "v" using {command down}
  end tell
  
  
delay 0.01 –Important!!
  
  
set tOut to {}
  
tell application "Keynote"
    tell front document
      tell current slide
        set tList to every iWork item
        
        
repeat with i in tList
          set tmpText to (object text of i) as string
          
set tmpHeight to height of i
          
set tmpWidth to width of i
          
          
set the end of tOut to {tmpText, tmpWidth, tmpHeight}
        end repeat
        
      end tell
    end tell
    
    
–Dispose document
    
tell front document
      close without saving
    end tell
  end tell
  
  
return tOut
end getEveryTextFromCurrentKeynoteSlide

on retDelimedText(aList, aDelim)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retDelimedText

★Click Here to Open This Script 

Posted in selection | Tagged 10.14savvy 10.15savvy 11.0savvy Keynote | Leave a comment

Keynoteの表の中に入っている1列目のデータをもとにフォルダ検索して値を返す

Posted on 3月 6, 2021 by Takaaki Naganoya

Keynoteの表の中に入っている1列目のデータをもとに、指定ルートフォルダ以下のサブフォルダを指定して、その下位フォルダに存在しているAppleScript書類の数をSpotlightでかぞえ、表の2列目に書き戻すAppleScriptです。

新著「Cocoa Scripting Course #1」の付録Scriptについて、各フォルダに入っているScript書類をかぞえて表に入れておく必要がありました。ただ、内容をアップデートするたびに書き換える必要があるので、自動でカウントして表に書き込むScriptを用意しました。

実行にはShane Stanleyの「Metadata Lib」を必要とします。実行にはScript Debuggerも必要です。Keynoteで表の入ったスライドを表示している必要もあります。表の1列目に書かれたサブフォルダ名を指定して、それぞれ含まれている書類数をカウントして表に書き戻します。


▲初期状態


▲カウント対象フォルダ。AppleScript書類がフラット形式とバンドル形式の2つが混在して入っている


▲書類数をかぞえるルートフォルダを指定。表の1列目にかかれているフォルダ名と連結して、その下をすべてSpotlightで検索してファイルの存在確認を行う


▲すべてカウントして表に書き戻したところ

AppleScript名:表の中に入っている1列目のデータをもとにフォルダ検索して値を返す.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/03/05
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.7" — High Sierra (10.13) or later
use framework "Foundation"
use scripting additions
use mdLib : script "Metadata Lib" version "2.0.0"

–カウントするAppleScript書類が入っているフォルダのルートフォルダを指定
set origPath to POSIX path of (choose folder with prompt "Select Target folder")

tell application "Keynote"
  tell front document
    tell current slide
      –現在のスライド上の表を取得
      
set tList to every table
      
      
–表オブジェクトでループ
      
repeat with i in tList
        
        
–表オブジェクトの情報取得
        
tell i
          set colCount to column count
          
set rowCount to row count
          
set headerCount to header row count
          
set footerCount to footer row count
          
          
          
–表のセル内データを取得して2D Arrayにまとめる
          
set newList to {}
          
repeat with y from 1 to rowCount
            tell row y
              set cList to value of every cell
              
set tmpL to {}
              
              
repeat with ii in cList
                set jj to contents of ii
                
if jj = missing value then set jj to "" –空欄はmissing valueが返ってくるのでヌルストリングに差し替え
                
set the end of tmpL to jj
              end repeat
              
              
set the end of newList to tmpL
            end tell
          end repeat
          
          
          
–Keynoteの表データ1列目の情報をもとに、実際のフォルダの中のAppleScript書類を検索してカウントしてlistに記入
          
set new2List to items (headerCount + 1) thru -1 of newList
          
repeat with ii in new2List
            copy ii to oneLine
            
set targFolName to first item of oneLine
            
set tmpTarg to origPath & "/" & targFolName & "/"
            
            
–Spotlight検索でファイルを取得する
            
set aResList to perform search in folders {tmpTarg} predicate string "kMDItemContentType == %@ || kMDItemContentType == %@" search arguments {"com.apple.applescript.script", "com.apple.applescript.script-bundle"} –フラット形式書類とバンドル形式書類を検索
            
            
set scrCount to length of aResList –書類数をカウント
            
set last item of ii to scrCount
          end repeat
          
          
          
–Keynoteの表にデータを書き戻す
          
repeat with y from 1 to (length of new2List)
            tell row (y + 1)
              set aDat to contents of item y of new2List
              
repeat with x from 1 to length of aDat
                
                
–非同期実行モード(倍速処理)
                
ignoring application responses
                  tell cell x
                    set its value to contents of (item x of aDat)
                  end tell
                end ignoring
                
              end repeat
            end tell
          end repeat
          
        end tell
      end repeat
    end tell
  end tell
end tell

★Click Here to Open This Script 

Posted in file list Spotlight | Tagged 10.14savvy 10.15savvy 11.0savvy Keynote | 1 Comment

Keynoteで全スライドのタイトルアイテムの高さを統一

Posted on 3月 1, 2021 by Takaaki Naganoya

Keynoteの最前面の書類で、選択したマスタースライドに該当するスライドのタイトルのテキストフレームの高さ(height)を統一するAppleScriptです。

Cocoa Scripting本の監修者のedama2さんからタイトルの大きさが不揃いで見にくいという指摘があり、その対処のために組んでみました(泣)。


▲マスタースライド名でどのスライドを処理するか指定


▲スライドタイトルの高さを統一

AppleScript名:全スライドのデフォルトタイトルアイテムの高さを統一.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/03/01
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

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

set targetHeight to 97

–最前面の書類のベーススライド(マスタースライド)名称一覧を取得してユニーク化
tell application "Keynote"
  set baseNames to (name of base slide of every slide of front document)
end tell
set aRes to uniquify1DList(baseNames) of me

–処理対象のマスタースライドを選択
set tSlide to (choose from list aRes with prompt "処理対象のベーススライド名を選択") as string

–最前面の書類の全スライドのうち、指定のマスタースライドを指定してあるもののタイトルの高さを統一
tell application "Keynote"
  tell front document
    –全スライドを取得してループ
    
set sList to every slide
    
repeat with i in sList
      set j to contents of i
      
tell j
        –スライドのマスタースライド名が指定のものに該当する場合にのみ処理
        
set bSlide to (name of base slide) as string
        
if bSlide is equal to tSlide then
          try
            ignoring application responses
              set height of default title item to targetHeight
            end ignoring
          end try
        end if
      end tell
    end repeat
  end tell
end tell

on uniquify1DList(theList as list)
  set theSet to current application’s NSOrderedSet’s orderedSetWithArray:theList
  
return (theSet’s array()) as list
end uniquify1DList

★Click Here to Open This Script 

Posted in list | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy Keynote | Leave a comment

指定した文字で囲まれたキーワードの色を置換する

Posted on 2月 24, 2021 by Takaaki Naganoya

Keynoteで現在オープン中の書類の現在表示中のスライド(ページ)中にあるtext itemのうち、開始文字、終了文字ではさまれているテキストを指定色に塗り替えるAppleScriptです。

用途はご覧の通り、現在作成中のCocoa Scripting本の資料の装飾のためであり、「|」と「|」で囲まれている箇所の色を変えたら見やすいのでは? と、考えて作成・実行したものです。

NSScannerを使えば楽に書けるかも? などと思っておりましたが、検出位置(文字数)を返す機能がないので、位置だけを返してほしいという用途に向いていないもよう。仕方なく、普通にAppleScriptで1文字ごとチェック&開始文字/終了文字のチェックをフラグ管理……という、高級言語らしからぬ泥臭いコードを書く羽目に(でも、こういうの書くケースが多いですよね)。


▲実行前


▲実行後

AppleScript名:指定した文字で囲まれたキーワードの色を置換する
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/02/24
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

set targCol to choose color –塗る文字色を選択

set headPickUpChar to "|"
set tailPickUpChar to "|"

–処理対象データの取得
tell application "Keynote"
  
  
–最前面の書類
  
tell front document
    
    
–現在表示中のスライド
    
tell current slide
      –text itemをすべて取得
      
set tList to every text item
      
if tList = {} then return –何もtext itemが存在しなかったら処理終了
      
      
–text itemの中に入っている文字列を取得
      
set objTList to object text of every text item
      
    end tell
  end tell
end tell

–出現判定しつつ色を塗る
tell application "Keynote"
  tell front document
    tell current slide
      
      
set posList to {}
      
set aCount to 1
      
      
–text itemでループ
      
repeat with i in objTList
        set j to contents of i
        
        
–ターゲット文字で囲まれている箇所を検出。複数箇所検出対応
        
set bCon to pickUpPositionPairsFromTo(j, headPickUpChar, tailPickUpChar) of me
        
        
if bCon is not equal to {} then
          
          
set aTarg to item aCount of tList
          
          
–出現位置でループ(複数検出対応)
          
repeat with ii in bCon
            copy ii to {aStart, anEnd}
            
            
–高速処理のために非同期実行
            
ignoring application responses
              tell aTarg
                set color of characters aStart thru anEnd of object text to targCol
              end tell
            end ignoring
            
          end repeat
          
        end if
        
        
set aCount to aCount + 1
      end repeat
      
    end tell
  end tell
end tell

–開始文字と終了文字に囲われた文字列のすべての位置情報を返す
on pickUpPositionPairsFromTo(aParamStr, fromStr, toStr)
  script hsAry
    property anArray : {}
    
property aList : {}
  end script
  
  
set (anArray of hsAry) to {}
  
set (aList of hsAry) to characters of aParamStr
  
  
set tmpPair to {}
  
set searchMode to 0 — 0:fromStr, 1:toStr
  
set aCount to 1
  
  
repeat with i in (aList of hsAry)
    set j to contents of i
    
    
if searchMode = 0 then
      if j = fromStr then
        set the end of tmpPair to aCount
        
set searchMode to 1
      end if
      
    else if searchMode = 1 then
      if j = toStr then
        set the end of tmpPair to aCount
        
set searchMode to 0
        
set the end of (anArray of hsAry) to tmpPair
        
set tmpPair to {}
      end if
    end if
    
    
set aCount to aCount + 1
  end repeat
  
  
return (anArray of hsAry)
end pickUpPositionPairsFromTo

★Click Here to Open This Script 

Posted in Color Text | Tagged 10.14savvy 10.15savvy 11.0savvy Keynote | 1 Comment

最前面のKeynote書類のテキストアイテムの英語化率を求める

Posted on 1月 28, 2021 by Takaaki Naganoya

Keynote書類内のテキストアイテムのうち、どの程度が英語で書かれているかを計算するAppleScriptです。macOS 10.14以降で動作します。

日本語で書いた本を英語に翻訳しているわけですが、進捗を確認するのに自分でページを見ながら確認する作業が地味に大変です。そこで、この大変な作業をAppleScriptで自動処理することにしました。最終的なチェックは目視で行う必要がありますが、翻訳済みのテキストがどの程度あるかをテキストアイテムの個数ベースで(文章の長さは考慮せず)自動計算します。

AppleScriptでKeynote書類中のテキストアイテム中のテキストをすべて取得し、言語を自動判定して英語化率(≒英訳作業の進捗率)を計算してみました。


▲Script Debugger v8で実行したところ。Dark Modeへの対応が嬉しいが、全体的に動作が重い

日本語と英語以外にいろいろ意図しない言語判定されているテキストアイテムが存在していますが、そんな言語は書いていないのでおおよそ英語ブロックでしょう。

いま英語化率は80%と出ていますが、誤判定もあるはずなので、最後の最後は目視判定が必要と思われます。あとは、日本語フォントが指定されている箇所をクリーニングして、英語フォントに指定し直す作業も必要です。

開発マシンでmacOS 10.14.6+Keynote 10.1の環境で、139ページある「FileMaker Pro Scripting Book」の全テキスト判定に3.9秒ほどかかります。速度面でとくに不満はありませんが、こういう処理をM1 Macで行うとどの程度の速度になるのかには興味があります。

FileMaker選手権に応募した作品が入賞すれば、開発マシンの更新も可能かもしれませんが……勝負は時の運ということで。


▲英語への翻訳が完了したページ(表紙)


▲まだ日本語の文章がまだらに残っているページ。ただ、オリジナルの日本語版をもとに日本語の本のルールでページ順を設定しているので、「本当にこれでいいのか?」(奥付が最終ページにある日本スタイルそのまま、とか)とは思っています

AppleScript名:最前面のKeynote書類のテキストアイテムの英語化率を求める.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/01/27
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.7" — High Sierra (10.13) or later
use framework "Foundation"
use framework "NaturalLanguage" –Mojave (10.14) or later
use scripting additions

tell application "Keynote"
  set dCount to count every document
  
if dCount = 0 then
    display notification "There is no Keynote document"
    
return {}
  end if
end tell

set tOut to {}
set erOut to {}

tell application "Keynote"
  tell front document
    set sCount to count every slide
    
repeat with i from 1 to sCount
      tell slide i
        set tList to every text item
        
        
repeat with i in tList
          set aTmpStr to object text of i
          
if aTmpStr is not equal to "" then
            set b1Res to guessLanguageCodeOf(aTmpStr) of me
            
set the end of tOut to b1Res
            
(*
            if b1Res is not equal to "en" then
              set the end of erOut to aTmpStr
            else
              set the end of tOut to b1Res
            end if
            *)

          end if
        end repeat
      end tell
    end repeat
  end tell
end tell

set tPercent to calcOneItemsPercent(tOut, "en") of me

on guessLanguageCodeOf(theString as string)
  set aNL to current application’s NLLanguageRecognizer’s alloc()’s init()
  
aNL’s processString:theString
  
set langRes to (aNL’s dominantLanguage()) as string
  
return langRes
end guessLanguageCodeOf

–true/falseで構成されるlistのうち、指定要素が占める割合を%で計算。小数点以下を四捨五入
on calcOneItemsPercent(aList, targItem)
  set aLen to length of aList
  
set theCountedSet to current application’s NSCountedSet’s alloc()’s initWithArray:aList
  
set tRes to (theCountedSet’s countForObject:targItem)
  
if tRes < 1 then return 0
  
set pRes to (tRes / aLen) * 100
  
return (round pRes rounding as taught in school)
end calcOneItemsPercent

★Click Here to Open This Script 

Posted in Language Text | Tagged 10.14savvy 10.15savvy 11.0savvy Keynote | 1 Comment

Keynoteで選択中のテキストアイテムからテキスト取り出し

Posted on 1月 27, 2021 by Takaaki Naganoya

Keynoteで選択中のテキストアイテムからテキスト情報を抽出するAppleScriptです。

# 本内容は当時のKeynote v11.xの状況を反映したもので、その後リリースされたv12ではselectionを取得できるように変更されました

FileMaker Pro Scripting Bookの英語版を来る日も来る日も作っており、気づけばぜんぜんScriptを書いていないので「Piyomaru Software」ではなく「Piyomaru Publishing」だ、などと言っている今日このごろです。Keynoteを毎日使っていますが、微妙に痒いところに手が届かないので、使えば使うほどAppleScriptで機能を補いたくなってきます。

Keynoteに「selected objects」といった「選択中のオブジェクト」を求めるAppleScript用語が用意されていないため、本来やりたい「選択中の部品のデータを処理して元の部品に書き戻す」「選択中の部品からデータを抜き出す」といった処理ができません。selectionで取得できるのが「選択中のスライド」だというのが非常に残念です。

Keynote書類でテキストアイテムを選択し、コピーすると……テキスト情報は取り出せません。Finder上でクリップボード内容を確認してみると、あろうことか「PNGイメージ」と表示されます。

コピーされたクリップボードの内容を解析して、そのオブジェクト情報からテキストを抽出できるとよいだろうか、などとも考えたのですが、

Keynote内部オブジェクトで、ちょっと手強そうです。

というわけで、Keynote側には一切機能がないわけですが、選択中のオブジェクトをコピーし、新規ドキュメントにペーストしたうえで新規ドキュメント上のオブジェクトからテキストを取り出し、新規ドキュメントを破棄することにしました。

GUI Scriptingを用いているので、システム環境設定の「セキュリティとプライバシー」でGUI Scriptingを許可してから実行してください。

AppleScript名:選択中のテキストアイテムからテキスト取り出し.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/01/27
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set kList to getEveryTextFromCurrentKeynoteSlide() of me
set tRes to retDelimedText(kList, return) of me

on getEveryTextFromCurrentKeynoteSlide()
  tell application "Keynote"
    activate
    
    
set dCount to count every document
    
if dCount = 0 then
      display notification "There is no Keynote document"
      
return {}
    end if
    
    
tell front document
      set sCount to count every slide
    end tell
    
if sCount = 0 then
      display notification "There is no Slide in Keynote document"
      
return {}
    end if
    
  end tell
  
  
–Copy
  
tell application "System Events"
    keystroke "c" using {command down}
  end tell
  
  
  
tell application "Keynote"
    activate
    
set nDoc to make new document
    
tell nDoc
      set aMaster to master slide "空白"
      
–set aMaster to master slide "Blank"
      
      
tell current slide
        set base slide to aMaster
      end tell
    end tell
    
  end tell
  
  
–Paste
  
tell application "System Events"
    keystroke "v" using {command down}
  end tell
  
  
delay 0.1 –Important!!
  
  
set tOut to {}
  
tell application "Keynote"
    tell front document
      tell current slide
        set tList to every iWork item
        
        
repeat with i in tList
          set aTmpStr to object text of i
          
set the end of tOut to aTmpStr
        end repeat
      end tell
    end tell
    
    
–Dispose document
    
tell front document
      close without saving
    end tell
  end tell
  
  
return tOut
end getEveryTextFromCurrentKeynoteSlide

on retDelimedText(aList, aDelim)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retDelimedText

★Click Here to Open This Script 

Posted in GUI Scripting Text | Tagged 10.14savvy 10.15savvy 11.0savvy Keynote | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

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

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 (131) CotEditor (66) Finder (51) iTunes (19) Keynote (117) 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年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