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

タグ: 11.0savvy

Pagesで選択中の表のカラム幅を自動調整

Posted on 12月 18, 2021 by Takaaki Naganoya

Pagesの最前面の書類で選択中の表オブジェクトの2列目以降を均等に自動調整するAppleScriptです。

–> Watch Demo

1列目の列幅は変更しません。表全体を選択するのではなく、どこかのセルを選択しておく必要があります。


▲処理前 カラム幅が不均一。表の1つあるいは複数のセルを選択して処理対象を指定


▲処理後 2列目以降が均一に調整される

AppleScript名:Pagesで選択中の表のカラム幅を自動調整
— Created 2021-12-18 by Takaaki Naganoya
— 2021 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

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

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

–2列目から末尾までの列幅を均等に変更する
using terms from application "Pages"
  tell curTable
    set cCount to count every column
    
set cWidth to width of every column
    
    
–表の幅を取得したら、0が返ってきたので、各カラム幅を合計した
    
set aWidth to calcSumFrom1DList(cWidth) of me
    
    
set aveWidth to (aWidth – (first item of cWidth)) / (cCount – 1)
    
    
–いったん幅を少なくしておく(予備動作)
    
–途中で幅がページ幅をオーバーすると最終的な表の幅がおかしくなるので、この処理がないとおかしくなる
    
repeat with i from 2 to cCount
      set width of column i to 10
    end repeat
    
    
–カラム幅を変更する
    
repeat with i from 2 to cCount
      set width of column i to aveWidth
    end repeat
  end tell
end using terms from

–1D List中の数値を合計して返す
on calcSumFrom1DList(aList)
  set anArray to current application’s NSArray’s arrayWithArray:aList
  
return (anArray’s valueForKeyPath:"@sum.self")’s intValue()
end calcSumFrom1DList

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

★Click Here to Open This Script 

Posted in list | Tagged 10.15savvy 11.0savvy 12.0savvy Pages | Leave a 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

漢字をパーツに分解して部品が共通する文字を検索

Posted on 12月 14, 2021 by Takaaki Naganoya

2つの漢字をパーツごとに分解し、それぞれの分解したパーツからそれを使用している漢字を検索。2つのグループ間で共通して存在している漢字を抽出することで「部品が共通する文字」をリストアップする処理を擬似的に実現しているAppleScriptです。

macOSの日本語入力プログラムで実装している、「部品が共通な漢字を検索」機能っぽいものを目指したものです。例によって、掲載プログラムリストでは部品が足りずに実行できないので、以下のすべての部品を含むScriptバンドルのアーカイブをダウンロードして実行してください。

–> Download listupKanjiWithCommonParts(including AppleScript Libraries and json data)

「際」「隆」という文字を指定すると、

--> {"際", "障", "隠", "隣", "阪", "防", "阻", "附", "降", "限", "陛", "院", "陣", "除", "陥", "陪", "腹", "堕", "陰", "墜", "陳", "陵", "陶", "陸", "険", "陽", "隅", "隆", "隊", "階", "随", "隔", "隙"}

という結果を返してきます。Mac mini M1で0.46秒ぐらいです。処理時間はデータによっては大幅に増える場合がありますが、そういうものだと思ってください。テストした範囲内では最悪で1.6秒ぐらいかかりました。もともとのJSONデータを(日本の)常用漢字の範囲内だけにシェイプアップすれば半分以下の速度で実行できると思います。

いろいろ細かく高速化のための記述していますが、実測値で0.02秒ぐらい速くなっているだけなので、わかりやすさを優先して高速化のための記述はすべて外してしまったほうがよいかもしれません。

割とロマン系の「役に立つんだかどうか不明だけど、作れることがわかったので作ってみた」プログラムです。

AppleScript名:漢字をパーツに分解して部品が共通する文字を検索.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/14
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.7"
use framework "Foundation"
use scripting additions
use jkLib : script "jyoyoKanjiLib" –常用漢字一覧を返してくるAppleScriptライブラリ

script jsonStr
  property aJsonDict : missing value
  
property jKanji : missing value
  
property vOut : missing value
end script

script spd0
  property aList : {}
  
property bList : {}
  
property cList : {}
end script

property NSString : a reference to current application’s NSString
property NSCountedSet : a reference to current application’s NSCountedSet
property NSJSONSerialization : a reference to current application’s NSJSONSerialization
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding

set aTarg to "際"
set (aList of spd0) to getRelatedCharacters(aTarg) of me

set bTarg to "隆"
set (bList of spd0) to getRelatedCharacters(bTarg) of me

set (cList of spd0) to (aList of spd0) & (bList of spd0)

set cRes to returnDuplicatesOnly((cList of spd0)) of me
–> {"際", "障", "隠", "隣", "阪", "防", "阻", "附", "降", "限", "陛", "院", "陣", "除", "陥", "陪", "腹", "堕", "陰", "墜", "陳", "陵", "陶", "陸", "険", "陽", "隅", "隆", "隊", "階", "随", "隔", "隙"}

on returnDuplicatesOnly(aList as list)
  script spd1
    property bList : {}
    
property dupList : {}
  end script
  
  
set aSet to NSCountedSet’s alloc()’s initWithArray:aList
  
set (bList of spd1) to (aSet’s allObjects()) as list
  
  
set (dupList of spd1) to {}
  
repeat with i in (bList of spd1)
    set aRes to (aSet’s countForObject:i)
    
if aRes > 1 then
      set the end of (dupList of spd1) to (contents of i)
    end if
  end repeat
  
  
return (dupList of spd1)
end returnDuplicatesOnly

on getRelatedCharacters(aTarg as string)
  set aVector to parseKanjiToParts(aTarg) of me
  
  
set (vOut of jsonStr) to {}
  
repeat with i in aVector
    set j to contents of i
    
set tmpOut to searchKanjiFromElementJ(j) of me
    
if tmpOut is not equal to missing value then
      set (vOut of jsonStr) to (vOut of jsonStr) & tmpOut
    end if
  end repeat
  
  
set tmp2 to makeUniqueListFrom((vOut of jsonStr)) of me
  
  
return tmp2
end getRelatedCharacters

on cleanUp1DList(aList as list, cleanUpItems as list)
  script spd2
    property bList : {}
  end script
  
  
set (bList of spd2) to {}
  
repeat with i in aList
    set j to contents of i
    
if j is not in cleanUpItems then
      set the end of (bList of spd2) to j
    end if
  end repeat
  
return (bList of spd2)
end cleanUp1DList

on parseKanjiToParts(aTarg)
  set outList to {}
  
  
set kList to listupQueryKeysForKanji() of me
  
repeat with i in kList
    set j to contents of i
    
set aTargL to ((aJsonDict of jsonStr)’s valueForKey:j) as list
    
if aTarg is in aTargL then
      set the end of outList to j
    end if
  end repeat
  
return outList
end parseKanjiToParts

on searchKanjiFromElementJ(aQueryStr)
  if (aJsonDict of jsonStr) = missing value then my init()
  
if (jKanji of jsonStr) = missing value then my initJ()
  
set aRes to (aJsonDict of jsonStr)’s valueForKey:aQueryStr
  
if aRes = missing value then return missing value
  
  
set cArray to aRes’s arrayByAddingObjectsFromArray:(jKanji of jsonStr)
  
set cRes to returnDuplicatesOnly(cArray) of me
  
return cRes
end searchKanjiFromElementJ

on searchKanjiFromElement(aQueryStr)
  if (aJsonDict of jsonStr) = missing value then my init()
  
set aRes to (aJsonDict of jsonStr)’s valueForKey:aQueryStr
  
if aRes = missing value then return missing value
  
return aRes as list
end searchKanjiFromElement

on listupQueryKeysForKanji()
  if (aJsonDict of jsonStr) = missing value then my init()
  
set aRes to (aJsonDict of jsonStr)’s allKeys()
  
return aRes as list
end listupQueryKeysForKanji

–Pure AS風のパラメータ記述
on makeUniqueListFrom(theList)
  set theSet to current application’s NSOrderedSet’s orderedSetWithArray:theList
  
return (theSet’s array()) as list
end makeUniqueListFrom

on initJ()
  set (jKanji of jsonStr) to (current application’s NSArray’s arrayWithArray:(retJyouyouKanji() of jkLib))
end initJ

on init()
  –https://github.com/yagays/kanjivg-radical
  
set aPath to (POSIX path of (path to me)) & "Contents/Resources/element2kanji.json"
  
set jsonString to NSString’s alloc()’s initWithContentsOfFile:(aPath) encoding:(NSUTF8StringEncoding) |error|:(missing value)
  
set jsonData to jsonString’s dataUsingEncoding:(NSUTF8StringEncoding)
  
set (aJsonDict of jsonStr) to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
end init

★Click Here to Open This Script 

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

構成要素を指定して漢字検索 v4

Posted on 12月 13, 2021 by Takaaki Naganoya

漢字の構成要素を指定して漢字を検索するAppleScriptです。

ライブラリやデータを含むAppleScriptバンドルをダウンロードできるようにしておきます。

–> Download kanjiSearchFromPartsV4 (Inlcuding AppleScript Libraries)

macOS標準搭載の「関連文字に変換」に似たような動作を行います。「関連文字に変換」では、いったん漢字を構成要素に分解し、分解した要素をもとに共通漢字を検索するユーザーインタフェースになっていますが、こちらはダイレクトに構成要素部品を示して、そこから共通部品を持つ文字を検索します。

 {"氵", "艹", "田"}

をパラメータに指定して実行すると、

--> {"藩"}

という結果を返します。同様に、

 {"⻖", "生"} 

をパラメータに指定して実行すると、

--> {"隆"}

という結果を出します。日本語を学習中の他国語の話者の方とか、子供、漢字をそろそろ思い出せなくなってきているお年寄りなどにニーズがあると思われるプログラムです。

元になっている「kanjiVG」データの特性なのですが、大人がよく理解して使う分には問題ないものの、義務教育過程の小中学生が使うと、学校で教える漢字部品の指定の仕方と異なるので、「宿題のために使えてもテストの点数が落ちる」といった弊害があるかもしれません。

あとは、GUIがあったほうが使いやすいプログラムだと思われるため、電卓(計算機)のようなイージーなインタフェースをつけて道具に仕立てたほうがよいのでしょう。

本プログラムリストは、スクリプトエディタに転送できますが、バンドル形式で保存してデータやライブラリを含んだ形にしないと実行できないため、実行のためには本記事冒頭にあるダウンロードリンクからバンドルのアーカイブをダウンロードしてください。

AppleScript名:構成要素を指定して漢字検索 v4.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/02/21
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.7"
use framework "Foundation"
use scripting additions
use jkLib : script "jyoyoKanjiLib" –常用漢字一覧を返してくるAppleScriptライブラリ

property NSString : a reference to current application’s NSString
property NSCountedSet : a reference to current application’s NSCountedSet
property NSJSONSerialization : a reference to current application’s NSJSONSerialization
property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding

script jsonStr
  property aJsonDict : missing value –JSONから読み取ったNSDictionary
  
property jKanji : missing value –常用漢字データ(NSArray)
end script

–図形としての構成要素を指定して漢字検索(厳密にいえば部首ではない)
set p1List to {"氵", "艹", "田"}
set c1Res to searchKanjiFromElementListJ(p1List) of me
–> {"藩"}

set p2List to {"⻖", "生"} –こざとへん。データ内のこざとへんとおおざとの区別がよくわからなかった
set c2Res to searchKanjiFromElementListJ(p2List) of me
–> {"隆"}

–検索に使える部首のキー文字の一覧を返す
–set qList to listupQueryKeysForKanji() of me
–> {"工", "棘", "左", "位", "婁", "攴", …}

on searchKanjiFromElementListJ(aQueryList as list)
  script spd
    property aList : {}
    
property bList : {}
  end script
  
  
set aLen to length of aQueryList
  
if aLen = 1 then return searchKanjiFromElementJ(first item of aQueryList) of me
  
  
set (aList of spd) to {}
  
set (bList of spd) to {}
  
repeat with i in aQueryList
    set j to contents of i
    
set tmpRes to searchKanjiFromElementJ(j) of me
    
set the end of (aList of spd) to tmpRes
  end repeat
  
  
return searchDuplicateItemsOnlyIn2DList((aList of spd)) of me
  
end searchKanjiFromElementListJ

on searchDuplicateItemsOnlyIn2DList(allList as list)
  set aLen to length of allList
  
  
set aList to contents of first item of allList
  
set bList to contents of rest of allList
  
  
repeat with i in bList
    
    
set j to contents of i
    
    
–aListとbListを連結したListで重複が存在するかをチェック
    
set aArray to (current application’s NSArray’s arrayWithArray:aList)
    
set bArray to (current application’s NSArray’s arrayWithArray:j)
    
set cArray to (aArray’s arrayByAddingObjectsFromArray:bArray)
    
    
set aList to returnDuplicatesOnly(cArray, aLen) of me
    
  end repeat
  
  
return aList
end searchDuplicateItemsOnlyIn2DList

on searchKanjiFromElementJ(aQueryStr)
  if (aJsonDict of jsonStr) = missing value then my init()
  
if (jKanji of jsonStr) = missing value then my initJ()
  
set aRes to (aJsonDict of jsonStr)’s valueForKey:aQueryStr
  
if aRes = missing value then return missing value
  
  
set cArray to aRes’s arrayByAddingObjectsFromArray:(jKanji of jsonStr)
  
set cRes to returnDuplicatesOnly(cArray) of me
  
return cRes
end searchKanjiFromElementJ

on searchKanjiFromElement(aQueryStr)
  if (aJsonDict of jsonStr) = missing value then my init()
  
set aRes to (aJsonDict of jsonStr)’s valueForKey:aQueryStr
  
if aRes = missing value then return missing value
  
return aRes as list
end searchKanjiFromElement

on listupQueryKeysForKanji()
  if (aJsonDict of jsonStr) = missing value then my init()
  
set aRes to (aJsonDict of jsonStr)’s allKeys()
  
return aRes as list
end listupQueryKeysForKanji

on initJ()
  set (jKanji of jsonStr) to current application’s NSArray’s arrayWithArray:(retJyouyouKanji() of jkLib)
end initJ

on init()
  –https://github.com/yagays/kanjivg-radical
  
set aPath to (POSIX path of (path to me)) & "Contents/Resources/element2kanji.json"
  
set jsonString to NSString’s alloc()’s initWithContentsOfFile:(aPath) encoding:(NSUTF8StringEncoding) |error|:(missing value)
  
set jsonData to jsonString’s dataUsingEncoding:(NSUTF8StringEncoding)
  
set (aJsonDict of jsonStr) to NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
end init

on returnDuplicatesOnly(anArray)
  set aSet to NSCountedSet’s alloc()’s initWithArray:anArray
  
set bList to (aSet’s allObjects()) as list
  
  
set dupList to {}
  
repeat with i in bList
    set aRes to (aSet’s countForObject:i)
    
if aRes > 1 then
      set the end of dupList to (contents of i)
    end if
  end repeat
  
  
return dupList
end returnDuplicatesOnly

★Click Here to Open This Script 

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

2021年に書いた価値あるAppleScript

Posted on 12月 10, 2021 by Takaaki Naganoya

2021年:macOS 12(自分はmacOS 11、12を使用)

毎年行なっている、Piyomaru Softwareが書いたAppleScriptの1年を振り返る記事の2021年版です。恒例行事です。

あいかわらずコロナ禍の影響で踏んだり蹴ったりの1年だったので、本を書くことに注力。これではPiyomaru SoftwareではなくPiyomaru Publishingだ、などと冗談を言っていましたが、年に13冊出して進行中のものが数冊あるという状況。本当にPiyomaru Publishingです。

→ 2018年に書いた価値あるAppleScript
→ 2019年に書いた価値あるAppleScript
→ 2020年に書いた価値あるAppleScript

本を短期間かつ1人で書いて、レイアウトして、電子書籍として仕上げるためには、それを補助する大量のAppleScriptが必要です。面倒な作業はとっととAppleScriptで自動化して、繰り返し行う煩雑な作業をMacに行わせる環境を整備してきました。逆に、そういうScriptを用意できているので作業を省力化して、書けているという状況があるともいえます。AppleScriptによる補助なしにはこんなペースで書くことは不可能でしょう。

今年書いた本の中でどれがどうということはないんですが、Cocoa Scripting本は書き始める前には、一体どういうものを書いてよいのかさっぱり分かりませんでした。戦場の絆本だってそうです。どれひとつとして、企画段階のままの内容で完成したものなどありません。

2021/1 FileMaker Pro Scripting Book with AppleScript macOS 12対応アップデート
2021/2 [Eng] FileMaker Pro Scripting Book with AppleScript
2021/3 Cocoa Scripting Course Volume #1 NSString
2021/3 Cocoa Scripting Course Volume #2 NSArray
2021/3 Macとアップルスクリプトで学ぶ いまからはじめるプログラミング①
2021/4 Macとアップルスクリプトで学ぶ いまからはじめるプログラミング②
2021/4 Cocoa Scripting Course Volume #3 NSDictionary
2021/5 ミュージック.app scripting book with AppleScript
2021/7 機能強化AppleScript集 CotEditor用 PowerPack 取扱説明書
2021/7 空前絶後あなたの知らない ヤバイAppleScriptの世界
2021/8 elgato STREAM DECK 徹底活用 Mac+STREAM DECKで時短+作業効率化!!
2021/9 空前絶後あなたの知らないヤバイAppleScriptの世界 iOS対応版
2021/10 アーケードゲーム「戦場の絆」僕らの15年戦争

書いた本はどれも「こんなものがあればいいのに!」というアイデアを形にしたものばかり(読者ニーズを形にしたものではないところにギャップはありますが、、、)。新基軸は、iOS用のFileMaker Goを電子書籍プラットフォームと見立てて、各種情報+ロジックを1つにまとまた本を出したことです。「iOSで読む」本についてはいろいろ構想もありますが、まだ試行錯誤の最中といったところでしょう。

FileMaker Pro系のコンテストには「FileMaker選手権2020」「FileMaker選手権2021」「FM-1グランプリ」の3つに応募し、FileMaker選手権2020ではDropbox賞を3つ(3年分)、FM-1グランプリでは仕事効率化部門で部門準賞をいただいています。

■BEST AppleScript of 2021

今年書いたAppleScriptの中で、個人的に一番会心の出来だったのは「部首で漢字検索」シリーズです。外部のデータに依存してはいるものの、テキスト中の漢字を部首で検索できるというのは、なかなかすごいことです。

技術レベル云々を問題にしなければ、macOS 11+M1 MacでAppleScriptの動作速度が遅いことを検証したサンプルAppleScriptと報告書のまとめでしょうか。おそらく、他にも同じようなレポートが行われていたところに、きちんとベンチマークをとって動作の異様さを報告したものが役立ったが、おそらくその前からApple社内で当たりはつけていたのでしょう。

M1 Mac miniが到着してすぐベンチマークを回して「なんだこの遅さは???」と気づいたぐらいだったので、半年前のWWDC以降に開発者向けに貸し出されたDTK(Developer Transition Kit、A12Bionic搭載マシン)を試していた人たちは揃いも揃ってみんな本当に気づかなかったんだろうか? という素朴な疑問があります。

M1上でのAppleScriptからのCocoa呼び出しの速度がmacOS 11.x上のレベルのままだったら、と思うとなかなか怖いものがあります。

■2021/1

FileMaker Pro Scripting Bookの英語版を書いていたので、こうしたものが必要になりました。日本語のままで未翻訳の文章が残っていると困るので、そのチェックのためのツールを書いてみたというところです。
1/28 最前面のKeynote書類のテキストアイテムの英語化率を求める

■2021/2

部首で漢字検索を行えるAppleScriptは、もともとのデータを作った人が偉いわけですが、実際に手元で用意したテストデータに対して実行し、その処理内容を正しく評価してAppleScriptから呼び出せるようにしたところに価値があるものと考えます。
2/21 部首で漢字検索
2/24 指定した文字で囲まれたキーワードの色を置換する

■2021/3

環境を用意するだけで手間がかかる超解像処理が、PixelMator Proで手軽に呼び出せるようになっていることに感服します。本処理は実に有用です。
3/25 画面スナップショット超解像バッチ処理 v1

■2021/4

Pagesの書類から最大サイズの文字で書かれている文章をタイトルとして取り出す処理を行うAppleScriptです。せめてPagesでタイトルの書式属性に該当するテキストを抽出できるようになっているとよいのですが、そういう機能は一切ないので困ります。
4/12 Pagesで最前面の書類中のテキストアイテムと本文テキストで文字サイズが最大のもののテキストを求める

■2021/5

Music.app本に掲載するために書いたものですが、なかなか苦労しました。AppleScriptからデータを取得したい内容です。
5/12 LAN上のdaapクライアントの共有名をリストアップ v2

■2021/6

M1 Mac miniを手にしたその日に強烈な違和感をおぼえ、ベンチマークを用意して実行してみました。これをやっていなかったら、macOS 12でもAppleScriptからのCocoa呼び出しは遅いままだったでしょう。技術的に高度かどうかはさておき、これをやらなかったら全世界的に致命的な影響が出たであろうことを考えると身震いします。
6/22 macOS 11, AppleScriptをFirestormではなくIcestormで実行か?!

■2021/7,8

とくになし。この頃が一番忙しかったので。

■2021/9

電子書籍「僕らの15年戦争」のために用意したものです。Pagesは長大なコンテンツの編集にまったく向いておらず、なるべく記事ごとに小割りに書類を分けないと困ります。そのため、全書類に対して同じ修正を行いたいといった場合、手作業で行うのは現実的な方法ではありません。AppleScriptから一括処理することが必須です。
9/22 Pagesの最前面の書類で選択中のツメを修正する

■2021/10

macOS 12で新規搭載されたショートカットをAppleScriptから呼び出したりいろいろ試し出しました。ただ、ショートカットの完成度がいまひとつというべきなのか、「よくまあこれでいままでiOSユーザーは文句を言わなかったもんだ」と呆れるほどのいい加減な出来に言葉を失っています。
10/27 AppleScriptからショートカット実行&ショートカット内でAppleScriptを実行

■2021/11

macOS 12上でショートカットをAppleScriptの処理内で実行するため、存在確認とインストールまで自動で行うことを試したAppleScriptです。まだ完全体とは言い難いですが、方向性はいいと感じています。
11/1 Shortcuts Eventsでショートカットのインストール+実行

電子書籍の作成補助Scriptの1つ。表のセルに入れた丸つき数字のリナンバー処理は、表コンテンツを編集すると必ず発生していた無意味な作業であり、これを手軽に行えるようにしたことは意義深いでしょう。
11/27 選択中の表の指定行・列のマル付き数字リナンバー v2

Posted in news | Tagged 10.15savvy 11.0savvy 12.0savvy | 2 Comments

Microsoftが新OutlookにAppleScript機能を追加表明

Posted on 12月 9, 2021 by Takaaki Naganoya

Outlook: Support for AppleScript in the New Outlook for Mac
Planned Availability: June CY2022 https://t.co/bGOxW8leZG #M365Roadmap #Outlook #NewRoadmapItem

— Microsoft365Roadmap (@M365Roadmap) December 8, 2021

Microsoft 365のロードマップを流布するTwitterアカウント@M365Roadmapが、Mac用のOffice365(ローカルで動かす版のOffice、月単位契約可)の新たなロードマップとして、新バージョンのOutlookに対して、2022年の6月を目処にAppleScript対応機能を追加することを発表しました。

Microsoft Outlook for Macは企業内のExchangeサーバーにアクセスするクライアントで、筆者の手元にある(契約していないので置いてあるだけ)バージョンをアップデートしたところ、バージョン16.55でAppleScript用語辞書もついています。

Microsoftが言うところの「新Outlook」というのは、バージョン17.xxというものなのか、あるいはそれ以上のバージョン番号がついたものなのかは分かりませんが、Outlook v16.xx系を使っていればAppleScriptサポートはあるので(Exchangeに用がないのでOutlookのScriptingしたことないですけど)、それまで16.xx系を使い続けてもよいのでしょう。

また、本件はあくまで計画段階の話なので、確度のほどは不明です。

Posted in news | Tagged 11.0savvy 12.0savvy Outlook | Leave a comment

アンダースコアが入っていたら削除して次の文字を大文字化

Posted on 12月 6, 2021 by Takaaki Naganoya

文字列の途中にアンダースコア(「_」)が入っていたら削除し、その次の文字列(たぶんアルファベット)を大文字化するAppleScriptです。

github上でたまに「あ、これいいかも」というObjective-Cで書かれたプログラムを見つけた場合に、Cocoa Frameworkとしてビルドして外部からAppleScriptから呼び出したくなります。

しかし、メソッド名にアンダースコア(「_」)が入っていて断念したことが、何回かあります。

AppleScript、とくにCocoaを呼び出すAppleScriptObjCの世界では、そうしたCocoa Framework中のメソッドにアンダースコアが入っていると、「|」でエスケープしても展開されて、パラメータがそこに入るものとして解釈されてしまいます。つまり、すべてのソースを書き換えなくてはならないわけです。

ここで、単純にアンダースコアを削除しただけでは可読性が落ちるので、アンダースコアの後にあった文字(おそらくアルファベット)を大文字化したいわけですが、なかなか手が混んでいて大変な処理です。

そこで、AppleScriptを書いて処理させるためのテストプログラムを作ってみました。1行分の処理はできているように見えます。

この種類のプログラムは、何回か組んだ記憶があるのですが、いつも大して役に立たずに立ち消えになっているような気が、、、、やっぱり、Xcode自体のAppleScript対応度が散々な出来で、やりたい処理ができずにどこかに消えてしまうから、なのかも、、、、

AppleScript名:アンダースコアが入っていたら削除して次の文字を大文字化.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/12/06
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set a to "- (void)patch_splitMax_toMax:(NSMutableArray *)patches;"
set aTarg to "_"

set b to removeAcharAndCapitalize(a, aTarg) of me
–> "- (void)patchSplitMaxToMax:(NSMutableArray *)patches;"

on removeAcharAndCapitalize(aStr, aTargChar)
  if aStr contains aTargChar then
    set bList to countAcharInStrAndDetectPos(aStr, aTargChar) of me –1行に複数のアンダースコアが登場する場合に対処
    
copy bList to {countNum, itemList}
    
    
set revList to reverse of itemList –後方から処理
    
    
repeat with i in revList –アイテム番号でループ(後方から処理)
      set a1Str to text 1 thru (i – 1) of aStr
      
set bStr to text (i + 1) thru -1 of aStr
      
set bStr2 to capitalizeHeadChar(bStr) of me
      
set aStr to a1Str & bStr2
    end repeat
  end if
  
  
return aStr
end removeAcharAndCapitalize

–指定文字列の頭文字を大文字化
on capitalizeHeadChar(aStr)
  set a1Str to text 1 of aStr
  
set a2Str to text 2 thru -1 of aStr
  
  
set a1aStr to current application’s NSMutableString’s stringWithString:a1Str
  
set a1bStr to (a1aStr’s uppercaseString()) as string
  
  
return (a1bStr & a2Str)
end capitalizeHeadChar

–文字列中に指定文字が何個入っているかカウントし、登場位置をリストで返す
on countAcharInStrAndDetectPos(aStr, aTargChar)
  set aList to {}
  
set posC to 1
  
  
considering case
    set aHit to offset of aTargChar in aStr
    
    
if aHit is not equal to 0 then
      set aaList to characters of aStr
      
set aCount to 0
      
      
repeat with i in aaList
        
        
set j to contents of i
        
        
if j = aTargChar then
          set aCount to aCount + 1
          
set the end of aList to posC
        end if
        
        
set posC to posC + 1
        
      end repeat
      
      
return {aCount, aList}
    else
      return {0, {}}
    end if
  end considering
end countAcharInStrAndDetectPos

★Click Here to Open This Script 

Posted in Text | Tagged 10.15savvy 11.0savvy 12.0savvy | 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

Amazon EC2 M1 Macインスタンスが利用可能に

Posted on 12月 3, 2021 by Takaaki Naganoya

Amazon AWSでMacインスタンスが利用可能になって、約1年。当初はMacインスタンスにはMac mini 2018(Intel Mac)が採用されていましたが、M1 Mac miniを採用した「Amazon EC2 M1 Macインスタンス」が本日から利用可能になったと報じられています。

「利用可能なM1 Mac miniのスペックは8CPUコア、8GPUコア、16GBメモリ、16コアApple Neural Engine。これがThunderbolt経由で10GbpsのVPCネットワークと8GB EBSストレージに接続されています。」

とのこと。OSバージョンが書かれていませんが、macOS 11.xか12.xということになるでしょう。

サービス開始当初からM1 Macに言及していた割に提供が1年遅れたことについては、やはり「(AppleScriptや)shell scriptのようなプロセスを決め打ちで遅い高効率コア(IceStorm)で実行してしまう」というmacOS 11.x+M1の問題点(バグといってもいい)がmacOS 12で解消されるのを待っていたというところでしょうか?

ただ、基本単位が24時間で、それを超える分については1秒単位の課金。「瞬間的にMacを大量に用意して大量のデータを処理したい!」という用途にはいまひとつ(費用面で)使いにくいサービスでもあります。

最小単位が1時間とかだったら、いろいろ使ってみようという気になりますが、AppleのEULAをクリアするためにこのようなサービス形態になっているとかいないとか。

あらかじめ起動ディスクイメージを用意しておいて、必要なときに新規インスタンスをこれで起動し、所定の処理を行ったあとでデータをストレージに書き戻すとかいった使い方になることでしょう。

最小単位が24時間というのが、かえすがえすも……。

AppleScriptでこうしたサービスを利用する機会があるのかと言われれば、膨大な量のPDFを処理する必要がある場合などに、一時的にAWS上のインスタンスをかき集めて一気に処理するといったものが考えられるでしょう。

膨大なKeynoteやPagesのデータを処理する必要があって、そのデータ処理を行う……というケースもないことはなさそうですが、別にAWSを利用しなくても処理できそうな気もします(データ量次第です)。

Posted in news | Tagged 11.0savvy 12.0savvy AWS | 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

1D Listに入っているテキストから丸つき数字を抽出して数値化し、最小のものを求める

Posted on 11月 26, 2021 by Takaaki Naganoya

1D List(1D Array)に入っているテキスト要素から、丸つき数字を抽出して数値化し、得られた数値のうち最小のものを求めるAppleScriptです。

こんな、丸つき数字のテキストが入った「表」をKeynote上に作成したときに、

項目を追加したり順番を入れ替えたりすると、セルの中に入れている丸つき数字も変更する必要があります。これが手作業でたいへんめんどくさいので、Scriptで処理できるようにサブルーチンを整備してみました。

やりたいことは割とシンプルなはずなのに、割といろいろ書かないと実現しない処理でもあります。

そして、本Scriptは置換対象のデータから丸つき数字部分を取り出して、最小のものを判別し、リナンバー時の開始値を既存のデータから推測するという処理のために作ったものです。

AppleScript名:1D Listに入っているテキストから丸つき数字を抽出して数値化し、最小のものを求める.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/11/26
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set aList to {"❹Script Editor", "②Xcode", "③ショートカット", "④Script Debugger④", "⑤SD Notary", "⑥UI Browser", "⑦Accessibility Inspector"}
set aSmallestNum to getMinimumNumFromNumberWithSign(aList) of me
–> 2

–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 numStr6 to "⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇"
  
–set numStr7 to "1︎⃣2︎⃣3︎⃣4︎⃣5︎⃣8︎⃣9︎⃣"
  
–set numStr8 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 Number Text | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

文字列から丸つき数字のみ抽出する

Posted on 11月 26, 2021 by Takaaki Naganoya

指定の文字列から丸つき数字の部分だけを抽出するAppleScriptです。

丸つき数字の文字を数値に変換するサブルーチンのために作成したものです。

AppleScript名:文字列から丸つき数字のみ抽出する.scpt
–  Original by: Shane Stanley
–  Created on: 2019/11/04
–  Modified by: Takaaki Naganoya
–  Modified on: 2021/11/26

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

set aRes to holdNumberWithSignOnly("⑦Accessibility Inspector") of me
–> "⑦"

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

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

丸つき数字を数値にデコードする v2

Posted on 11月 26, 2021 by Takaaki Naganoya

丸つき数字を数値にデコードするAppleScriptです。よくよく考えるとこれまでに作っていなかったので、作っておきました。

AppleScript名:丸つき数字を数値にデコードする v2.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/11/26
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

set dRes to decodeNumFromNumWithSign("⑮") of me
–> 15

–丸つき数字を数値にデコードする v2
on decodeNumFromNumWithSign(aStr as string)
  set numStr1 to "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿"
  
set numStr2 to "❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴"
  
set numStr3 to "➀➁➂➃➄➅➆➇➈➉"
  
set numStr4 to "➊➋➌➍➎➏➐➑➒➓"
  
set numStr5 to "⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾"
  
–set numStr6 to "⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇"
  
–set numStr7 to "1︎⃣2︎⃣3︎⃣4︎⃣5︎⃣8︎⃣9︎⃣"
  
–set numStr8 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 Number Text | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy 11.0savvy | Leave a comment

丸つき数字の除去と数値からの変換

Posted on 11月 26, 2021 by Takaaki Naganoya

丸つき数字を指定文字列から削除するAppleScriptと、1〜50の範囲の数値を丸つき数字に変換するAppleScriptです。

Keynoteの表に入っている丸つき数字のリナンバーを行うために、必要な処理を書いてみたものです。日本語環境でしかこういう文字は使わないことでしょう。

丸つき数字が文字コード上でところどころ切れているため、単純に調べるのに手間がかかりました。

AppleScript名:数字を丸つき数字に変換.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/11/26
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

repeat with i from -10 to 100 by 1
  set aStr to convNumToNumWithSign(i) of me
  
log {i, aStr}
end repeat

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

★Click Here to Open This Script 

AppleScript名:丸つき数字を検出・削除
–  Original by: Shane Stanley
–  Created on: 2019/11/04
–  Modified by: Takaaki Naganoya
–  Modified on: 2021/11/26

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

set aRes to removeNumberWithSign("⓪①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿⓿❶❷❸❹❺❻❼❽❾❿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴➀➁➂➃➄➅➆➇➈➉➊➋➌➍➎➏➐➑➒➓") of me
–> "‍‍‍‍‍‍"

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

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.13savvy 10.14savvy 10.15savvy 11.0savvy 12.0savvy | Leave a comment

AS Publisher v18–> v19

Posted on 11月 7, 2021 by Takaaki Naganoya

とくに新しいわけでもなんでもありませんが、「スクリプトエディタでオープン中のScriptをリンクつきのHTMLとして書き出すAppleScript」である「AS Publisher」の機能改修版です。前バージョンまではURLエンコーディング処理にpythonを呼び出して処理していましたが、Cocoaの機能を呼び出すように書き換えました。

# v18に処理のミスがあり、そのミスがOS側で「こういう間違いが多いから通しておこう」と見逃されていたのが厳密に処理されるようになったため、ミスの部分を修正しておきました

AppleScriptのリストをBlogなどに掲載する場合、WordPressやMarkdownの構文自動認識フォーマットを使って掲載されているものを見かけますが、そもそも「return return」(改行コードを返す)といった記述が許可されており、それらがきちんと構文色分けされているAppleScriptにおいて、単なるキーワードによる構文自動認識フォーマットが役立つわけがありません(知らずによく実装したもんですわ>オリジナル版を作った人)。

それ以前に「必要があったので」作ったAppleScriptでもあります。手元に残っている最古のバージョンは2005年の4月1日のタイムスタンプになっていますが、おそらくそれ以前から作っていたものでしょう。

# retMacOSpathList ハンドラで、ファイルパスをやたらとCastしている箇所がありますが、Mac OS X 10.5より前のAppleScriptではファイルパスの文字列のエンコーディングに問題を抱えていた(AppleScript側の文字列とのエンコーディング/ノーマライズの不整合)ので、その対策です。ただし、不必要なぐらい無駄な処理をしているので、参考にはなりません。Mac OS X初期の試行錯誤を強いられていた時代の遺物とでも思ってください

本Scriptおよびその派生版は、macOS標準装備のScript Menuに入れて実行することを想定しています。AppleScriptからスクリプトエディタを操作する場合に、Mac OS X 10.4の時代ではあまり動作が安定せず、もう少し後のバージョンのOSになるまで(10.5ぐらい?)、たいへんスリリングな(クラッシュが頻発する)Scriptingを強いられていました。

いまはそのあたりは安定していますが、ごく初期においては挙動が怪しい部分があったということです(その時期で知識が止まっている人はそういう認識のまま)。

話を戻しましょう。

AppleScriptのプログラムをBlogなどに掲載する際に、自分はこういう道具を作って使用しています。巨大なリストを掲載する場合には記事の文字数が膨大になってしまい、掲載をあきらめたこともありましたが、ほんのごく一部です。

このScriptをもとに、自分が使いやすいように(色指定とか)変更してみてもよいでしょう。

「動けばいい」ぐらいの割り切りで、秘伝のタレに継ぎ足してメンテナンスしてきましたが、HTMLタグで書式制御するよりはCSSで制御したほうがシンプルにできてよさそうな感じもします。

ファイル処理も、とくに「ここまでやる必要ってあったっけ?」という感想を持ってしまいます。なにぶん、2006年ごろに作ったものなので、自分で自分のプログラムを見ても違和感がものすごいですね。「OSの処理に対する不信感」がそこかしこに漂っています、、、、

AppleScript名:AS Publisher v19.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2014/11/09
–  Modified on: 2021/11/07
—
–  Copyright © 2006-2021 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property quotChar : string id 34

property headerCol : "0000CC" –"0000CC" –ヘッダー部分(濃い色)
property bodyBackCol : "EEFFFF" –"EEFFFF" –Script本文下地(薄い色)
property footerCol : "66FFFF" –"66FFFF" –スクリプトリンク部分

set a to path to me

set pName to "com.apple.ScriptEditor2"

using terms from application "Script Editor"
  tell application id pName
    if (count every document) = 0 then
      display dialog "スクリプトをエディタでオープンした状態で実行してください。" buttons {"OK"} default button 1 with icon 1
      
return
    end if
    
    
set aP to path of front document
    
if aP = "" then
      display dialog "スクリプトを保存してから実行してください。" buttons {"OK"} default button 1 with icon 1
      
return
    end if
    
    
tell front document
      set aInfo to properties
      
set curLang to name of language of aInfo –現在のOSA言語の名称を取得する
    end tell
    
    
–OSA Language名称をもとに色セットを変更する
    
changeColor(curLang) of me
    
    
set c to name of front document
    
    
set aF to retMacOSpathList(aP) of me
    
set htmlFilePath to retHTMLpath(aF) of me
    
    
set contText to contents of front document
    
set encText to makeEncodedScript(contText) of me
    
    
set newLinkText to "applescript://com.apple.scripteditor?action=new&script=" & encText
    
–set insLinkText to "applescript://com.apple.scripteditor?action=insert&script=" & encText
    
–set apndLinkText to "applescript://com.apple.scripteditor?action=append&script=" & encText
    
    
set comText to description of front document
    
    
set textList to every attribute run of front document
    
set textList_ref to a reference to textList
    
    
set colorList to color of every attribute run of front document
    
set colorList_ref to a reference to colorList
    
    
set aTest to contents of item 2 of textList
    
set asciiTest to id of aTest
  end tell
end using terms from

set tabChar to string id 9

–set TIDsList to {{"&", "&amp;"}, {">", "&gt;"}, {"<", "&lt;"}, {"¥"", "&quot;"}}
set TIDsList to {{"\\", "\"}, {"’", "’"}, {"&", "&amp;"}, {">", "&gt;"}, {"<", "&lt;"}, {"  ", "  "}, {string id 13, string id 10}, {string id 13, "<br>"}, {string id 10, "<br>"}, {"\"", "&quot;"}}
–set TIDsList to {{"&", "&amp;"}, {">", "&gt;"}, {"<", "&lt;"}, {"¥"", "&quot;"}}

set TIDsList_ref to a reference to TIDsList

set dataOut to {}
set dataOut_ref to a reference to dataOut

set iCounter to 1
repeat with i in textList_ref
  set j to contents of i
  
  
set curDelim to AppleScript’s text item delimiters
  
repeat with eachItem in TIDsList_ref
    set AppleScript’s text item delimiters to contents of item 1 of eachItem
    
set j to every text item of j
    
set AppleScript’s text item delimiters to contents of item 2 of eachItem
    
set j to j as string
  end repeat
  
set AppleScript’s text item delimiters to curDelim
  
  
set cText to RBG2HTML(item iCounter of colorList_ref) of me
  
  
set the end of dataOut_ref to "<font color=" & cText & ">" & j & "</font>"
  
set iCounter to iCounter + 1
end repeat

set htmlHeader to "<table width=" & quotChar & "100%" & quotChar & " border=" & quotChar & "0" & quotChar & "cellspacing=" & quotChar & "2" & quotChar & " cellpadding=" & quotChar & "2" & quotChar & ">
<tr>
<td bgcolor=\"#" & headerCol & "\"><font color=" & quotChar & "#FFFFFF" & quotChar & ">" & curLang & "名:" & c

if comText is not equal to "" then
  set comText to "<br><font size=" & quotChar & "2" & quotChar & ">【コメント】 " & comText & "</font><br>"
end if

set htmlHeader2 to "</font></td>
</tr>
<tr>
<td bgcolor=\"#" & bodyBackCol & "\"><font size=\"3\">"

set htmlFooter1 to "</font></td>
</tr>
<tr>
<td bgcolor=\"#" & footerCol & "\"><p><font size=\"2\"><a href=\"" & newLinkText & "\">★Click Here to Open This Script</a> </font></p>
</td>
</tr>
</table>
"

set dataText to htmlHeader & comText & htmlHeader2 & (dataOut as text) & htmlFooter1
set dataText to dataText as Unicode text

–set dDir to (path to desktop as text) & "index.html"
–write_to_file(dataText, htmlFilePath, false) of me
–saveInEncoding(dataText, htmlFilePath, "UTF8") of me –UTF8
write_to_file_Safely(dataText, htmlFilePath) of me

tell application id "com.apple.ScriptEditor2"
  display dialog "HTML書き出し完了" buttons {"OK"} default button 1 with icon 1 giving up after 1
  
return
end tell

on retHTMLpath(aF)
  tell application "Finder"
    –set aF to retMacOSpathList(aP) of me
    
set afA to aF as alias
    
set aInfo to info for afA
    
set aExt to name extension of afA
    
    
set aFileName to name of afA
    
set aFileName to aFileName as text
    
set aFileName to aFileName as Unicode text
    
    
set aFileName_rev to reverse of (every character of aFileName) as text
    
set parentF to ((parent of afA) as alias) as text
    
set htmlFileName to name of afA
    
set htmlFileName to htmlFileName as text
    
set htmlFileName to htmlFileName as Unicode text
    
    
    
if aExt is not equal to "" then
      set htmlFileNameText to ((reverse of (items ((length of aExt) + 2) thru -1 of aFileName_rev)) as text) & ".html"
    else
      set htmlFileNameText to (aFileName as text) & ".html"
    end if
    
set htmlFileNameFullPath to parentF & htmlFileNameText
  end tell
  
return htmlFileNameFullPath
end retHTMLpath

on makeEncodedScript(contText)
  (*

  set aList to every paragraph of contText
  set aClass to class of aList
  if aClass = list then
    set aLen to length of aList
  else
    set aLen to 1
  end if
  
  set aaList to {}
  
  set delim to AppleScript’s text item delimiters
  set AppleScript’s text item delimiters to "//piyomaru_replacement_temp//"
  set bList to aList as text
  set AppleScript’s text item delimiters to delim
  
  set aaList to (encodeURL(bList) of me) as Unicode text
  
  set search_string to "//piyomaru_replacement_temp//" as Unicode text
  set replacement_string to "%0A" as Unicode text
set bList to replace_chars(aaList, search_string, replacement_string) of me
*)

  
  
set aaList to (encodeURL(contText) of me) as Unicode text
  
  
return aaList
end makeEncodedScript

–RGB値からHTMLの色指定に変換
on RBG2HTML(RGB_values)
  — NOTE: this sub-routine expects the RBG values to be from 0 to 65536
  
set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
  
set the the hex_value to ""
  
repeat with i from 1 to the count of the RGB_values
    set this_value to (item i of the RGB_values) div 256
    
if this_value is 256 then set this_value to 255
    
set x to item ((this_value div 16) + 1) of the hex_list
    
set y to item (((this_value / 16 mod 1) * 16) + 1) of the hex_list
    
set the hex_value to (the hex_value & x & y) as string
  end repeat
  
return ("#" & the hex_value) as string
end RBG2HTML

–安全なファイルの書き出し
on write_to_file_Safely(this_data, target_file)
  tell current application
    set finalPath to target_file
    
    
–テンポラリフォルダに一度書き出して、ターゲットのパスに移動  
    
set tmpFol to path to temporary items from system domain
    
set tmpName to do shell script "date +%Y%m%d%H%M%S"
    
set tmpPath to (tmpFol as string) & tmpName
    
    
–テンポラリのパスにファイル書き出し
    
try
      set the target_file to the tmpPath as text
      
set this_data to this_data as Unicode text
      
      
set the open_target_file to open for access file target_file with write permission
      
set eof of the open_target_file to 0 –つねにoverwrite mode(appendではなくて)
      
write this_data to the open_target_file starting at eof as «class utf8»
      
close access the open_target_file
      
    on error error_message
      try
        close access file target_file
      end try
      
return error_message
    end try
    
    
–ファイルの移動を行う
    
set tFilePOSIX to quoted form of POSIX path of target_file
    
set fPathPOSIX to quoted form of POSIX path of finalPath
    
set sText to "mv " & tFilePOSIX & " " & fPathPOSIX
    
try
      do shell script sText
    on error error_message
      return error_message
    end try
    
    
return true
  end tell
end write_to_file_Safely

on replace_chars(this_text, search_string, replacement_string)
  set AppleScript’s text item delimiters to the search_string
  
set the item_list to every text item of this_text
  
set AppleScript’s text item delimiters to the replacement_string
  
set this_text to the item_list as string
  
set AppleScript’s text item delimiters to ""
  
return this_text
end replace_chars

on encodeURL(origStr as string)
  set aStr to current application’s NSString’s stringWithString:origStr
  
set encodedStr to aStr’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s alphanumericCharacterSet())
  
return encodedStr as string
end encodeURL

on retMacOSpathList(aFile)
  set aPath to POSIX file aFile
  
set aPath to aPath as alias
  
set aPath to aPath as string
  
set aPath to aPath as Unicode text
  
set aliasFileList to aPath
  
return aliasFileList
end retMacOSpathList

–ファイルの追記ルーチン「write_to_file」
–追記データ、追記対象ファイル、boolean(trueで追記)
on write_to_file(this_data, target_file, append_data)
  tell current application
    try
      set the target_file to the target_file as text
      
set the open_target_file to open for access file target_file with write permission
      
if append_data is false then set eof of the open_target_file to 0
      
write this_data to the open_target_file starting at eof
      
close access the open_target_file
      
return true
    on error error_message
      try
        close access file target_file
      end try
      
return error_message
    end try
  end tell
end write_to_file

on changeColor(aLang)
  if aLang = "AppleScript" then
    set headerCol to "0000CC" –"0000CC" –ヘッダー部分(濃い色)
    
set bodyBackCol to "EEFFFF" –"EEFFFF" –Script本文下地(薄い色)
    
set footerCol to "66FFFF" –"66FFFF" –スクリプトリンク部分
    
  else if aLang = "JavaScript" then
    set headerCol to "804000" –"0000CC" –ヘッダー部分(濃い色)
    
set bodyBackCol to "E2D3D3" –"EEFFFF" –Script本文下地(薄い色)
    
set footerCol to "E7AC53" –"66FFFF" –スクリプトリンク部分
  end if
end changeColor

★Click Here to Open This Script 

Posted in OSA Text | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy Script Editor Script Menu | 15 Comments

Stream Deck Softwareがバージョン5.1.2にバージョンアップ

Posted on 10月 16, 2021 by Takaaki Naganoya

ElgatoのStream Deck用ソフトウェアがバージョン5から5.1.2にバージョンアップしました。

従来は1台のStream Deckに対してプロファイルを複数登録して、「プロファイルを切り替え」アクションで1→2→3→4→1と、順送りで切り替えして、物理ボタンが15個であってもボタン数を超える機能を呼び出せるようにしてありました。

この仕組みはあらためて説明されると理解できるのですが、一目見て理解できるというものでもありません。自分も書籍を書くために仔細に調査を行うまで分かりませんでした。このあたりの「説明不足感」を解消するために「ページ切り替え」「ページインジケータ」も用意したのでしょうか。

物理ボタン数を超える機能を呼び出すための「プロファイルを切り替え」アクションであったり、「フォルダ」アクションであったり、「ページ切り替え」アクションであったり。同じ働きを行う機能を、見せ方を変えて複数実装してあるというのは、それだけ「問題点」だと(販売・営業的に、)みなされているためでしょうか。

より多くのアクションをボタン上にホスティングするために、プロファイル切り替えアクションに、ページ切り替えアクション、ページ表示アクション、フォルダ移動アクションなど操作系のアクション配置ボタンが増え、実質的にSD上に配置できるアクションが減ってしまう(6ボタンの最小のSDの場合には切り替え用で半分埋まる?)わけで、購入時によりボタン数の多いSDモデルへの誘導を行いたいのか、実ボタン以上のアクションをホスティングできることを訴求したいのか……それは自分にはわかりませんが、力を入れている場所であることは確かです。

最終的にはフェイスプレート部分に透明窓を追加して、プロファイル切り替えやページ切り替え、フォルダ切り替えなどの追加表示をSDの画面上で行うといったあたりに「落とし所」がありそうです。

Posted in news | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy Stream Deck | Leave a comment

Pages末尾から指定ページまでページ削除

Posted on 10月 12, 2021 by Takaaki Naganoya

「ページ単位でページを削除する」という機能を持たないPagesに対して、限定的ながらもページ単位の削除を行うAppleScriptです。Pages v11.2+macOS 12beta9で動作確認していますが、macOS 11.xやmacOS 10.15でも動作すると思います。

Pagesでまとまった内容の書類(例:書籍)を作る場合には、1書類にすべての内容を詰め込むのは自殺行為で、なるべく記事単位に分割するのがセオリーです。さすがに1ページあたり1書類といった作り方はしませんが、まとまった意味のある内容で1つの書類(扉ページと記事ページは別書類)ぐらいの分け方はします。

それというのも、Pagesがページ単位での入れ替えとかページ単位での削除、ページ単位での挿入といった操作系の機能が弱いからです。ページ単位での削除、入れ替えといった操作ができません。おまえはPagesなんて名乗らず「Overflow」とか名乗っとけ! という気持ちでいっぱいです。Page関連操作の弱いPages.app。

Pagesのヘルプで確認しても、ページ単位での削除については、「ページ上のオブジェクトを削除したあと、地に敷いている文字を削除して回れ」という、脳みそに何か湧いているような説明しか見当たりません。

ウィンドウ左に表示されているページのサムネイルを選択して、コンテクストメニューから「削除」を選ぶと書類の内容がまるごと削除されるという「わけのわからない」挙動をします。セクションを小分けしてある場合には、当該ページを含むセクションを削除してくれるようですが、1セクション=1ページという作り方をしていない場合には複数ページが削除されてしまいます。

–> Watch Page Removal Demo on Pages Movie

そこで、仕方なく本Scriptを作成してみたわけです。本Scriptの前バージョンとして「最終ページ上のオブジェクトの全削除」Scriptを作成して、実際にこれでページ削除が行える/行えないパターンについて検証を重ねてきました。実用性もある程度あったので、実際に作業で使ってもいました。

条件1:ページ削除は末尾側から行う。先頭側からは行えない
条件2:書類作成時に、ページの「地」に文字を置かず、テキストボックスを配置して文字を流し込んでいる

–> Watch Page Removal AppleScript Demo Movie

本Scriptでは、ページの地に直接「テキスト」や「表」を流し込んでいるような作り方の書類だと、書類上の内容をまるごと削除してしまいます。そういう場合には、Pages上の「表」などの元データが存在しているはずなので、そちらを再利用して別のPagesレイアウトに流し込み直すような対応になることでしょう。

なお、Pagesは(KeynoteやNumbersも)書類に変更を加えるとリアルタイムにファイルに変更が反映されるため、本Scriptのような道具でページ単位の削除をおこなった場合、「ファイル保存していないから、ファイルの再オープンを行えば復帰」ということはできません。あらかじめ、元の書類のコピーを作っておいた上で実行するようにしてください。

AppleScript名:末尾から指定ページまでページ削除.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/10/12
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

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

set dMax to 3 –このページまで残す。ここから後はすべて削除する

tell application "Pages"
  set dCount to count every document
  
if dCount < 1 then
    display notification "Pagesでオープンしている書類はありません"
    
return
  end if
end tell

set pMax to getPageNum() of me
if pMax < (dMax + 1) then
  display notification "Pagesでオープンしている書類は、指定よりも少ないページ数であるため、削除の必要がありません"
  
return
end if

–セクションごとのページ数のリストを取得
set aList to retSectionPageList() of me
set bList to sectionPageListToSectionHeaderList(aList) of me

repeat with i from pMax to (dMax + 1) by -1
  tell application "Pages"
    tell front document
      –オブジェクト削除前のページ数
      
set pMax1 to getPageNum() of me
      
      
tell last page
        delete every iWork item
      end tell
      
      
–セクション先頭の場合
      
set curPStat to contents of item i of bList
      
if curPStat = true then
        tell last section
          set body text to ""
        end tell
      end if
      
      
–オブジェクト削除後のページ数
      
set pMax2 to getPageNum() of me
      
      
–オブジェクト削除処理を行ってもページ数に変動がない場合には、last section上のbody textも消してみる
      
if pMax2 = pMax1 then
        tell last section
          set body text to ""
        end tell
      end if
    end tell
  end tell
end repeat

–セクションごとのページ数のリストを、ページ単位のセクション開始位置リストに変換する
on sectionPageListToSectionHeaderList(aList)
  set bList to {}
  
  
repeat with tmpP in aList
    
    
if tmpP = 1 then
      set the end of bList to true
    else
      set the end of bList to true
      
repeat (tmpP – 1) times
        set the end of bList to false
      end repeat
    end if
  end repeat
  
  
return bList
end sectionPageListToSectionHeaderList

–最前面の書類の各セクションごとのページ数を1Dリスト(1次元配列)で返す
on retSectionPageList()
  tell application "Pages"
    tell front document
      set sList to every section
      
set pList to {}
      
repeat with i in sList
        set the end of pList to count every page of i
      end repeat
    end tell
  end tell
  
return pList
end retSectionPageList

–最前面の書類のページ数を取得
on getPageNum()
  tell application "Pages"
    tell front document
      return count every page
    end tell
  end tell
end getPageNum

★Click Here to Open This Script 

Posted in Object control | Tagged 10.15savvy 11.0savvy 12.0savvy Pages | Leave a comment

Pages書類の最終ページ上のすべてのオブジェクトを削除

Posted on 10月 11, 2021 by Takaaki Naganoya

最前面のPages書類のうちの最終ページ上のオブジェクトとテキストを削除するAppleScriptです。

Pagesで凝ったレイアウトの書類を作る場合、やりかたはいくつかありますが……ページはすべてセクション(改ページ処理みたいなもの)を入れて1ページ=1セクションみたいにページを作って、その上にテキストフレームを配置、ページ間のテキストフレームの連続はGUI上で指定しておき、テキストが先頭から末尾まで、指定ボックス上を流れていくように指定しています。

別途、脚注のテキストは別のテキストボックスを配置しておき、こちらも前ページの脚注と連結する必要があればつないでおきます。

そのうえで、このように作成したPages書類を前後で分割しようとした場合に、

(1)書類をコピー
(2)前半の書類は、後半部分を削除
(3)後半部分の書類は、前半部分を削除

となるわけです。

が、Pagesに「指定ページを削除」する機能はないので、このようなScriptを組んでいろいろ対処を行っています。

本Scriptは、(2)の処理を行うためのものです。最終ページ上のオブジェクトおよびテキストを削除することで、ページ自体を削除できます。

あとは、1ページ=1セクションで書類が構成されているかどうかをチェックすれば、ページ削除とほぼ同義で扱えるところです。

AppleScript名:末尾のページのオブジェクトを全削除.scpt
tell application "Pages"
  tell front document
    tell last page
      delete every iWork item
    end tell
    
    
tell last section
      set body text to ""
    end tell
    
    
  end tell
end tell

★Click Here to Open This Script 

Posted in Object control | Tagged 10.15savvy 11.0savvy 12.0savvy Pages | Leave a 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

Pagesでページ数とfacing pagesフラグの値から右側ページを判定

Posted on 9月 26, 2021 by Takaaki Naganoya

Pagesでページ数とfacing pagesの値から該当のページが右側に存在するかどうかを判定するAppleScriptです。

ページが右側にあるかどうか、というこの判定処理はツメ処理のために用意したものです。見開きの右側に置くのが基本といいますか、別に左側にもあってもいいんですが、右側ページ右端に置いた縦長の表オブジェクトをツメと見立てて処理することに(個人的に)したためです。


▲同一のPages書類を「見開きページ」の設定をオフにした状態(左)、オンにした状態(右)でページの並びが変わる

AppleScript名:Pages書類のfacing Pagesを取得.scpt

set fRes to getFacingPages() of me

on getFacingPages()
  tell application "Pages"
    tell front document
      return facing pages
    end tell
  end tell
end getFacingPages

★Click Here to Open This Script 

こんな記述でfacing pagesの値を取得でき、この値とページ数(ノンブル)をもとに奇数ページ、偶数ページの判定を行い、ページの左右を判定できます。

本Scriptを実行すると、以下のようにログ表示を行います。{facing pages, ノンブル, 右側ページかどうかの判定結果}を表示しています。

(*false, 1, false*)
(*false, 2, true*)
(*false, 3, false*)
(*false, 4, true*)
(*false, 5, false*)
(*false, 6, true*)
(*true, 1, true*)
(*true, 2, false*)
(*true, 3, true*)
(*true, 4, false*)
(*true, 5, true*)
(*true, 6, false*)

以前にどこかで「奇数/偶数の判定処理なんて、素朴な処理をサブルーチンとして独立して用意しておくのか?」と聞かれたことがありましたが、このように、サブルーチンとして用意しておいたので簡単に使い回して利用できているわけです。

AppleScript名:Pagesのページ番号の該当ページが右側にあるかどうか計算.scpt

set flagList to {false, true}
repeat with i in flagList
  set aFlag to contents of i
  
repeat with aPage from 1 to 6
    set rRes to checkPagesRightPage(aPage, aFlag) of me
    
log {i, aPage, rRes}
  end repeat
end repeat

–Pagesのページ番号の該当ページが右側にあるかどうか計算
on checkPagesRightPage(aNum, aFlag)
  set oddF to chkOddNum(aNum) of me –奇数チェック
  
if {aFlag, oddF} = {true, true} then
    return true –右側ページ
  else if {aFlag, oddF} = {false, false} then
    return true –右側ページ
  else
    return false
  end if
end checkPagesRightPage

–奇数かどうかチェック
on chkOddNum(aNum)
  set a to aNum mod 2
  
if a = 1 then
    return true
  else
    return false
  end if
end chkOddNum

–偶数かどうかチェック
on chkEvenNum(aNum)
  set a to aNum mod 2
  
if a = 0 then
    return true
  else
    return false
  end if
end chkEvenNum

★Click Here to Open This Script 

Posted in boolean Number | Tagged 10.15savvy 11.0savvy 12.0savvy Pages | Leave a comment

Post navigation

  • Older posts
  • Newer posts

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

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 13.6.5 AS系のバグ、一切直らず
  • CotEditorで2つの書類の行単位での差分検出
  • Apple純正マウス、キーボードのバッテリー残量取得
  • macOS 15, Sequoia
  • 初心者がつまづきやすい「log」コマンド
  • ディスプレイをスリープ状態にして処理続行
  • 指定のWordファイルをPDFに書き出す
  • Adobe AcrobatをAppleScriptから操作してPDF圧縮
  • メキシカンハットの描画
  • 与えられた文字列の1D Listのすべての順列組み合わせパターン文字列を返す v3(ベンチマーク用)
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • 2023年に書いた価値あるAppleScript
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AdobeがInDesign v19.4からPOSIX pathを採用
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • Cocoa Scripting Course 続刊計画
  • NaturalLanguage.frameworkでNLEmbeddingの処理が可能な言語をチェック
  • macOS 14.xでScript Menuの実行速度が大幅に下がるバグ
  • スクリプトエディタで記入した「説明」欄の内容が消えるバグ

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1392) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (206) 13.0savvy (163) 14.0savvy (113) 15.0savvy (90) CotEditor (64) Finder (51) iTunes (19) Keynote (115) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (19) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (72) Pages (53) Safari (44) Script Editor (26) 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)
  • 未分類

アーカイブ

  • 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