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

月: 2021年12月

NSAttributedStringにtab指定つきparagraph styleを指定

Posted on 12月 31, 2021 by Takaaki Naganoya

書式付きテキスト(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

★Click Here to Open This Script 

(Visited 63 times, 1 visits today)
Posted in RTF | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy NSMutableParagraphStyle NSTextTab | Leave a comment

SkimのAppleScriptサポート機能にバグ

Posted on 12月 28, 2021 by Takaaki Naganoya

フリーの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だとあからさまな不具合であっても修正に半年ぐらいかかるので、このあたりはオープンソースのソフトウェアのほうが安心感があります。

(Visited 83 times, 1 visits today)
Posted in Bug | Tagged 11.0savvy 12.0savvy Skim | 3 Comments

FileMaker PowerPack活用ガイドを発売!

Posted on 12月 28, 2021 by Takaaki Naganoya

既存の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作です。

(Visited 45 times, 1 visits today)
Posted in PRODUCTS | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy FileMaker Pro | Leave a comment

Pagesのテキストアイテム内の文字の実際の幅でリサイズ

Posted on 12月 25, 2021 by Takaaki Naganoya

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

★Click Here to Open This Script 

(Visited 55 times, 1 visits today)
Posted in Object control RTF | Tagged 10.15savvy 11.0savvy 12.0savvy Pages | 1 Comment

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 

(Visited 80 times, 1 visits today)
Posted in list | Tagged 10.15savvy 11.0savvy 12.0savvy Pages | Leave a comment

Xcode 13.2 〜 13.3は有害(正式版でもまだ有害)

Posted on 12月 17, 2021 by Takaaki Naganoya

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が正式リリースされましたが、相変わらず使えません

(Visited 324 times, 1 visits today)
Posted in Bug | Tagged 12.0savvy Xcode | 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 

(Visited 63 times, 1 visits today)
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 

(Visited 65 times, 1 visits today)
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 

(Visited 45 times, 1 visits today)
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

(Visited 326 times, 1 visits today)
Posted in news | Tagged 10.15savvy 11.0savvy 12.0savvy | 1 Comment

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系を使い続けてもよいのでしょう。

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

(Visited 197 times, 1 visits today)
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 

(Visited 49 times, 1 visits today)
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 

(Visited 45 times, 1 visits today)
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を利用しなくても処理できそうな気もします(データ量次第です)。

(Visited 138 times, 1 visits today)
Posted in news | Tagged 11.0savvy 12.0savvy AWS | Leave a comment

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

Google Search

Popular posts

  • AppleScriptによるWebブラウザ自動操縦ガイド
  • macOS 13, Ventura(継続更新)
  • ドラッグ&ドロップ機能の未来?
  • macOS 12.x上のAppleScriptのトラブルまとめ
  • PFiddlesoft UI Browserが製品終了に
  • macOS 12.3 beta 5、ASの障害が解消される(?)
  • SF Symbolsを名称で指定してPNG画像化
  • 新刊発売:AppleScriptによるWebブラウザ自動操縦ガイド
  • macOS 12.3 beta4、まだ直らないASまわりの障害
  • macOS 12.3上でFinder上で選択中のファイルをそのままオープンできない件
  • Safariで表示中のYouTubeムービーのサムネイル画像を取得
  • macOS 12のスクリプトエディタで、Context Menu機能にバグ
  • Pixelmator Pro v2.4.1で新機能追加+AppleScriptコマンド追加
  • 人類史上初、魔導書の観点から書かれたAppleScript入門書「7つの宝珠」シリーズ開始?!
  • CotEditor v4.1.2でAppleScript系の機能を追加
  • macOS 12.5(21G72)がリリースされた!
  • UI Browserがgithub上でソース公開され、オープンソースに
  • Pages v12に謎のバグ。書類上に11枚しか画像を配置できない→解決
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1391) 10.14savvy (586) 10.15savvy (434) 11.0savvy (274) 12.0savvy (174) 13.0savvy (34) CotEditor (60) Finder (47) iTunes (19) Keynote (97) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (21) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (42) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (118) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSUUID (18) NSView (33) NSWorkspace (20) Numbers (55) Pages (36) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • 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
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • 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)
  • 未分類

アーカイブ

  • 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