フリーのPDFビューワー「Skim」の最新版v1.6.8で、前バージョンで発生していたAppleScriptのサポート機能のバグが修正されました。どちらかといえば、OS側の不具合のような雰囲気も漂っています。
SafariでブックマークされたURL一覧を取得
Safariのブックマークに登録されたURL一覧を取得するAppleScriptです。
SafariのAppleScript対応機能に、ブックマーク操作系のものは存在していません。そのため、Bookmarks.plistを直接読み込んでデータの絞り込みを行なっています。
Bookmarks.plistを実際に読んでみると、ツリー(フォルダ)とリーフ(個別のブックマーク)があって、本Scriptではリーフ部分しか読んでいないのですが、とりあえず試作レベルということで。
読むのはいいんですが、追加が大変そうなので(BookmarkのIDとか外部で勝手に追加できるものなんだろうか。UUIDっぽいけど)ブックマーク登録は野蛮にGUI Scripting経由でメニューを操作することになるのでしょう。
AppleScript名:SafariのBookmarks.plistから登録URLを取得 |
— – Created by: Takaaki Naganoya – Created on: 2022/01/07 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.7" use framework "Foundation" use scripting additions script spdB property urlList : {} end script set (urlList of spdB) to getBookmarkedURLs() of me set (urlList of spdB) to cleanUp1DList((urlList of spdB), missing value) of me –Sweep missing value items –Read Bookmarked URLs on getBookmarkedURLs() set newPath to "~/Library/Safari/Bookmarks.plist" set aRec to my readPlistAt:newPath return ((aRec’s Children)’s valueForKey:"URLString") as list end getBookmarkedURLs –Read Plist on readPlistAt:thePath set thePath to current application’s NSString’s stringWithString:thePath set thePath to thePath’s stringByExpandingTildeInPath() set theDict to current application’s NSDictionary’s dictionaryWithContentsOfFile:thePath return theDict end readPlistAt: –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 end if end repeat return bList end cleanUp1DList |
Intel MacとApple Silicon Macの速度差〜画像処理
macOS 11から12に移行して、なぜかリリースが近づくにつれて細かいバグや巨大なバグが顕在化し、まだ手放しでおすすめできる状況にないのが心苦しいところですが、AppleScriptからのCocoa呼び出しについてはIntel Mac/Apple Silicon Macでも速度が向上。とくに、Apple Silicon Macでの速度向上が顕著です。
で、特定の処理(巨大なlistやrecord in list同士の検索)でM1 Mac miniがiMac Proの2.5倍ぐらい速いといったベンチマークは出しているわけですが、いかにもApple Silicon Macで処理が速そうな画像処理のベンチマークを実施してみました。
Intel Mac mini 2014が手元からなくなってしまったので、以前のメインマシンであったMacBook Pro Retina 2012(MacBookPro10,1)と比較してみました。
ベンチマーク内容は、指定の画像が空白かどうかをチェックするというものです。フルHD画像でも4K画像でも8K画像でも、1ピクセルでも白くない点があれば検出できるという処理内容。外部のGPUImage FrameworkやPhotoshopのヒストグラム処理を呼び出すよりもAppleScriptだけで処理した方が速いというものです(8K画像だとPhotoshopの方が速いかも)。
▲画像解像度の変化と処理時間の相関。グラフは数値が小さいほど高速。巨大な画像になると処理速度差が大きくなる傾向にある?
だいたい、MacBook Pro Retina 2012と比べて3〜4倍ぐらいM1 Mac miniの方が高速です。MacBook Pro Retina 2012は2017年のMacBook Pro 13インチといい勝負ぐらいの速度が出ており、古い割にはごく最近まで使えていました。
そこから3〜4倍高速ということで、M1 Mac miniはコストの割にはパワフルです。ファンレスのMacBook Air M1でも同程度の速度が出るはずです。
一方で、M1 Pro/M1 Max搭載のMacBook ProでM1機よりも大幅に高速なのかと言われると……このぐらいの静止画の処理程度だとごくわずかな差しかつかないはずです。下手をすると、速度差がないかもしれません。
→ Download blank_image_detection_benchmark.zip (including script libraries)
▲バーが短いほど高速。M1がM1 Maxの2倍高速という結果が出てしまった。M1 Max MBPは2014年のIntel Mac miniより少し速いだけのマシンという結果に。2018年のIntel Mac miniとの比較だとM1 Max MBPよりもIntel Mac miniのほうが速そう
M1、M1 Max、M1 Ultra(最上位機種)で処理時間を比較した結果。予想どおりM1がM1 MaxやM1 Ultraよりも2倍以上高速。もはや8K画像ぐらいは、大きなデータにならないというべきなのか。
AppleScript名:画像の空白判定 v4_bench_時間計測.scptd |
— – Created by: Takaaki Naganoya – Created on: 2022/01/01 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.7" use framework "Foundation" use framework "AppKit" use scripting additions use mdLib : script "Metadata Lib" version "2.0.0" use easyTable : script "display table by list" set aFol to choose folder set aResList to perform search in folders {aFol} predicate string "kMDItemContentTypeTree CONTAINS %@" search arguments {"public.image"} set aList to {} repeat with i in aResList set a1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() set iRes to checkImageIsWhite(i) of blankImageKit of me set b1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() set c1Dat to b1Dat – a1Dat set aName to (current application’s NSString’s stringWithString:i)’s lastPathComponent() as string if iRes = true then set jRes to "White" else set jRes to "Black" end if –log {aName, jRes} set the end of aList to {aName, jRes, c1Dat} end repeat set fLabels to {"File name", "Result", "Estimate(Seconds)"} set aRes to (display table by list aList main message "Blank Image check" size {800, 600} labels fLabels) return aList script blankImageKit use AppleScript version "2.7" — High Sierra (10.13) or later use framework "Foundation" use framework "AppKit" use scripting additions property parent : AppleScript property NSData : a reference to current application’s NSData property |NSURL| : a reference to current application’s |NSURL| property NSColor : a reference to current application’s NSColor property NSImage : a reference to current application’s NSImage property NSBezierPath : a reference to current application’s NSBezierPath property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep –Compare Original Data and on checkImageIsWhite(aFile) set aPOSIXpath to POSIX path of aFile set aURL to |NSURL|’s fileURLWithPath:(aPOSIXpath) set aNSImage to NSImage’s alloc()’s initWithContentsOfURL:(aURL) set bNSImage to NSImage’s alloc()’s initWithContentsOfURL:(aURL) set fillColor1 to NSColor’s clearColor() set blankNSImage1 to drawImageWithFilledColor(aNSImage, fillColor1) of me set fillColor2 to makeNSColorFromRGBAval(65535, 65535, 65535, 65535, 65535) of me –white set blankNSImage2 to drawImageWithFilledColor(bNSImage, fillColor2) of me set aTiff to blankNSImage1’s TIFFRepresentation() set bTiff to blankNSImage2’s TIFFRepresentation() set chkWhite to (aTiff’s isEqualToData:bTiff) as boolean return chkWhite end checkImageIsWhite on getSizeOfImage(anNSImage) set aSize to anNSImage’s |size|() set aClass to class of aSize if aClass = record then copy aSize to theSize –To macOS 10.12.x else –macOS 10.13 or later set sizeX to (item 1 of item 2 of aSize) set sizeY to (item 2 of item 2 of aSize) set theSize to {width:sizeX, height:sizeY} end if return theSize end getSizeOfImage –指定サイズの画像を作成し、背景を指定色で塗る on drawImageWithFilledColor(anImage, fillColor) set aSize to getSizeOfImage(anImage) of me anImage’s lockFocus() set theRect to {{x:0, y:0}, {width:(width of aSize), height:(height of aSize)}} set theNSBezierPath to NSBezierPath’s bezierPath theNSBezierPath’s appendBezierPathWithRect:theRect fillColor’s |set|() theNSBezierPath’s fill() anImage’s unlockFocus() return anImage end drawImageWithFilledColor –aMaxValを最大値とする数値でNSColorを作成して返す on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer) set aRedCocoa to (redValue / aMaxVal) as real set aGreenCocoa to (greenValue / aMaxVal) as real set aBlueCocoa to (blueValue / aMaxVal) as real set aAlphaCocoa to (alphaValue / aMaxVal) as real set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBAval end script |
CotEditorで選択範囲の行頭にある数字をリナンバーする v1
CotEditorでオープン中の最前面の書類の選択範囲のテキストを行ごとにチェックし、行頭に存在する数字を、それらのうちの最小値を検出しつつ、指定のステップ数でリナンバー(番号振り直し)を行うAppleScriptです。
さまざまな項目の整理のために、テキストの先頭に仮想的なノンブル(ページ番号的なもの、ソート順を指定するための番号)を振っています。この番号ではじまるテキストをもとにFinder上でフォルダ整理をしており、項目の前後関係を入れ替えると…前後関係を明示するために、番号を振り直す必要が出てくるわけです。
# Numbers上やExcel上で行うと、余計な書体スタイルなどが入ってきて邪魔なので、テキストエディタ上で行うことが多いです
その番号の振り直しを行うAppleScriptです。macOS 12.2beta+CotEditor v4.0.9で動作確認を行っています。
▲選択範囲内の行頭の番号を振り直す。ちなみに、表示例はボツになった本の企画
本来は「行頭にある数字」を指示する必要があるものの、まだうまく機能していません。桁数でなんとなく判別しているだけです。
▲フォント作者の方々の合意を得られなさそうで流れた企画「同人フォントソムリエ」
AppleScript名:選択範囲の行頭にある数字をリナンバーする v1.scptd |
— – Created by: Takaaki Naganoya – Created on: 2022/01/02 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions property NSMutableArray : a reference to current application’s NSMutableArray property myNumStep : 1000 tell application "CotEditor" tell front document set aSel to contents of selection set aSelList to paragraphs of aSel end tell end tell –範囲指定がない場合 if length of aSelList = 0 then display dialog "Error: No Selection" buttons {"OK"} default button 1 with icon 2 return end if –過大な範囲指定チェック if length of aSelList > 1000 then set bRes to button returned of (display dialog "選択範囲が1000行を超えています。処理に時間がかかることが予想されますが、実行しますか?") end if –行頭の数字部分のみ取得して、数字の最小値を取得する set topNumList to getNumbersAtLineBegennings(aSelList) of me set minNum to calcIntMinAsStr(topNumList) of me –行頭部分の数字部分のみリナンバー set bRes to renumberNumsAtLineBegennings(aSelList, minNum as integer, myNumStep) of me –1D Arrayを改行コードをデリミタに指定しつつテキスト化 set outStr to retDelimedText(bRes, return) of me tell application "CotEditor" tell front document set contents of selection to outStr end tell end tell –行頭に入っている数字の文字のみリナンバー –実際には行頭判定はまだ行えていない。行頭の番号と文の途中に出てくる数字の区別は「桁数」でのみ行っている on renumberNumsAtLineBegennings(aSelList as list, firstNum as string, stepNum as number) set aDigit to length of firstNum –桁数 copy firstNum to aCount set aRes to {} repeat with i in aSelList set j to (contents of i) as string set nRes to (getNumberCharsOnlyAtBegening(j) of me) as string if nRes is not equal to "" then –InputとOutputが違った set n1Res to removeNumCharsOnly(j, aDigit) of me set tmpNumStr to zeroPadding(aCount, aDigit) of me set n1Res to tmpNumStr & n1Res set aCount to aCount + stepNum set the end of aRes to n1Res else set the end of aRes to j end if end repeat return aRes end renumberNumsAtLineBegennings –1D Listの最小値を文字列で返す on calcIntMinAsStr(aList as list) set nArray to (NSMutableArray’s arrayWithArray:aList) set maxRes to (nArray’s valueForKeyPath:"@min.self")’s intValue() return maxRes as string end calcIntMinAsStr –行頭に入っている数字の文字のみ抽出 on getNumbersAtLineBegennings(aSelList as list) set aRes to {} repeat with i in aSelList set j to contents of i set nRes to getNumberCharsOnlyAtBegening(j) of me if nRes is not equal to "" then set the end of aRes to nRes end if end repeat return aRes end getNumbersAtLineBegennings –数字のみ返す on getNumberCharsOnlyAtBegening(aStr as string) set anNSString to current application’s NSString’s stringWithString:aStr set anNSString to anNSString’s stringByReplacingOccurrencesOfString:"[^0-9]{4,}" withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, anNSString’s |length|()} return anNSString as text end getNumberCharsOnlyAtBegening –数字のみ削除して返す on removeNumCharsOnly(aStr as string, aDigit) set anNSString to current application’s NSString’s stringWithString:aStr set anNSString to anNSString’s stringByReplacingOccurrencesOfString:("[0-9]{" & (aDigit as string) & ",}") withString:"" options:(current application’s NSRegularExpressionSearch) range:{0, anNSString’s |length|()} return anNSString as text end removeNumCharsOnly –指定桁数で指定の数にゼロパディングして文字列を返す on zeroPadding(aNum as number, aDigit as number) set aText to "00000000000" & (aNum as text) set aLen to length of aText set aRes to text (aLen – aDigit + 1) thru -1 of aText return aRes end zeroPadding –1D Listを指定デリミタをはさみつつテキストに on retDelimedText(aList as list, aDelim as string) 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 |
NSAttributedStringにtab指定つきparagraph styleを指定
書式付きテキスト(NSAttributedString)にパラグラフスタイルを指定して、tab指定を行い、tab展開を行うテストです。
Keynote/Pagesなどのテキストアイテムで、中にタブを含むスタイル付きテキストだった場合に、スタイル付きテキストの幅を計算したら大きな誤差が生じていたため、タブを展開して近似するサイズを取得できないかと考え、試してみたものです。
いまひとつ、まだしっくり来ていません。
AppleScript名:Attributed Stringにparagraph styleを指定してtab指定.scptd |
— – Created by: Takaaki Naganoya – Created on: 2021/12/27 — – Copyright © 2021 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use scripting additions set aStr to "Piyomaru Software 100 200 10 20" set anAssrStr to makeRTFfromParameters(aStr, 3.0, 16, "HelveticaNeue") of me set aTabStops to {90, 400, 500, 600, 700} set aStyle to makeParagraphStyle(aTabStops) of me anAssrStr’s addAttribute:(current application’s NSParagraphStyleAttributeName) value:aStyle range:(current application’s NSMakeRange(0, (anAssrStr’s |length|()))) return anAssrStr –Make Paragraph Style on makeParagraphStyle(tList as list) set tabList to {} repeat with i in tList set the end of tabList to (current application’s NSTextTab’s alloc()’s initWithTextAlignment:(current application’s NSTextAlignmentLeft) location:i options:(missing value)) end repeat set aParagraphStyle to (current application’s NSParagraphStyle’s defaultParagraphStyle())’s mutableCopy() aParagraphStyle’s setTabStops:tabList return aParagraphStyle end makeParagraphStyle –Make Attributed String on makeRTFfromParameters(aStr as string, outlineNum as real, aFontSize as real, aFontName as string) set aVal1 to current application’s NSFont’s fontWithName:aFontName |size|:aFontSize set aKey1 to (current application’s NSFontAttributeName) set aVal2 to current application’s NSColor’s redColor() set aKey2 to (current application’s NSForegroundColorAttributeName) set aVal3 to 1.0 set akey3 to (current application’s NSKernAttributeName) set aVal4 to 0 set akey4 to (current application’s NSUnderlineStyleAttributeName) set keyList to {aKey1, aKey2, akey3, akey4} set valList to {aVal1, aVal2, aVal3, aVal4} set attrsDictionary to current application’s NSMutableDictionary’s dictionaryWithObjects:valList forKeys:keyList set attrStr to current application’s NSMutableAttributedString’s alloc()’s initWithString:aStr attributes:attrsDictionary return attrStr end makeRTFfromParameters |
SkimのAppleScriptサポート機能にバグ
フリーのPDFビューワー「Skim」の最新版v1.6.7で、AppleScriptのサポート機能にバグが発生していることが明らかになりました。
スクリプトエディタでtell app “Skim”….end tellと入力して構文確認しただけで「内部の表があふれました」というワーニングが出てしまいます。
Script Debuggerでも同様です。
Script MenuからAppleScriptを呼び出して実行する分にはエラーが出ないので、今日もSkim経由でオープン中のJPEG画像を書き出したりしていたのですが、、、、
12/15の時点で、SkimのForum上でこの問題について話題になり、対策ビルドなども出ているようなので、年明けほどなくして解決されることを期待しています。
AppleのKeynote/Pages/Numbersだとあからさまな不具合であっても修正に半年ぐらいかかるので、このあたりはオープンソースのソフトウェアのほうが安心感があります。
FileMaker PowerPack活用ガイドを発売!
既存のAppleScript対応アプリケーションから、Cocoa Scriptingの現在のパワーを利用できるようにした「PowerPack」シリーズ。
第1弾は日本国内外で多くのユーザーに支持されている「CotEditor」用PowerPack。
第2弾がさまざまなプラットフォームへの広がりを見せつつmacOSのコア機能であるAppleScriptへの対応も行っているデータベース製品「FileMaker Pro」用のPowerPack。「FileMaker PowerPack」は、このFileMaker Proの上で動くCocoa AppleScriptサンプル集です。
サンプル集としてAppleScriptが掲載されているだけなく、FileMaker Pro上でそのまま実行でき、サンプルデータをもとに実行して、その結果をFileMaker Pro上に返してきます。
一般的なAppleScriptランタイム環境と異なるFileMaker Pro上のランタイム環境においても動作するようFileMaker Pro向けのチューニングを行ってあります。
「FileMaker PowerPack」は、日本国内のFileMaker系最難関コンテスト「FM-1グランプリ」への応募作品でもあり、業務効率化部門準賞 入賞作品でもあります。
日頃、AppleScriptになじみのない開発者にもこのPowerPackを試し、実際に実行し、そして書き換えて自分のシステムで使うための使い方を詳細に解説する電子書籍「FileMaker PowerPack」活用ガイドを刊行いたしました。
PDF 317ページ、FileMaker PowerPackの最新版 v2.1を同梱。
さらに、期間限定おまけコンテンツ「FM-1 GP戦記」(PDF 43ページ)を添付。前代未聞の難関コンテストを勝ち残るためにチーム内で行われた詳細な議論。試作品を没にしてたどりついた、コンテスト攻略の秘策とは?! いまだからこそ明かされる、真剣かつ呑気なやりとりに全米が泣いた、総ページ数350ページを超える1作です。
Pagesのテキストアイテム内の文字の実際の幅でリサイズ
Pagesでオープン中の最前面の書類の上に載っている(Pagesの仕様でページ指定はできない)テキストアイテムから、テキスト、フォント、フォントサイズを取得し、その内容でスタイル付きテキストを作成して画面上に描画するサイズを取得し、その幅でテキストアイテムをリサイズするAppleScriptです。
macOS 12.2beta+Pages 11.2で動作確認しています。
Pages上のタブの指定までスタイル付きテキスト内で再現できていないためか、中身の幅よりも外側のテキストアイテム(枠)が小さくなってしまうケースが見られますが、もう少し枠のリサイズを余裕を持って(大きめに)指定すると回避できます。
AppleScript名:Pagesのテキストアイテム内の文字の実際の幅でリサイズ |
— – Created by: Takaaki Naganoya – Created on: 2021/12/25 — – Copyright © 2021 Piyomaru Software, All Rights Reserved — use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property NSFont : a reference to current application’s NSFont property NSString : a reference to current application’s NSString property NSDictionary : a reference to current application’s NSDictionary property NSFontAttributeName : a reference to current application’s NSFontAttributeName tell application "Pages" tell front document set tList to every text item if tList = {} then return repeat with i in tList set j to contents of i tell j set tmpFontName to font of its object text set tmpFontSize to size of its object text set tmpText to (its object text as string) & " " set aSize to getSizeFromAttributedText(tmpText, tmpFontName, tmpFontSize) of me set aWidth to |width| of aSize set bWidth to aWidth + 10 set its width to bWidth end tell end repeat end tell end tell –指定のテキスト、フォント名、フォントサイズでAttributed Stringを組み立ててサイズを取得する on getSizeFromAttributedText(aText as string, aFont as string, aFSize as real) set aFont to NSFont’s fontWithName:aFont |size|:aFSize set aStr to NSString’s stringWithString:aText set aSize to aStr’s sizeWithAttributes:(NSDictionary’s dictionaryWithObjects:{aFont} forKeys:{NSFontAttributeName}) return aSize end getSizeFromAttributedText |
Pagesで選択中の表のカラム幅を自動調整
Pagesの最前面の書類で選択中の表オブジェクトの2列目以降を均等に自動調整するAppleScriptです。
1列目の列幅は変更しません。表全体を選択するのではなく、どこかのセルを選択しておく必要があります。
▲処理前 カラム幅が不均一。表の1つあるいは複数のセルを選択して処理対象を指定
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 |
Xcode 13.2 〜 13.3は有害(正式版でもまだ有害)
Xcode 13.2がリリースされ、賛否両論いろいろ意見が出されていますが、自分にとっては有害であり、Betaの段階でバグレポートを出していましたが、修正されずにリリースされました。
Interface Builder上でApp Delegateを選択してもScriptコード内のイベントハンドラが認識されないため、インタフェースのアクションとコードのひもづけが行えません。
▲左:Xcode 9.xで作ったAppleScriptのプロジェクトをXcode 13.2でオープンしたもの、右:Xcode 13.2上でゼロから作ったAppleScriptのプロジェクト XIB上でAppDelegateのイベントハンドラを追加できず、GUI部品の操作に対応するハンドラのひもづけができない
もはや「またか?」というぐらい何度も発生しているバグであり、責任者に知性が欠如しているのではという疑いを持たずにはいられません。
# Xcode 13.3が正式リリースされましたが、相変わらず使えません
Keynoteの表の選択中のセルに入っている丸つき数字をインクリメント/デクリメント
Keynoteの最前面の書類の現在のスライド上にある表の選択中のセルに入っている丸つき数字(①②③…… )をインクリメント(+1)、デクリメント(ー1)するAppleScriptです。
書籍などに掲載する資料で、参照番号をいじくるツールがどうしても必要になって作ったものです。
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 |
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 |
漢字をパーツに分解して部品が共通する文字を検索
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 |
構成要素を指定して漢字検索 v4
漢字の構成要素を指定して漢字を検索する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 |
2021年に書いた価値あるAppleScript
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
Microsoftが新OutlookにAppleScript機能を追加表明
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系を使い続けてもよいのでしょう。
また、本件はあくまで計画段階の話なので、確度のほどは不明です。
アンダースコアが入っていたら削除して次の文字を大文字化
文字列の途中にアンダースコア(「_」)が入っていたら削除し、その次の文字列(たぶんアルファベット)を大文字化する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 |
選択中の表の指定行・列のマル付き数字リナンバー v3
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 |
Amazon EC2 M1 Macインスタンスが利用可能に
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を利用しなくても処理できそうな気もします(データ量次第です)。
FM-1グランプリで仕事効率化部門の部門準賞を獲得!
FileMaker Proの「すごいデータベース」のコンテストであるFM-1グランプリにおいて、Piyomaru Softwareの応募作品「FileMaker PowerPack」が、予選を通過し、決勝において仕事効率化部門の部門準賞を獲得いたしました。
FileMaker PowerPackは、CotEditor PowerPackと同様、Cocoa ScriptingのパワーをFileMaker Proユーザーに体感していただく技術的なデモンストレーションであり、かつ日常的にデータベース用のデータ作成などの作業に従事する人たちが便利と感じる(のではないか)という内容のScriptを収録したScriptサンプルデータベースに仕上がっています。
データベース上にサンプルデータを入れ、その場で実行して結果を確認できるようになっています。
内容はあっさり読めるように書いてあるものが多いので、入門用に見えてしまうかもしれませんが……実際にAppleScriptを「書ける」人間が読むと顔をひきつらせるような内容のものが多々入っています。作った本人としては「これを見て書けるようになるとは思わない」というレベル設定です。超絶レベルではないものの、アクロバティックなものが多いので。
技術的にも度肝を抜くような内容であり、178本のScriptを収録してはいるものの、本作品はあくまでFileMaker Pro上で動作するAppleScriptのコードなわけで、「ド派手で巨大で存在感はあるものの、さすがに大賞はとらないだろう」という味付け。本作品のレビュー記事は、文字数からBlog側で単純計算した読了所要時間が23分という大作です(通常の8倍のボリウム)。
それでも、FM-1グランプリの運営方針である「きちんと内容をチェックする詳細なレビュー」が行われおり、日本国内のFileMaker Proコミュニティのレベルを内外に示したという意味において、よそで真似できない「まっとうな」骨太のコンテストであったことを痛感。運営に携わられたお3方、関連スポンサー企業の方々に敬意を表します。
というわけで、このFileMaker PowerPackの書き換え方法や利用方法などを解説した「FileMaker PowerPack活用ガイド」を近日発刊予定です。技術的なレベルを下げて解説し、久しぶりにAppleScriptにさわるという方向けにレベル設定しています。完全理解というレベルでなくても、必要な書き換えはできるだろうという内容を目指しています。
そして、自分で作った作品の作者自身の解説のはずなのに、自分で書いていてとても大変です。「誰だ、こんなもの作ったのは?」という悪態をついてしまうほどですが、それは自分のことなので自業自得というところです。
選択中の表の指定行・列のマル付き数字リナンバー v2
Keynote書類の現在表示中のスライド(ページ)中の選択中の「表」において、指定の行、指定のヘッダー列以降のセルに対して、(1)丸つき数字の削除 (2)丸つき数字の最小値の計算 を行い、丸つき数字の番号降り直しの開始値をユーザーに確認したうえで、丸つき数字部分のみのリナンバーを行います。
本ScriptはmacOS 12+Keynote v11.2で作成・動作確認を行なってあります。それほどOSおよびKeynoteのバージョンに依存するような処理は行なっていないため、上記以外のOSおよびKeynoteのバージョンの組み合わせでも動くと思います。
初期状態では、
と、丸つき数字のナンバリングが項目の入れ替えなどで正しく並んでいない状態を想定しています。
リナンバー対象の表を選択した状態で本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 |