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

投稿者: Takaaki Naganoya

シンプルな文字置換

Posted on 9月 13 by Takaaki Naganoya

AppleScriptで文字置換を行う場合には、いくつかの方法があります。

①text item delimitersを利用する

AppleScriptの登場以来、利用されてきた方法です。AppleScriptの処理系では最速といってよい方法ですが、複数の箇所を一気に置換するのでやや過剰なのと処理方法が独特なので敬遠する人もいるようです。

また、処理対象の文字列のサイズが数Mバイト以上になると、処理が終了しなかったりします。巨大な文字列の置換には⑤が推奨されます。CotEditorのようなScriptableなテキストエディタの機能を利用するのもアリでしょう。

AppleScript名:文字置換(最短).scpt
set origText to "abcdefg"
set targStr to "de"
set repStr to "xx"

set a to repChar(origText, targStr, repStr) of me
–> "abcxxfg"

–Written By Philip Aker
–文字置換ルーチン
on repChar(origText as string, targStr as string, repStr as string)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChar

★Click Here to Open This Script 

②shellコマンドを利用する

do shell scriptコマンド経由で置換コマンドを利用する方法です。Mac OS X以降後に利用できるようになったものですが、日本語に対して正確に置換できるのか不安が残るので、個人的にはあまり積極的には利用していません。

③OSAXを利用する

macOS 10.15でサードパーティのOSAX(Scripting Additions)が廃止されたため、現在では利用できません。

④AppleScript Librariesを利用する

Shane StanleyのRegexAndStuffLibなどのライブラリを用いて正規表現を用いた文字置換を利用するものです。

⑤Cocoaの機能を利用する

Cocoaの機能を呼び出して文字置換を行うものです。いくつも利用できる機能があります。正規表現を使うものなど、お好きなものを利用してください。

AppleScript名:ASOCで文字置換5.scptd
— Created 2015-06-30 by Takaaki Naganoya
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set a to "あいうえお++かきくけこ"
set b to cleanUpText(a, "+", "●")
–> "あいうえお●●かきくけこ"

on cleanUpText(someText, targStr, repStr)
  set theString to current application’s NSString’s stringWithString:someText
  
set targString to current application’s NSString’s stringWithString:targStr
  
set repString to current application’s NSString’s stringWithString:repStr
  
  
set theString to theString’s stringByReplacingOccurrencesOfString:targString withString:repString options:(current application’s NSRegularExpressionSearch) range:{location:0, |length|:length of someText}
  
return theString as text
end cleanUpText

★Click Here to Open This Script 

これらに加えて、もっと簡単な方法がないかと探してみたら、

⑥offset ofで文字列検索して置換を行う

offset ofで置換対象の文字列を検索して置換を行います。オリジナル文字列に対して置換対象の文字列の位置が「冒頭ではない」「末尾ではない」という条件がそろっていて、かつ置換対象が0ないし1回しか対象文字列中に存在しないことが事前に分かりきっている場合にのみ使えます。

set urlData to "https://www.amazon.co.jp/gp/css/summary/print.html/ref=oh_aui_ajax_invoice"

set anOffset to offset of "/gp/" in urlData
set newURL to (text 1 thru (anOffset + (length of "/gp/") – 1) of urlData) & "legacy" & (text (anOffset + (length of "/gp/") – 1) thru -1 of urlData)

–> "https://www.amazon.co.jp/gp/legacy/css/summary/print.html/ref=oh_aui_ajax_invoice"

★Click Here to Open This Script 

このぐらいで使えますが、本当にすべてのパターンを利用するには、場合分けしつつ書く必要があります。シンプルなはずなのに、シンプルになっていないという。

AppleScript名:単純文字置換.scpt
set urlData1 to "https://www.amazon.co.jp/gp/css/summary/print.html/ref=oh_aui_ajax_invoice"
set newURL1 to replaceOne(urlData1, "/gp/", "/gp/legacy/") of me
–> "https://www.amazon.co.jp/gp/legacy/css/summary/print.html/ref=oh_aui_ajax_invoice"

set urlData2 to "https://www.amazon.co.jp/css/summary/print.html/ref=oh_aui_ajax_invoice/gp/"
set newURL2 to replaceOne(urlData2, "/gp/", "/gp/legacy/") of me
–> "https://www.amazon.co.jp/css/summary/print.html/ref=oh_aui_ajax_invoice/gp/legacy/"

set urlData3 to "https://www.amazon.co.jp/css/summary/print.html/ref=oh_aui_ajax_invoice/gp/"
set newURL3 to replaceOne(urlData3, "https://", "http://") of me
–> "http://www.amazon.co.jp/css/summary/print.html/ref=oh_aui_ajax_invoice/gp/"

on replaceOne(origStr as string, targStr as string, repStr as string)
  set anOffset to offset of targStr in origStr
  
if anOffset = 0 then return origStr
  
set {aLen, bLen} to {length of origStr, length of targStr}
  
  
if anOffset + bLen > aLen then
    –置換対象文字が末尾にある場合
    
set newStr1 to (text 1 thru (anOffset – 1) of origStr)
    
set newStr2 to ""
  else if anOffset = 1 then
    –置換対象文字が先頭にある場合
    
set newStr2 to (text (anOffset + ((length of targStr))) thru -1 of origStr)
    
set newStr1 to ""
  else
    –通常パターン
    
set newStr1 to (text 1 thru (anOffset – 1) of origStr)
    
set newStr2 to (text (anOffset + ((length of targStr))) thru -1 of origStr)
  end if
  
return newStr1 & repStr & newStr2
end replaceOne

★Click Here to Open This Script 

Posted in Text | Tagged 13.0savvy 14.0savvy 15.0savvy 26.0savvy | Leave a comment

macOS(Mac OS X/OS X)上のAppleScriptの歴史

Posted on 9月 9 by Takaaki Naganoya

macOS 26.0のリリース日が公開されそうなので、AppleScriptの歴史年表について書いておきます。

各種電子書籍に掲載している内容ですが、一般にも共有できていたほうがメリットが多そうなので公開しておきます。

マイナーバージョンで発生したバグの件については、すべて掲載できていません。ファイル書き込み時の文字エンコーディングが狂うとか、OSのバージョン番号をまともに返してこないとか。

Mac OS X 10.0(AppleScript v1.6)
◯Classic Mac OS向けに書かれたOSAXを認識しなくなった 
◯Unicode Textを扱えるようになった(as Unicode Text)
◯Mac OS Xのファイル/アプリケーションの新しい仕組み「バンドル」を扱えるようになった 
◯コマンドラインからosascript、osacompile、osadecompile、osalangが使えるようになった 
◯read/writeコマンドで2GB以上のデータの読み書きができるようになった

Mac OS X 10.1(AppleScript v1.7)
◯AppleScript自体の改行コードに、CR+LF(Windows)、CR(Mac)、LF(Unix)の3種類を使えるようになった(ネイティブはCR)
◯プログラムリンクの機能が復活。ただし、AppleTalkによるノード名称の指定はできず、TCP/IPベースに変更される(eppc://)
◯SOAPとXML-RPCの機能がサポートされた
◯拡張子を扱えるようになった
◯アプレット保存時にPowerPC/68kのどちらかのバイナリを選択するようになった

Mac OS X 10.2(AppleScript v1.9)
◯choose URLがWindows系サービスとFTPサーバーに対応
◯Script Menuの機能が単体でダウンロード提供されるようになった
(Folder ActionsがSystem Eventsの管轄にあるなど混乱期)

Mac OS X 10.3(AppleScript v1.9.2)
×AppleScript史上最大最悪のバグ「is in」演算子が使えないバグが発生(→10.3.x台で修正される)
◯AppleScriptおよびアプリケーションがバンドル形式でも保存できるようになった
◯delayに小数点以下の数値が指定できるようになった
(×AppleScriptの処理系のバグがとても多かった暗黒時代)

Mac OS X 10.4(AppleScript v1.10)
◯日本語環境がらみのバグが多数修正されたが、本格的な修正は10.5を待つ必要があった
×バグの指摘から修正まで時間がかかり、このバージョンを経てようやく処理系の安定が図られるようになった
×Finderからファイルパスを取得するとエンコーディングがおかしくなったり、display dialogで日本語がまともに表示できないアプリが存在していたのもこのバージョンまで

Mac OS X 10.5(AppleScript v2.0)
◯文字列をすべてUnicodeで扱うようになった
◯ASCIIキャラクター0〜255だけでなく、Unicodeの広い範囲の文字を取得できるように「text id xxxx」(xxxxは10進数で指定)が新設された
◯日本語の文字を含むファイルパスの取扱いがまともになってきた
◯アプリケーション起動中の状態(running)を取得できるようになった
◯アプリケーションをバンドルIDで指定できるようになった
◯FolderAction SetupがScriptableになった
◯「#」ではじまるコメント行を書けるようになった

OS X 10.6(AppleScript v2.1)
◯「スクリプトエディタ」が「AppleScriptエディタ」と名称変更
◯AppleScriptエディタが「ユーティリティー」フォルダに移動になった
◯AppleScriptエディタ上での構文色分け機能が強化された
◯AppleScript処理系の(ログイン後の)起動が速くなった
→ ただし、逆にログインアイテムに登録したAppleScriptアプレットが正しく実行されないというバグを生んだ。ログインアイテムに登録したAppleScriptアプレットでSystem Eventsを呼び出すと、System Eventsの起動が間に合わず、エラーになったりしていた

OS X 10.7(AppleScript v2.2)
◯AppleScriptエディタにスクリプトテンプレートの機能が追加された
◯AppleScriptエディタ上でAppleScriptObjC(Cocoa AppleScript Applet)を書けるようになった
×64bit版に書き直されたFinder、通称「Cocoa Finder」の動作速度が遅く、とくにファイル処理をFinderに任せると大幅に速度が低下する現象が発生。Scripterの間で「ファイル処理をFinderで行わないという」暗黙の合意が広がった

OS X 10.8(AppleScript v2.2)
◯AppleScriptエディタに「エクスポート」の機能がついた。アプレットはエクスポートして生成するよう変更
◯AppleScriptがOSのSandbox化の機能の影響を受けるようになった

OS X 10.9(AppleScript v2.3)
◯AppleScript 20年来の大改革。ライブラリ機能「AppleScript Libraries」が追加された
◯ユーザーへのNotification Center経由での通知命令「display notification」が追加された
◯AppleScriptObjCのハンドラ記述方式に、Objective-Cに似た記述方式が使えるようになった(Xcode上)
◯AppleScriptエディタでアプレットへのコードサインができるようになった
◯AppleScriptエディタがiCloudに対応し、iCloud上にAppleScriptを保存、iCloudからのAppleScriptの読み込みができるようになった

OS X 10.10(AppleScript v2.4)
◯「AppleScriptエディタ」が名称変更でふたたび「スクリプトエディタ」に
◯スクリプトエディタ上でCocoaの機能を利用するAppleScriptObjCを標準でサポートするようになった
◯プログレスバーの表示機能をサポート
◯「as」による型変換(cast)で複数の型をリストで指定できるようになった
◯AppleScriptObjCのハンドラ記述方式に、Objective-Cに似た記述方式が使えるようになった(スクリプトエディタ上)
◯ハンドラ(サブルーチン)宣言部でパラメータの型指定が行えるようになった
◯無意味句を用いたハンドラ記述した場合に、パラメータ省略時の初期値を宣言できるようになった

×AppleScriptアプレット中でdelayコマンドを利用したときに、正しく時間待ちされないバグ

×AppleScriptとCocoaの間で小数点を含む実数を変換すると小数点以下の値が欠落するバグ
×NSStringとtext、NSStringとstring、NSStringとUnicode textの間でデータ 変換をしたときに、一部の文字が正しく変換されないというバグ(castバグ)
×Folder Actionにバグ。利用者権限に抵触してまともに動作しない(OS X 10.11で修正)

OS X 10.11(AppleScript v2.5)
◯Folder Actionのバグ修正。FSEventsを用いて随時ドロップされたファイルのイベントを受け付けるように処理方式があらためられた(以前は数秒ごとに対象フォルダをチェック する方式)
◯10.10のAppleScriptObjCのバグ修正(cast、enum)
◯AppleScript Librariesのファイルの置き場所を追加(環境変数で指定可能に)
◯より多くのCocoaのデータ型をAppleScriptの型に変換するようになった
◯delayの時間待ち無効バグ修正

macOS 10.12(AppleScript v2.5)
◯パフォーマンス改善のためのバグ修正を実施
×ドロップレットに不具合。ファイルの拡張属性「com.apple.quarantine」がついていると、ファイルが無視される(Script側で対処。以後Apple修正せず)
×Scripting Bridgeの定義ファイルに不具合。Enum「NSNotFound」のブリッジされている定義値が-1にならない(macOS 10.13.1で修正)

macOS 10.13(AppleScript v2.7)
◯セキュリティ関連の修正を実施
◯NSCharacterSet newlineCharacterSet()のようなメソッドでクラッシュしなくなった
◯NSNotFoundのScriptingBridge経由でブリッジされる定義値を-1に修正(macOS 10.13.1)
◯Remote AppleEventsの機能が復旧し、AppleScriptからLAN上の他のマシンの上で動作しているアプリケーションを直接操作できるようになった(起動している必要がある)

◯ScriptingBridge定義ファイルにバグ。PDFKit中のPDFPageのcurrentPage()がブリッジされていない。報告するも未修正(ずっと未修正)

macOS 10.14(AppleScript v2.7)
◯システム環境設定の「アクセシビリティ」>「プライバシー」に、「フルディスクアクセス」「オートーメーション」が新設され、ディスクやリソースへのアクセス、アプリケーションとの通信を管理するようになった。これに伴い、このアプリケーションへのアクセス認証を得ていない場合に発生するエラー(エラーコード-1743)が新設された。
◯サードパーティのOSAXサポートが廃止された
◯スクリプトメニューが、Menu Extraから独立したアプリケーションに変更になった
◯DVDプレイヤーがアプリケーションから補助ツールに格下げになり、AppleScriptからの操作が行えなくなった
◯SIPの機能が強化されセキュリティが強化された。その結果、アプレット内にFrameworkを同梱して配布したり、~/Library/Frameworksに入れたフレームワークをスクリプトエディタが認識しなくなった(事実上、Script Debuggerが必須に)
◯スクリプトエディタのAppleScript用語辞書から「execute」コマンドが削除された
◯住所録(Contacts.app)でプラグインScriptを利用できる機能が削除された(AppleScript用語辞書には残されているが機能しない)

macOS 10.15(AppleScript v2.7)
◯Remote AppleEvents(eppc)経由で他のマシン上のアプリケーションと通信する場合に、同一ユーザー名であることが必要になった。この制限を解除するためには、「defaults write /Library/Preferences/com.apple.AEServer RestrictAccessToUserSession -bool false」の操作をターミナル上で行う必要がある
◯AppleScript専用補助ツール「Image Events」がデフォルト状態ではファイルに一切アクセスできない状態で出荷される(セキュリティ設定によりアクセス可能に。その後のOSバージョンでも修正されない)

◯Cocoa呼び出しを行なった際の実行速度が大幅に低下(→macOS 12で修正)

macOS 11.0(AppleScript v2.7)
◯ARM(Apple Silicon)対応。AppleScript Appletなど実行アプリケーションがすべてApple Silicon/Intel 64のUniversal Binary化
◯Automator Action用に盲腸のように残っていたAppleScript Studioランタイムの廃止が明言される

◯AppleScriptアプレット中にproperty値が保存されなくなった。property値を保存する場合にはUser Defaultsに書き込む必要がある
◯ファイル共有のプロトコルに「afp://」が利用できなくなった。「smb://」に一本化

◯AppleScript書類(.scpt)アイコンが単なるプレーンテキストのものに変わっている(バグなのか本気なのかは不明。macOS 11.x台では修正されない見込み)

◯System Eventsの初回操作時に明示的に「launch」コマンドの実行が必要に(自動起動しない)

◯macOS 11.5において、NSString’s stringWithFormat:メソッドをAppleScriptから呼び出してもクラッシュしなくなった(macOS 11.0〜11.4の間はクラッシュ発生)

◯これまでに書かれたCocoa Scriptingのプログラムで「書き方が間違っているが、許容されてきた」書き方がエラーもしくはクラッシュを引き起こすようになった
×AppleScriptがM1 CPUの高効率コア「Icestorm」で実行されてしまうため、Cocoa Scripting時にIntel CPUにくらべて10分の1〜70分の1程度にパフォーマンス低下(→macOS 12で修正)

macOS 12.0(AppleScript v2.8)
◯ショートカット.appが標準搭載される。ショートカットにAppleScript実行アクションが搭載され、ショートカット中からAppleScript(Cocoa Scripting機能を含む)の呼び出しが可能に
◯ショートカット.appが起動中でなくてもショートカットを実行できるShortcuts Eventsが標準搭載される

◯Piyomaru Softwareの働きかけにより、macOS 10.15で生じたCocoa Scriptingの速度低下、およびmacOS 11で生じたApple Silicon MacのCocoa Scripting速度低下が是正される。Cocoa呼び出し時のAppleScriptの大幅な処理速度向上
◯macOS 12.3、悪意を持って作られたAppleScriptバイナリ(アプレット)のセキュリティ上の問題(CVE-2022-22626)を解消
◯macOS 12.1〜2の間、日本語環境限定で、NSDataDetectorで自然言語テキストから「電話番号」を抽出する機能が動作していなかった(12.3で修正)
◯macOS 12.3、スクリプトエディタのコンテクストメニュー(スクリプトアシスタント表示用)にバグ。項目が複数回表示される(macOS 13で修正)

◯macOS 12.3.1、セキュリティ強化にともなう不具合(Finder上のselectionをオープンすると、ファイルを作成したアプリケーションが起動されないエラーなど)を修正

◯macOS 12.4、AppleScriptアプレットで、入力検証を強化して、領域外読み込みの脆弱性に対処(CVE-2022-26697)。アプリケーションが予期せず終了したり、プロセスメモリが漏洩したりする可能性がある脆弱性に対し、配列境界チェック機能を改善して対処(CVE-2022-26698)。

macOS 13.0(AppleScript v2.8)
◯「システム環境設定」が「システム設定」に。AppleScriptで各種Paneを表示させる機能が動作しなくなった
→ macOS 13.5あたりでmacOS 14同様の機能を実装、機能復旧
◯「Font Book」がAppleScript非対応に
◯「アプリケーションのAppleScript用語辞書をブラウズする方法が、「アプリケーションのアイコンをスクリプトエディタにドラッグ&ドロップする」ものから「アプリケーションのアイコンをスクリプトエディタの「ライブラリ」ウインドウにドラッグ&ドロップもしくは「+」ボタンのクリックで追加するやりかたに変更される(macOS 14で復旧)
×Text To Speech(テキスト読み上げ音声)のIDがすべて変更になる。日本語環境用にSiriのO-renとHattoriが搭載されるが、sayコマンドで使えるようにはなっていない

×AppleScriptからMail.appにメール作成+送信を行うと1通作成ごとに生成されたプロセスがメモリ上から消えず、メモリを食い潰してマシンの処理速度自体が遅くなる
×スクリプトエディタ上で「Cocoa-AppleScript Applet」のテンプレートから作成したアプレットが動作しない問題が発生
◯テキスト読み上げキャラクタのうち一部のキャラクタで名前がローカライズされてしまい、sayコマンドで指定できないバグが発生(日本語環境限定バグ)

macOS 14.0(AppleScript v2.8)
◯「システム設定」のAppleScriptで各種Paneを表示させる機能が復旧
◯Safari v17で隠し命令「show credit card settings」が追加される。do javascript命令の実行許可のための「Apple EventからのJavaScriptを許可」の場所が設定>デベロッパに変更される
◯ミュージック.appでplaylistの「loved」属性が「favorited」に変更される
◯連絡先.appの廃止済みプラグイン機能用の「Address Book Rollover Suite」の属性がhiddenに変更される

◯スクリプトエディタのコンテクストメニュー・スクリプトに、絵文字つきのファイル/フォルダのコンテンツを入れると、コンテクストメニューに「項目が重複して表示される」不具合が修正される
◯「アプリケーションのAppleScript用語辞書をブラウズする方法「アプリケーションのアイコンをスクリプトエディタにドラッグ&ドロップ」が復旧する
×スクリプトエディタ上で「Cocoa-AppleScript Applet」のテンプレートから作成したアプレットが動作しない問題が継続中
×スクリプトメニューから呼び出したAppleScriptの実行速度が、スクリプトエディタ上よりも6倍以上遅くなる現象が発生
×半角スペース文字列の数値への型変換で処理系自体がクラッシュするバグ(14.0 Sonoma Crasher→ 15.0で修正)
×テキスト読み上げキャラクタのうち一部のキャラクタで名前がローカライズされてしまい、sayコマンドで指定できないバグが継続中(日本語環境限定バグ)

macOS 15.0(AppleScript v2.8)
◯Safariのtabにpidプロパティが追加される
◯AppleScriptドロップレットにドロップしたファイル/フォルダが、xattr「com.apple.quarantine」の影響とGatekeeperの兼ね合いですべてが受け渡されない現象がmcOS 10.12から継続して発生していたが、これが改められ、ドロップされたファイル/フォルダの欠損が発生しなくなった。なお、この変更がmacOS 13.xおよび14.xにもさかのぼって適用された
◯半角スペース文字列の数値への型変換で処理系自体がクラッシュするバグ(14.0, Sonoma Crasher)が解消
×スクリプトエディタ上で「Cocoa-AppleScript Applet」のテンプレートから作成したアプレットが動作しない問題が継続中

×スクリプトメニューから呼び出したAppleScriptの実行速度が、スクリプトエディタ上よりも6倍以上遅くなる現象が継続中
×テキスト読み上げキャラクタのうち一部のキャラクタで名前がローカライズされてしまい、sayコマンドで指定できないバグが継続中(日本語環境限定バグ)

macOS 26.0(AppleScript v2.8)
◯スクリプトエディタ上で「Cocoa-AppleScript Applet」のテンプレートから作成したアプレットがApple Silicon Mac上で動作しない問題が解決される(??????)
◯スクリプトエディタ上でコンパイル(構文確認)を行うと、最終行が見栄なくなる門外が解決される

◯スクリプトエディタがDark Modeに対応
◯スクリプトエディタのアイコンがmacOS 26風に変更される

Posted in History | Tagged 26.0savvy | Leave a comment

暗黙のuseコマンド

Posted on 9月 2 by Takaaki Naganoya

かつてAppleScript関連で大きな議論を呼んだ話題に「暗黙のrunハンドラ」というものがありました。

何も書かないAppleScriptのプログラムに暗黙のrunハンドラが存在している(ように動く)という話です。

現在でも同様に、

「明確に仕様として明記されていないものの、おそらくこういう動作になっているだろう」

と推測して書いているものがあります。それが、暗黙のuseコマンドです。

useコマンドはデフォルトで指定されている

何もuseコマンドを書かなくても、仮にuseコマンドを記述しても、パラメータを指定しない場合にはデフォルト値が

use AppleScript
無指定時には、現在実行中のバージョンのAppleScriptで動作するようになっているようです。

use scripting additions
macOS 10.15以降はサードパーティのScripting Additionのサポートが廃止されたため、Apple純正のStandard Additionsのオン/オフ設定コマンドという位置付けになっているようです。

use framework
この記述では、とくに無指定時のデフォルト値というものはないようです。

ただし、Vanilla ScriptとCocoa Scriptで動作が異なっています。また、Script Object内に記述したAppleScriptでも動作が変わります。

AppleScript名:scripting additions 1.scpt
display dialog "TEST"

★Click Here to Open This Script 

AppleScript名:scripting additions in script object.scpt

testScript’s testMes("TEST")

script testScript
  on testMes(aMes as string)
    display dialog aMes
  end testMes
end script

★Click Here to Open This Script 

AppleScript名:scripting additions 2.scpt
use AppleScript
use framework "Foundation"

–display dialog "TEST"–構文チェック自体できない

★Click Here to Open This Script 

AppleScript名:scripting additions in script object.scpt
use AppleScript
use framework "Foundation"

testScript’s testMes("TEST")

script testScript
  use AppleScript
  
use framework "Foundation"
  
use scripting additions –ないと、構文確認をパスできない
  
property parent : AppleScript –なくても動く。Scriptオブジェクトの階層を増やすと必須になったりする
  
  
on testMes(aMes as string)
    display dialog aMes
  end testMes
  
  
on testMes2(aMes as string)
    using terms from scripting additions —どの用語辞書の予約語かを明示的に指定するよう構文確認時に求められるケースがある
      display dialog aMes
    end using terms from
  end testMes2
end script

★Click Here to Open This Script 

Posted in Scripting Additions | Tagged 13.0savvy 14.0savvy 15.0savvy | Leave a comment

開始時刻から終了時刻までhh:mm形式の文字列を15分単位でリスト出力

Posted on 8月 13 by Takaaki Naganoya

指定の開始時刻から終了時刻まで、hh:mm形式の文字列を15分間隔でリスト出力するAppleScriptです。

ありもののサブルーチンを組み合わせて即席で作ったCocoa版のルーチンと、ChatGPT(GPT-5)に書かせた非Cocoa版のルーチンの2つがあり(ChatGPTにCocoa使用を指定せず)、速度を比較してみたら……

非Cocoa版のルーチンのほうが6倍ぐらい高速でした。データが小さすぎるとか、処理内容が小さすぎる場合にはCocoaの機能を利用しないほうが高速に処理できる場合があります。

Shane Stanleyの処理時間計測アプリ「ScriptGeek」が、かなりCocoa Scriptingの登場初期から存在しており、不思議に思ってきたものですが……処理内容の規模によってはCocoa Scriptingのほうが遅くなるケースがあるため、実際に書いて動かして計測してみないと、Cocoa Scriptingで高速化が達成できるかわからないところです。

このほか、AppleScriptのランタイム環境が何になるかによって実行速度は変わってきます。Xcode上で開発するCocoa Applicationなども実行特性が違うため、やはり動かして計測してみないと何がベストなのかはわかりません。

ただ、この程度の処理であれば、0.0028秒が0.4秒になったところで、何か実用上の差異が発生するわけでもありません。

追記:
追加で最速パターンを掲載しておきます。汎用性を求めて計算して出力していましたが、ほとんどの用途で毎回同じようなデータを出力することでしょう。なので、固定データを返すだけでいいだろうかと。高速版とくらべても65〜70倍ぐらい高速です(当たり前)。

AppleScript名:15分メッシュの時間文字列を生成.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/08/13
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

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

set aRes to generateWorkUnitList() of me
–> {"5:15", "5:30", "5:45", "6:00", "6:15", "6:30", "6:45", "7:00", "7:15", "7:30", "7:45", "8:00", "8:15", "8:30", "8:45", "9:00", "9:15", "9:30", "9:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45"}

on generateWorkUnitList()
  set hList to {}
  
  
repeat with h from 5 to 20 by 1
    repeat with m from 0 to 59 by 15
      if {h, m} is not equal to {5, 0} then
        set mStr to numToZeroPaddingStr(m, 2, "0")
        
set the end of hList to (h as string) & ":" & mStr
      end if
    end repeat
  end repeat
  
  
return hList
end generateWorkUnitList

–整数の値に指定桁数ゼロパディングして文字列で返す
on numToZeroPaddingStr(aNum as integer, aDigit as integer, paddingChar as text)
  set aNumForm to current application’s NSNumberFormatter’s alloc()’s init()
  
aNumForm’s setPaddingPosition:(current application’s NSNumberFormatterPadBeforePrefix)
  
aNumForm’s setPaddingCharacter:paddingChar
  
aNumForm’s setMinimumIntegerDigits:aDigit
  
  
set bNum to current application’s NSNumber’s numberWithInt:aNum
  
set aStr to aNumForm’s stringFromNumber:bNum
  
  
return aStr as text
end numToZeroPaddingStr

★Click Here to Open This Script 

AppleScript名:15分メッシュの時間文字列を生成(高速).scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/08/13
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

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

set mList to retWorkingMeshStrList() of me
–> {"5:15", "5:30", "5:45", "6:00", "6:15", "6:30", "6:45", "7:00", "7:15", "7:30", "7:45", "8:00", "8:15", "8:30", "8:45", "9:00", "9:15", "9:30", "9:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00"}

on retWorkingMeshStrList()
  set startSeconds to (5 * 3600) + (15 * 60) — 5:15 の相対秒
  
set endSeconds to (20 * 3600) — 20:00 の相対秒
  
set intervalSeconds to 15 * 60 — 15分間隔
  
  
set timeList to {}
  
  
repeat with t from startSeconds to endSeconds by intervalSeconds
    set h to t div 3600
    
set m to (t mod 3600) div 60
    
— 2桁表示に整形
    
–set hStr to text -2 thru -1 of ("0" & h)
    
set hStr to h as string
    
set mStr to text -2 thru -1 of ("0" & m)
    
set end of timeList to (hStr & ":" & mStr)
  end repeat
  
  
return timeList
end retWorkingMeshStrList

★Click Here to Open This Script 

AppleScript名:15分メッシュの時間文字列を生成(最速).scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/08/10
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

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

set hList to generateWorkUnitList() of me

on generateWorkUnitList()
  return {"5:15", "5:30", "5:45", "6:00", "6:15", "6:30", "6:45", "7:00", "7:15", "7:30", "7:45", "8:00", "8:15", "8:30", "8:45", "9:00", "9:15", "9:30", "9:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00"}
end generateWorkUnitList

★Click Here to Open This Script 

Posted in date list Text | Tagged 14.0savvy 15.0savvy 26.0savvy ChatGPT | Leave a comment

Adobe InDesignのAppleScript実行エンジンがCarbonからCocoaベースに書き換えられる

Posted on 7月 30 by Takaaki Naganoya

最新のAdobe InDesignにおいて、AppleScriptの実行エンジンが従来のCarbonベースのものから、Cocoaベースのものに置き換えられるという情報が入ってきました。

実行エンジンの切り替えにともない、利用できる各種機能に変更点があるかどうかは未確認です。

従来のAdobe InDesignのAppleScript実行エンジンはCocoa系の機能実行に関しては最強レベルのものが搭載されており、Script Debugger並みのユーザーディレクトリ下のFramework呼び出しをはじめ、最強クラスの実行エンジンが搭載されていました。

通常のAppleScript実行はもちろんのこと、幅広い対応を行なっていたCocoa Scripting機能対応が、どの程度新エンジンに継承されているかがみどころです。評価できるといいのに。

Posted in news | Tagged 15.0savvy InDesign | Leave a comment

Finderを終了させる

Posted on 7月 29 by Takaaki Naganoya

Finderを終了させたままにしておくAppleScriptです。

この種類のQ&Aだと、

do shell script "killall Finder"

といった回答が書かれがちですが、これだと終了させたままにはできません。

Finderを終了させたままにしておく方法について調べてみたら、見つかったのでまとめておきました。

AppleScript名:Finderを終了させる.scpt
do shell script "defaults write com.apple.Finder QuitMenuItem -boolean true"

tell application "Finder" to quit

★Click Here to Open This Script 

Posted in process | Tagged 13.0savvy 14.0savvy 15.0 26.0savvy Finder | Leave a comment

Claris FileMaker Pro 2025(v22)がリリースされた

Posted on 7月 12 by Takaaki Naganoya

Appleの子会社であるClarisから、Claris FileMaker Pro 2025(v22)がリリースされました。

AppleScript名:Claris FileMaker Pro 2025のバージョン番号取得.scpt
tell application "FileMaker Pro"
  version
  
–> "22.0.1"
end tell

★Click Here to Open This Script 

AppleScript用語辞書は前バージョンから変更されていません。

Posted in news | Tagged 14.0savvy 15.0savvy 26.0savvy | Leave a comment

ASCII ARTで直線を引く v3.1

Posted on 7月 8 by Takaaki Naganoya

ASCII ARTで指定の2点間に線を引くAppleScriptです。(デモ用の)文字で表現するゲームを作る場合に、基礎ルーチンを整備しておく必要性を感じて、書いておきました。

AppleScript名:ASCII ARTで直線を引く v3.1.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/07/07
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

property widthCount : 40
property heightCount : 25
property spaceChar : " "
property drawChar : "■"

— 使用例
set canvas to makeBlankCanvas() of me

set canvas to drawLine(canvas, 0, 0, 39, 24, 1) of me — 太さ1の線

set curDelim to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to return
set resultText to canvas as text
set AppleScript’s text item delimiters to curDelim

return resultText

on makeBlankCanvas()
  set blankLine to ""
  
repeat widthCount times
    set blankLine to blankLine & spaceChar
  end repeat
  
set canvas to {}
  
repeat heightCount times
    set end of canvas to blankLine
  end repeat
  
return canvas
end makeBlankCanvas

on drawLine(canvas, x0, y0, x1, y1, thickness)
  script spd
    property drawnPositions : {}
    
property drawXCoords : {}
    
property canvas : {}
    
property lineChars : {}
  end script
  
  
copy canvas to (canvas of spd)
  
  
set dx to x1 – x0
  
set dy to y1 – y0
  
set lineLength to (dx * dx + dy * dy) ^ 0.5
  
  
if lineLength = 0 then
    if x0 ≥ 0 and x0 < widthCount and y0 ≥ 0 and y0 < heightCount then
      set theLine to item (y0 + 1) of (canvas of spd)
      
set newLine to replaceCharAt(theLine, x0 + 1, drawChar)
      
set item (y0 + 1) of (canvas of spd) to newLine
    end if
    
    
return (canvas of spd)
  end if
  
  
set nx to –dy / lineLength
  
set ny to dx / lineLength
  
  
set halfThickness to (thickness – 1) / 2
  
if halfThickness < 0 then set halfThickness to 0
  
  
set x to x0
  
set y to y0
  
  
set dxAbs to absNum(dx) of me
  
set dyAbs to absNum(dy) of me
  
set sx to signNum(dx) of me
  
set sy to signNum(dy) of me
  
set err to dxAbs – dyAbs
  
  
set (drawnPositions of spd) to {}
  
  
repeat
    set (drawXCoords of spd) to {}
    
repeat with t from –halfThickness to halfThickness
      set pxF to x + nx * t
      
set pxInt to pxF as integer
      
if pxInt ≥ 0 and pxInt < widthCount then
        if (drawXCoords of spd) does not contain pxInt then
          set end of (drawXCoords of spd) to pxInt
        end if
      end if
    end repeat
    
    
set pyInt to y as integer
    
if pyInt ≥ 0 and pyInt < heightCount then
      set theLine to item (pyInt + 1) of (canvas of spd)
      
set (lineChars of spd) to characters of theLine
      
      
repeat with pxInt in (drawXCoords of spd)
        set posKey to (pxInt as string) & "," & (pyInt as string)
        
        
if (drawnPositions of spd) does not contain posKey then
          set item (pxInt + 1) of (lineChars of spd) to drawChar
          
set end of (drawnPositions of spd) to posKey
        end if
        
      end repeat
      
      
set newLine to (lineChars of spd) as string
      
set item (pyInt + 1) of (canvas of spd) to newLine
    end if
    
    
if x = x1 and y = y1 then exit repeat
    
    
set e2 to 2 * err
    
if e2 > –dyAbs then
      set err to err – dyAbs
      
set x to x + sx
    end if
    
    
if e2 < dxAbs then
      set err to err + dxAbs
      
set y to y + sy
    end if
  end repeat
  
  
return (canvas of spd)
end drawLine

on replaceCharAt(str, pos, char)
  if pos < 1 or pos > (length of str) then return str
  
set prefix to text 1 thru (pos – 1) of str
  
set suffix to text (pos + 1) thru -1 of str
  
return prefix & char & suffix
end replaceCharAt

on absNum(n)
  if n < 0 then return –n
  
return n
end absNum

on signNum(n)
  if n > 0 then return 1
  
if n < 0 then return -1
  
return 0
end signNum

★Click Here to Open This Script 

Posted in Text | Tagged 13.0savvy 14.0savvy 15.0savvy 26.0savvy | Leave a comment

ASCII ARTで円を塗る

Posted on 7月 5 by Takaaki Naganoya

ASCII ARTで円を塗りつぶすAppleScriptです。(デモ用の)文字で表現するゲームを作る場合に、基礎ルーチンを整備しておく必要性を感じて、書いておきました。

AppleScript名:ASCII ARTで円を塗る.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/07/04
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

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

— 円の半径 (この数値を変更すると円の大きさが変わります)
set radiusNum to 12

— キャンバスのサイズ
set canvasWidth to 80
set canvasHeight to 40

— 円の中心座標
set centerX to canvasWidth / 2
set centerY to canvasHeight / 2

— 全角文字のアスペクト比補正係数
set aspectRatio to 0.7
set aaRes to aaFillCircle(radiusNum, canvasWidth, canvasHeight, centerX, centerY, aspectRatio) of me

on aaFillCircle(radiusNum, canvasWidth, canvasHeight, centerX, centerY, aspectRatio)
  script spd
    property outputText : ""
  end script
  
  
— 描画結果を格納する変数
  
set (outputText of spd) to ""
  
  
— 描画処理 (1行ずつ文字を生成)
  
repeat with y from 1 to canvasHeight
    set currentLine to ""
    
repeat with x from 1 to canvasWidth
      — 円の方程式を使い、中心からの距離を計算
      
set distSquared to ((x – centerX) * aspectRatio) ^ 2 + (y – centerY) ^ 2
      
      
— 現在の座標が円の内側または円周上にあるか判定
      
if distSquared ≤ radiusNum ^ 2 then
        set currentLine to currentLine & "■"
      else
        set currentLine to currentLine & " "
      end if
    end repeat
    
— 1行分の文字列と改行を出力に追加
    
set (outputText of spd) to (outputText of spd) & currentLine & "
"
  end repeat
  
  
return contents of (outputText of spd)
end aaFillCircle

★Click Here to Open This Script 

Posted in Text | Tagged 13.0savvy 14.0savvy 15.0savvy | Leave a comment

ASCII ARTで円を線で描く

Posted on 7月 5 by Takaaki Naganoya

ASCII ARTで円を線で描くAppleScriptです。(デモ用の)文字で表現するゲームを作る場合に、基礎ルーチンを整備しておく必要性を感じて、書いておきました。

AppleScript名:ASCII ARTで円を線で描く.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/07/04
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

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

set radiusNum to 12

— キャンバスのサイズ
set canvasWidth to 80
set canvasHeight to 40

— 円の中心座標
set centerX to canvasWidth / 2
set centerY to canvasHeight / 2

— 全角文字のアスペクト比補正係数
set aspectRatio to 0.7

— 描画する線の太さ
set lineWidth to 0.5
set ssRes to aaDrawCircleLine(radiusNum, canvasWidth, canvasHeight, centerX, centerY, aspectRatio, lineWidth) of me

on aaDrawCircleLine(radiusNum, canvasWidth, canvasHeight, centerX, centerY, aspectRatio, lineWidth)
  script spd
    property outputText : ""
  end script
  
  
— 描画結果を格納する変数
  
set (outputText of spd) to ""
  
  
— 描画処理 (1行ずつ文字を生成)
  
repeat with y from 1 to canvasHeight
    set currentLine to ""
    
repeat with x from 1 to canvasWidth
      — 円の方程式を使い、中心からの距離を計算
      
set distSquared to ((x – centerX) * aspectRatio) ^ 2 + (y – centerY) ^ 2
      
      
— 現在の座標が円周上にあるか判定
      
if (distSquared > (radiusNum – lineWidth) ^ 2) and (distSquared < (radiusNum + lineWidth) ^ 2) then
        set currentLine to currentLine & "■"
      else
        set currentLine to currentLine & " "
      end if
    end repeat
    
— 1行分の文字列と改行を出力に追加
    
set (outputText of spd) to (outputText of spd) & currentLine & "
"
  end repeat
  
return contents of (outputText of spd)
end aaDrawCircleLine

★Click Here to Open This Script 

Posted in Text | Tagged 13.0savvy 14.0savvy 15.0savvy | Leave a comment

Appleに買収されたPixelmator ProがAppleとしての初アップデート

Posted on 7月 2 by Takaaki Naganoya

前回の「買収されました」メッセージを表示するだけのアップデートとは異なり、Apple体制下でのそれらしいアップデート(v3.7)が行われました。

AppleScript用語辞書にも変更が加わっているのですが、

Pixelmatorの各バージョンに設定されていたコードネームを返してくるコマンドが廃止になったようです。そんな機能があったとは知りませんでした。廃止はまぁ、機能的に何も影響がないので妥当なところだろうかと。

Posted in Update | Tagged 13.0savvy 14.0savvy 15.0savvy Pixelmator Pro | Leave a comment

NLTaggerで品詞つきの日本語形態素解析

Posted on 6月 30 by Takaaki Naganoya

ChatGPTに日本語形態素解析のAppleScriptを書かせてみました。そのままでは使い物にならず、ずいぶん書き直しましたが、ゼロから書くよりは時間の節約になっています。

ただし、実際に書いて動かしてみるとNLTaggerでは日本語の品詞情報が取得できないので、ほぼ役に立ちません。MeCab経由で取得したほうが実用的です。

この状態で「なんちゃらIntelligence」とか言われても、「はぁ?」としか、、、基礎ができていないのに応用はできませんよ。

AppleScript名:NLTaggerで品詞つきの日本語形態素解析(ただし、品詞はほとんど役に立たない).scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/06/30
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.8" — macOS 12 or later
use framework "Foundation"
use framework "NaturalLanguage"
use scripting additions

set inputText to current application’s NSString’s stringWithString:"私は昨日東京に行きました。"

— NLTaggerの作成(品詞情報を取得したいのでLexicalClassを指定)
set tagger to current application’s NLTagger’s alloc()’s initWithTagSchemes:{current application’s NLTagSchemeLexicalClass}
tagger’s setString:inputText
tagger’s setLanguage:(current application’s NLLanguageJapanese) range:{location:0, |length|:(inputText’s |length|())}

— 結果を格納するリスト
set tokenList to {}

— 解析処理
set inputLength to (inputText’s |length|()) as integer
set currentIndex to 0

repeat while currentIndex < inputLength
  set tokenRangeRef to reference
  
set {tagRes, theRange} to tagger’s tagAtIndex:(currentIndex) unit:(current application’s NLTokenUnitWord) |scheme|:(current application’s NLTagSchemeLexicalClass) tokenRange:(tokenRangeRef)
  
–return theRange
  
  
set theRange to theRange as record
  
set startLoc to theRange’s location
  
set rangeLen to theRange’s |length|()
  
  
if (rangeLen > 0) then
    set tokenText to inputText’s substringWithRange:(current application’s NSMakeRange(startLoc, rangeLen))
    
if tagRes is not missing value then
      set posStr to tagRes as string
    else
      set posStr to "Unknown"
    end if
    
set endIndex to startLoc + rangeLen
    
set end of tokenList to {(tokenText as string), posStr}
    
set currentIndex to endIndex
  else
    exit repeat
  end if
end repeat

return tokenList
–> {{"私", "OtherWord"}, {"は", "OtherWord"}, {"昨日", "OtherWord"}, {"東京", "OtherWord"}, {"に", "OtherWord"}, {"行き", "OtherWord"}, {"まし", "OtherWord"}, {"た", "OtherWord"}, {"。", "SentenceTerminator"}}

★Click Here to Open This Script 

Posted in Natural Language Processing | Tagged 15.0savvy 26.0savvy NLTagger | Leave a comment

Script Debuggerがフリーダウンロードで提供されることに

Posted on 6月 16 by Takaaki Naganoya

2025年1月1日に製品開発の終了を宣言した、AppleScriptの統合開発環境「Script Debugger」について、Late Night Software, ltd.のWebサイトからダウンロードできるようになりました。

→ Download Script Debugger

過去の各バージョンのScript Debugger(v4、v4.5、v5、v6、v7、v8)をダウンロードでき、Registration Numberが記載されています。

ただし、As Isで提供されるものであり、サポートや新たに発見されたバグについては免責とされています。

Posted in news | Tagged Script Debugger | Leave a comment

CotEditor用ブロック崩しゲーム

Posted on 6月 14 by Takaaki Naganoya

CotEditorでオープン中の最前面の書類の内容を書き換えて、ブロック崩し(Breakout)ゲームを行うAppleScriptです。

最前面の書類ウィンドウ内の表示フォントに等幅フォントを指定し、行間をなるべく少なく(0.6行ぐらい)指定した状態で実行してください。

実行はスクリプトエディタなどの実行環境のほか、CotEditorの内蔵スクリプトメニューを利用することも可能です。

CotEditor依存部分はほんの2箇所なので、JeditΩ、mi、BBEdit、Pagesなど幅広いアプリに容易に対応できます。

操作は、パドルの左移動を[option]キー、右移動を[shift]キーで行います。スコアの管理は行なっていません。ミスるとその場でゲームオーバーです。

動作速度は相当に速いので、速すぎてウェイト(delayコマンド)を入れているぐらいです。テキストエディタ上で実行できるブロック崩しとしては、文句のない出来になっていますが、ブロック崩しとしては不満の残る仕上がりです。

開発とテストはMacBook Air M2で行なっているため、delayコマンドの値は同機のパフォーマンスに合わせて調整していますが、Intel Macでは半分以下ぐらいの速度しか出ないので、delayコマンドの値を減らす必要があることでしょう。

Apple Silicon Macの上位機種でコアを貼り合わせてSoCを構成しているものは、シンプルなSoCの構成のものよりもパフォーマンスが下がる可能性もあります。

ボールを打ち返す角度が一定であるため、ブロック崩しといいつつ永遠に消せないブロックが大量に存在しています。実際には、パドルの移動速度を勘案して反射角度を変えるといった処理が必要になることでしょう。

そのままXcode上にもっていって実行していますが、やはりこの反射角度が固定されていることは相当にストレスフルなので、いい感じに書き換えられるとよさそうです。

AppleScript名:Block崩し v3.1.1.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/06/14
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit" — for NSEvent

property screenWidth : 30
property screenHeight : 20
property paddleSize : 6

property ballX : 0
property ballY : 0
property paddleX : 0

script spd
  property blockList : {}
  
property screenLines : {}
  
property lineText : ""
end script

set paddleX to (screenWidth div 2) – (paddleSize div 2)
set ballX to screenWidth div 2
set ballY to screenHeight – 3
set ballDX to 1
set ballDY to -1
set (blockList of spd) to {}

— ブロックは「x,y」の文字列形式で格納し、検索高速化
set blockSet to {}

repeat with y from 1 to 3
  repeat with x from 0 to screenWidth – 1
    if (x mod 2 = 0) then
      set end of blockSet to (x as text) & "," & (y as text)
    end if
  end repeat
end repeat

set text item delimiters to return

–CotEditorに依存している部分(1) 差し替え可能
— CotEditorのドキュメント取得
tell application "CotEditor"
  set dCount to count every document
  
if dCount = 0 then make new document
  
activate
  
set doc to front document
  
display dialog "◀︎[Option] [Shift]▶︎" with title "How to control"
end tell

delay 2

— ゲームループ
repeat
  — キー入力処理
  
if my modKeyScan:"Option" then
    if paddleX > 0 then set paddleX to paddleX – 1
  else if my modKeyScan:"Shift" then
    if paddleX < screenWidth – paddleSize then set paddleX to paddleX + 1
  end if
  
  
— ボール移動
  
set ballX to ballX + ballDX
  
set ballY to ballY + ballDY
  
  
— 壁に当たったら反射
  
if ballX ≤ 0 or ballX ≥ screenWidth – 1 then set ballDX to ballDX * -1
  
if ballY ≤ 0 then set ballDY to ballDY * -1
  
  
— キー入力処理
  
if my modKeyScan:"Option" then
    if paddleX > 0 then set paddleX to paddleX – 1
  else if my modKeyScan:"Shift" then
    if paddleX < screenWidth – paddleSize then set paddleX to paddleX + 1
  end if
  
  
— パドル反射
  
if ballY = screenHeight – 2 and ballX ≥ paddleX and ballX < (paddleX + paddleSize) then
    set ballDY to ballDY * -1
  end if
  
  
— ブロック衝突
  
set coordKey to (ballX as text) & "," & (ballY as text)
  
if coordKey is in blockSet then
    set blockSet to my removeFromList(coordKey, blockSet)
    
set ballDY to ballDY * -1
  end if
  
  
— ゲームオーバー
  
if ballY ≥ screenHeight – 1 then
    my renderScreen(doc, blockSet, paddleX, ballX, ballY, "GAME OVER")
    
exit repeat
  end if
  
  
— 描画
  
my renderScreen(doc, blockSet, paddleX, ballX, ballY, "")
  
delay 0.06
end repeat

— 描画ルーチン(配列構築をまとめて実行)
on renderScreen(theDoc, blockSet, paddleX, ballX, ballY, messageText)
  set screenBuffer to {}
  
repeat with y from 0 to screenHeight – 1
    set rowText to ""
    
repeat with x from 0 to screenWidth – 1
      if y = ballY and x = ballX then
        set rowText to rowText & "●"
      else if y = (screenHeight – 1) and x ≥ paddleX and x < (paddleX + paddleSize) then
        set rowText to rowText & "〓"
      else if ((x as text) & "," & (y as text)) is in blockSet then
        set rowText to rowText & "■"
      else
        set rowText to rowText & " "
      end if
    end repeat
    
set end of screenBuffer to rowText
  end repeat
  
  
if messageText is not "" then
    set end of screenBuffer to ""
    
set end of screenBuffer to messageText
  end if
  
  
–CotEditorに依存している部分(2) 差し替え可能
  
ignoring application responses
    tell application "CotEditor"
      set contents of theDoc to screenBuffer as rich text
    end tell
  end ignoring
end renderScreen

— 指定要素をリストから削除
on removeFromList(target, sourceList)
  set newList to {}
  
repeat with i in sourceList
    set j to contents of i
    
if j is not target then set end of newList to j
  end repeat
  
return newList
end removeFromList

on modKeyScan:(targKeyName as string)
  set chkList to {"fn", "", "", "Command", "Option", "Control", "Shift", "Caps"}
  
  
set theKey to current application’s NSEvent’s modifierFlags() as integer
  
set aBin to binaryEncode(theKey) of me
  
set detectStr to text 9 thru 16 of aBin
  
set detectList to characters of detectStr
  
set aList to {}
  
set aCount to 1
  
  
repeat with i in detectList
    set j to contents of i
    
if j = "1" then
      set jj to contents of item aCount of chkList
      
set the end of aList to jj
    end if
    
set aCount to aCount + 1
  end repeat
  
  
return (targKeyName is in aList) as boolean
end modKeyScan:

–0~2^32範囲の10進数を2進数の文字列に変換して返す
on binaryEncode(aNum)
  if aNum < 0 or 67108864 < aNum then return false
  
set bitStr to ""
  
  
repeat with i from 31 to 0 by -1
    try
      set aRes to aNum div (2 ^ i)
      
set aNum to aNum mod (aRes * (2 ^ i))
    on error
      set aRes to 0
    end try
    
    
set aRes to aRes as integer
    
set bitStr to bitStr & (aRes as string)
  end repeat
  
  
return bitStr
end binaryEncode

★Click Here to Open This Script 

Posted in GAME list | Leave a comment

macOS 26, 15.5でShortcuts.app「AppleScriptを実行」アクションのバグが修正される

Posted on 6月 10 by Takaaki Naganoya

macOS 15.xで続いていた、Shortcuts.appの「AppleScriptを実行」アクションにおいて、デフォルト入力されるべきハンドラなどのScriptが新規追加時に入力されないバグが、修正されたようです。

これは、macOS 26BetaのShortcuts.app上で本バグが修正されていたことから、macOS 15.5上のShortcuts.appの再確認を行ったところ、実際に修正されていることが明らかになりました。

macOS 15.5上のShortcuts.app ようやく「AppleScriptを実行」アクションのバグが修正されたもよう

Posted in Bug | Tagged 15.0savvy 26.0savvy Shortcuts | Leave a comment

macOS 26, Tahoe

Posted on 6月 10 by Takaaki Naganoya

バージョン番号のリナンバーが行われ、macOS 15の次はmacOS 26ということになりました。AppleScriptで「system info」を実行すると、 system version:”26.0″が返ってきます。

スクリプトエディタのバージョンは上がっていませんが、Dark Modeへの対応が行われており、エディタ背景がDark Modeで暗くなるようです。ウィンドウの角丸の半径が大きくなって、全体的にオモチャみたいな印象を受けます。個人的に嫌いではないですが、画面を広く使えない(余白が多い)ので現場によっては困ることも。

AppleScriptのバージョンは2.8で変更なし。まだそんなに真剣に使っていないので、AppleScriptから見て挙動が変わったかといった点はわかりません。

β版のmacOSでありがちな、バージョン取得機能ミスや、aliasからPOSIX pathへの変換ミスなどは見られませんでした。

ただ、WWDC Keynoteで見られたガラス調のUIの見た目については、現時点では見られず、これがまだ実装が間に合っていないためなのか、M1 Mac miniだと環境的に再現しないのかは不明です。

テキスト読み上げ音声系のVoice Characterが増えたりはしていないのですが、「プレミアム」と「拡張」が同時に存在しているなど、ポリシーにゆらぎが見えます。どちらかにするのではないんですね。

バグ:
スクリプトエディタ内からスクリプトメニューをオンに設定しても、ステータスメニュー上にスクリプトメニューが表示されません。すぐにオフになります。この点はバグでしょう(調査が始まったとの話)。

Xcode 26でAppleScript App templateが認識されませんでした。関係者に報告していますが、回答はもらっていません。一応、既存のXcode ProjectをmacOS 26に持って行って、ビルド+実行できることは確認しています。テンプレートの場所や記法が変わったのかも?

→ Xcodeのテンプレートのフォルダが変更になったようです。従来は、~/Library/Xcode/Templatesでしたが、Xcode 26では~/Library/Developer/Xcode/Templates に変わっていました(ドキュメントとかヘルプに記載なし)。

→ 関係者との協議のすえ、これは自分の勘違いで最初から ~/Library/Developer/Xcode/Templates であったことが判明しました。

Posted in news | Tagged 26.0savvy | Leave a comment

Applicationのactivateを記録する v2

Posted on 6月 8 by Takaaki Naganoya

Applicationのactivate(最前面への移動)のイベントを検知して、記録するAppleScriptです。30秒間待って、その間に最前面に移動されたアプリケーションのローカライズ名称をリストに蓄積して返します。Script終了時には、activateされたアプリの名称をリストで返します。

macOS 15.5上で動作を確認しています。スクリプトエディタ、Script Debugger、Xcode(Cocoa Application)での実行を確認していますが、observerの追加宣言の箇所、

notifCenter’s addObserver:me selector:”appWasActivated:” |name|:(current application’s NSWorkspaceDidActivateApplicationNotification) object:(missing value)

で、objectなど記述にエスケープを記述しないと動かないこともありました(Xcode)。「|object|:」と記述する必要が。

イベント記録用の実験用Scriptなので、ほぼ実用性はありません。実際に使い物にするのであれば、リアルタイムでGUIに反映させるとか、アプリごとのactivate期間を集計するとかいったレベルに昇華させる必要があることでしょう。

AppleScript名:activateを記録する v2.scptd
— Created 2015-05-30 18:11:12 +0900 by Piyomaru
— Modified 2025-06-08 12:56:30 +0900 by Piyomaru
— 2015–2025 Piyomaru Software
use AppleScript version "2.8"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property appList : {}
property prevApp : missing value

–アプリケーションのActivateをNotification Centerに登録
set theWorkspace to current application’s NSWorkspace’s sharedWorkspace()
set notifCenter to theWorkspace’s notificationCenter()
notifCenter’s addObserver:me selector:"appWasActivated:" |name|:(current application’s NSWorkspaceDidActivateApplicationNotification) object:(missing value)

delay 30
my stopObserving:(me)

return appList
–> {"Safari", "Finder", "Keynote", "QuickTime Player", "Keynote", "Finder", "スクリプトエディタ"}

on stopObserving:sender
  set theWorkspace to current application’s NSWorkspace’s sharedWorkspace()
  
set notifCenter to theWorkspace’s notificationCenter()
  
notifCenter’s removeObserver:me |name|:(current application’s NSWorkspaceDidActivateApplicationNotification) object:(missing value)
end stopObserving:

on appWasActivated:aNotification
  — query the notification for the active app
  
set theApp to aNotification’s userInfo()’s valueForKey:(current application’s NSWorkspaceApplicationKey)
  
set theAppName to (theApp’s localizedName()) as string
  
if prevApp is not equal to theAppName then
    set prevApp to theAppName
    
set the end of appList to theAppName
  end if
  
  
using terms from scripting additions
    display notification theAppName & " is activated"
  end using terms from
end appWasActivated:

★Click Here to Open This Script 

Posted in list Noification process | Leave a comment

Keynoteの各slideのtitleで、章番号を振り直す v2

Posted on 6月 5 by Takaaki Naganoya

Keynote書類で選択中のスライド(ページ)において、default title itemからテキストを取り出し、正規表現で数字を抽出し、章番号(数字+章)を振り直すAppleScriptです。

本ScriptはmacOS 15.5+Keynote 14.4で動作確認を行なっていますが、バージョン依存する部分はほとんどないので、もっと前のバージョンでも動作することでしょう。本Scriptの動作には、BridgePlus AppleScript Libraryを必要とします。

このサンプルのKeynote書類では、ウィンドウ左端に表示させた「ナビゲータ」の階層表示の機能を利用して、一番上の階層(章トビラなど)だけを選択できるようになっています(そのように編集)。


▲スライドごとに階層構造になっているKeynote書類。章トビラ、記事トビラ、記事といった構造になっている

そのため、このナビゲータ上の階層を最上位のものだけ表示させると、最上位の章トビラだけを選択できます。

この状態で、章トビラのスライドだけを選択した状態で、本AppleScriptを実行すると、スライドのdefault tile item(タイトルが入るテキストボックス)に書かれた「1章」「2章」といった文字を読み取って、範囲内の最小の数値と最大の数値を検出し、冒頭から「1章」「2章」と章番号を割り振り直します。

Keynote書類の編集中に章構成に変更が発生し、章番号の割り振りをやり直す必要がある場合のために作成したものです。

AppleScript名:Keynoteの各slideのtitleで、章番号を振り直す v2.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/06/04
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
–  本バージョンから、Keynoteのスライドのdefault title itemへの描き戻しをサポート

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property NSStringTransformFullwidthToHalfwidth : a reference to current application’s NSStringTransformFullwidthToHalfwidth

load framework

–選択中のスライドのタイトルとスライドオブジェクトを取得
set {aList, aSel} to getTitleAndObjFromSelectedSlides() of me
if {aList, aSel} = {false, false} then error "Keynote書類内のスライド選択状態に異常あり"

–抽出したタイトルから章番号のみ取得し、最小値/最大値/枚数などの情報を取得
set nList to {}
repeat with i in aList
  set j to contents of i
  
set n to filterNumStr(j) of me
  
set the end of nList to n
end repeat

set minRes to calcMin(nList) of me
set maxRes to calcMax(nList) of me
set countRes to maxRes – minRes + 1
set gapRes to calcGaps(nList) of me
log {minRes, maxRes, countRes, gapRes} –最小値、最大値、枚数、連番からの数値抜け(リスト)
–> {1, 30, 30, {13}}

–章番号を振り直すデータを作成。選択範囲内の最小の章番号から1ずつインクリメントして章番号を付与
set n2List to {}
set aCount to minRes
repeat with i in aList
  if aCount < 10 then
    –1桁の数の場合には、全角数字を使用する
    
set aNumStr to hanToZen(aCount as string) of me
  else
    copy aCount to aNumStr
  end if
  
  
set the end of n2List to ((aNumStr as string) & "章")
  
  
set aCount to aCount + 1
end repeat
–return n2List

–選択中のスライドの各タイトルに章番号を振り直し
setEachTitlesToListedSlides(n2List, aSel) of me

–Keynoteの各slideのタイトルを取得する
on getTitleAndObjFromSelectedSlides()
  
  
set aList to {}
  
set selectedTitles to {}
  
  
tell application "Keynote"
    tell front document
      set aSel to selection
      
      
–エラーチェック
      
if length of aSel ≤ 1 then
        display dialog "連番を振り直す対象のスライドが複数枚数選択されていないので処理終了します" buttons {"OK"} default button 1 with icon 1
        
return {false, false}
      end if
      
      
set aFirstClass to class of contents of first item of aSel
      
if aFirstClass is not equal to slide then
        display dialog "スライド単位で選択されていないので処理終了します" buttons {"OK"} default button 1 with icon 1
        
return {false, false}
      end if
      
      
–スライドの逆順選択が発生していた場合には、listを逆順に入れ替えて正順(開始ページ→終了ページ)に修正  
      
set fItem to slide number of first item of aSel
      
set lItem to slide number of last item of aSel
      
if fItem > lItem then set aSel to reverse of aSel
      
      
–選択中のスライドのタイトル(default title item)
      
repeat with i in aSel
        tell i
          try
            set tmpStr to object text of default title item
          on error
            set tmpStr to ""
          end try
        end tell
        
        
set the end of aList to tmpStr
      end repeat
      
      
return {aList, aSel}
    end tell
  end tell
end getTitleAndObjFromSelectedSlides

on setEachTitlesToListedSlides(newList, aSel)
  –連番を振り直したリストを元に、Keynoteの各slideのtitleを変更する
  
set aCount to 1
  
tell application "Keynote"
    tell document 1
      repeat with i in aSel
        tell i
          set object text of default title item to (contents of item aCount of newList)
        end tell
        
set aCount to aCount + 1
      end repeat
    end tell
  end tell
end setEachTitlesToListedSlides

on filterRealNumStr(aStr as string)
  set regStr to "\\d+\\.\\d+"
  
set aRes to findStrByPattern(aStr, regStr) of me
  
return aRes as real
end filterRealNumStr

on filterNumStr(aStr as string)
  set aLen to length of aStr
  
set regStr to "\\d{1," & (aLen as string) & "}"
  
set aRes to findStrByPattern(aStr, regStr) of me
  
return aRes as {boolean, number}
end filterNumStr

on findStrByPattern(aText as string, regStr as string)
  set anNSString to current application’s NSString’s stringWithString:aText
  
set aRange to anNSString’s rangeOfString:regStr options:(current application’s NSRegularExpressionSearch)
  
if aRange = {location:0, length:0} then return ""
  
set bStr to anNSString’s substringWithRange:aRange
  
return bStr as string
end findStrByPattern

on calcMax(aList as list)
  set tmpFItem to first item of aList
  
set aClass to class of tmpFItem
  
  
set nArray to current application’s NSMutableArray’s arrayWithArray:aList
  
if aClass = real then
    set maxRes to (nArray’s valueForKeyPath:"@max.self")’s doubeValue()
  else
    set maxRes to (nArray’s valueForKeyPath:"@max.self")’s intValue()
  end if
  
  
return maxRes
end calcMax

on calcMin(aList as list)
  set tmpFItem to first item of aList
  
set aClass to class of tmpFItem
  
  
set nArray to current application’s NSMutableArray’s arrayWithArray:aList
  
if aClass = real then
    set maxRes to (nArray’s valueForKeyPath:"@min.self")’s doubeValue()
  else
    set maxRes to (nArray’s valueForKeyPath:"@min.self")’s intValue()
  end if
  
  
return maxRes
end calcMin

on calcGaps(aList as list)
  set nArray to (current application’s NSMutableArray’s arrayWithArray:aList)
  
set maxRes to (nArray’s valueForKeyPath:"@max.self")’s intValue()
  
set minRes to (nArray’s valueForKeyPath:"@min.self")’s intValue()
  
  
–最小値から最大値までの連番リスト作成
  
set theIndexSet to current application’s NSIndexSet’s indexSetWithIndexesInRange:{minRes, maxRes}
  
set theList to (current application’s SMSForder’s arrayWithIndexSet:theIndexSet) as list
  
  
–補集合
  
set aSet to current application’s NSMutableSet’s setWithArray:theList
  
set bSet to current application’s NSMutableSet’s setWithArray:nArray
  
aSet’s minusSet:bSet
  
  
return aSet’s allObjects() as list
end calcGaps

–半角→全角変換
on hanToZen(aStr as string)
  set aString to current application’s NSMutableString’s stringWithString:aStr
  
return (aString’s stringByApplyingTransform:(current application’s NSStringTransformFullwidthToHalfwidth) |reverse|:true) as string
end hanToZen

–全角→半角変換
on zenToHan(aStr as string)
  set aString to current application’s NSMutableString’s stringWithString:aStr
  
return (aString’s stringByApplyingTransform:(current application’s NSStringTransformFullwidthToHalfwidth) |reverse|:false) as string
end zenToHan

★Click Here to Open This Script 

Posted in Object control regexp Text | Tagged 15.0savvy Keynote | Leave a comment

デフォルトタイトルと重なっているものと指定サイズのものを抽出

Posted on 5月 26 by Takaaki Naganoya

Keynote書類上でタイトルと(ボックスの領域が)重なるテキスト、および文字サイズが指定サイズと同じテキストを抽出してまとめるAppleScriptを書いてみました。Keynoteで作成した電子書籍の目次データ確認のためのものです。

実行はmacOS 15.5+Keynote v14.4上で行いましたが、とくにバージョン依存している箇所はないので、異なるバージョンであっても使えるはずです。分析対象の書類をKeynoteでオープンした状態で、解析対象のスライド(複数)を選択した状態でスクリプトエディタ上で実行することを前提にしています。

Keynoteのスライド(ページ)からのテキスト抽出は、Cocoaの機能が利用できるようになってからずいぶんと楽になりました。boundsが重なっているものを演算で判別するのも楽勝です。

以下は、執筆中の最新刊「AppleScript+Xcode GUIアプリソース集」の内容にもとづいています(未完成)。

# 同書の作業中、その暫定タイトル名からmacOS 15.5BetaのAppleScript処理系のクラッシュを誘発したといういわくつきのタイトルでもあります(同バグはリリース時に修正されました)

99ソースの部分はまだ添付Xcodeプロジェクトを確認していないので、「だいたいそのぐらいだろう」という暫定値でもあります。

この状態でウィンドウ左側のカバーページ部分だけを選択すると、selectionでカバーページだけを抽出できます。

カバーページに対して、デフォルトタイトルアイテム(ここでは章番号が入る)と、そのエリアに重なっているテキストアイテムを抽出。

さらに、中央に配置している96ポイントのテキストアイテムを抽出。

あとは、Keynote(iWorks Apps)でよくあるテキストアイテムから抽出したテキストに謎コード(iWork共通のオブジェクト内強制改行)が入っていて改行を解除できないという問題にも対処しています。

また、KeynoteなどのiWork Apps共通の問題点である「オブジェクト同士のIDによる識別ができない」点については、propertiesでまとめて属性値を取得し、この属性値でまとめて判定を行っています。少々遅くなりそうな感じでしたが、実際に試してみたら(マシンが速いので?)問題ありませんでした。

1章 さあ、はじめよう!
基礎的な内容のご説明
2章 ウィンドウ
各種部品を格納する基本的な部品
3章 ボタン
クリック操作を受け付けるもっとも基本的な部品
4章 テキストフィールド
文字や数値などの入力を受け付け、表示を行う
5章 テキストビュー
複数行のスタイルつきテキストを扱える入力・表示部品
6章 デートピッカー
カレンダー情報の入力を受け付ける
7章 イメージビュー
画像(色)の表示とドラッグ&ドロップ受け付け
8章 パスコントロール
フォルダやファイルパスのユーザーによる選択や表示
9章 スライダー
数値の入力、表示
10章 プログレスインジケータ
App Storeに出すなら必須の途中経過表示部品
11章 テーブルビュー
表データの表示、入力用ぜひ使いたい部品
12章 アウトラインビュー
階層化データの入力、表示用
13章 ブラウザ
ツリーデータ、フォルダ階層構造の表示・選択
14章 スプリットビューほか
複数のGUI部品をまとめて使いやすく
15章 メニュー
アプリの機能をひととおり呼び出す階層メニュー
16章 ポップオーバー
ボタンなどからオーバーレイ表示する簡易子ウィンドウ
17章 アドレスブックピープルピッカー
住所録データを表示・選択する
18章 AVキットプレイヤービュー
各種ムービーを再生表示
19章 マップキットビュー
場所と倍率と形式を指定して世界地図を表示
20章 OSAキットビュー
スクリプトエディタ部品でAS編集・実行
21章 PDFキットビュー
PDFとサムネイル表示
22章 ウェブキットビュー
Webコンテンツの表示と操作
23章 サービス
各アプリに対して共通で機能を提供する
24章 ドキュメントベースアプリ
「書類」を扱うアプリ形式
25章 外部アプリコントロール
AppleScriptで他のGUIアプリを操作
26章 スクリプタブル化
アプリ自体を外部からAppleScriptで操作可能に
27章 アプリアイコン
Dock上に表示するアプリアイコンを操作する
28章 ダイアログ
文字入力ダイアログを表示して文字入力
29章 アニメーション
GUI部品をアニメーションさせてユーザーの操作を促す
30章 その他
その他のこまごまとした実用的なノウハウ
あとがき、奥付 Author’s notes
ご愛読、ご協力感謝申し上げます。

すべて自分で作ったデータなので、非常に「ゆらぎ」が小さくなっており、そのデータ品質を前提に作っています。

実際にはもっとデータ品質が低いものと思われるため、そうした低品質データに対処するためには、もっといろいろな処理が必要になってくることでしょう。

処理対象データ(ここではKeynote書類)が事前に予想したとおりの構造(default title item使用)や品質(データのばらつきの存在)になっているかは、AppleScriptを何回か走らせてデータの内容を確認するものでもあるため、最初から一発で想定どおりの処理ができるわけではありません。

AppleScript名:デフォルトタイトルと重なっているものと指定サイズのものを抽出.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2025/05/26
—
–  Copyright © 2025 Piyomaru Software, All Rights Reserved
—

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

set targSize to 96.0 –取り出し対象の文字サイズ
set outList to ""

tell application "Keynote"
  tell front document
    set aSel to selection
    
repeat with i in aSel
      set j to contents of i
      
      
tell j
        set tList to every text item
        
        
set dItem to default title item
        
set dProp to properties of dItem
        
set dText to object text of dItem –テキスト
        
        
set dPos to position of default title item
        
set dHeight to height of default title item
        
set dWidth to width of default title item
        
set dList to dPos & {dWidth, dHeight}
        
        
–座標領域が重なっているオブジェクトを抽出
        
set colList to {}
        
repeat with ii in tList
          set jj to contents of ii
          
set tPos to position of jj
          
set tHeight to height of jj
          
set tWidth to width of jj
          
set ttList to tPos & {tHeight, tHeight}
          
set tProp to properties of jj
          
          
if dProp is not equal to tProp then
            set gCol to getCollision(dList, ttList) of me
            
if gCol = true then
              set the end of colList to jj
            end if
          end if
        end repeat
        
        
–指定フォントサイズのテキストアイテムを抽出
        
set sizeList to {}
        
repeat with ii in tList
          set jj to contents of ii
          
set tSize to size of object text of jj
          
if tSize = targSize then
            set the end of sizeList to jj
          end if
        end repeat
        
        
–出力用テキストの整形
        
set t1 to cleanUpKeynoteTextObj(dText) of me
        
set t2 to cleanUpKeynoteTextObj(object text of item 1 of sizeList) of me
        
set t3 to cleanUpKeynoteTextObj(object text of item 1 of colList) of me
        
        
set outStr to t1 & tab & t3 & return & t2
        
set outList to outList & return & outStr
        
      end tell
    end repeat
  end tell
end tell

return outList

–iWorkのtext itemのオブジェクト内改行を削除
on cleanUpKeynoteTextObj(tObj)
  set tObj to repChar(tObj, string id 10, "") of me –LF
  
set tObj to repChar(tObj, string id 13, "") of me –CR
  
set tObj to repChar(tObj, string id 8232, "") of me –強制改行?
  
return tObj
end cleanUpKeynoteTextObj

on getCollision(aList, bList)
  copy aList to {aXn, aYn, aWn, aHn}
  
copy bList to {bXn, bYn, bWn, bHn}
  
  
set a4Rect to current application’s NSMakeRect(aXn, aYn, aWn, aHn)
  
set b4Rect to current application’s NSMakeRect(bXn, bYn, bWn, bHn)
  
set a4Res to detectRectanglesCollision(a4Rect, b4Rect) of me
  
  
return a4Res
end getCollision

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

on repChar(origText as string, targStr as string, repStr as string)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChar

★Click Here to Open This Script 

Posted in bounds list | Tagged 15.0savvy Keynote | Leave a comment

Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行

Posted on 5月 13 by Takaaki Naganoya

Xcode上でAppleScriptによるアプリ開発を行なったときに、logコマンドによるXcodeのログ出力欄への出力が、最近になって行えなくなってしまいました。

そこで、テキストエディタにログ出力したり、NSLogでsyslogに出力したりと、いろいろな方法を試してみるわけですが……他のアプリに出力するのはできなくはないものの、手軽さに欠けますし、syslogに出力されたメッセージをコンソール.app上で検索するのも大変です。

ところが、他の言語処理系(Objective-Cなど)ではNSLogを用いてログ出力するとXcodeのログ表示欄にメッセージが表示されるとかで、教えていただいて腰を抜かしました。

そこで、以前に作ったデータの型と内容表示用のプログラムと合わせて、手軽にXcodeのログ表示欄にデータ内容を出力するプログラムを書いてみました。

set theResult to (current date) as string
Logger's logMessage_(theResult)
Logger's printObjectInfo_(sender)
Logger's printObjectInfo_({1,2,3})
Logger's printObjectInfo_({aLabel:1})
Logger's printObjectInfo_(1.2)
Logger's printObjectInfo_("Piyomaru")

さまざまなデータをXcodeのログ表示欄に出力できていることが見てとれます。

–> Download LogTest_V2

macOS 15.5+Xcode 16.3上で動作確認を行なっています。現在執筆中のこちら(↓)の本では、 Xcode+AppleScriptの多彩なサンプル・プロジェクトを紹介しており、本プロジェクトもその1つです。

Posted in AppleScript Application on Xcode | Leave a comment

Post navigation

  • Older posts

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

Google Search

Popular posts

  • Numbersで選択範囲のセルの前後の空白を削除
  • macOS 15でも変化したText to Speech環境
  • macOS 26, Tahoe
  • AppleScript入門③AppleScriptを使った「自動化」とは?
  • KagiのWebブラウザ、Orion
  • Script Debuggerの開発と販売が2025年に終了
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • macOS 14で変更になったOSバージョン取得APIの返り値
  • AppleScript入門① AppleScriptってなんだろう?
  • macOS 15 リモートApple Eventsにバグ?
  • NSObjectのクラス名を取得 v2.1
  • macOS 15:スクリプトエディタのAppleScript用語辞書を確認できない
  • 2024年に書いた価値あるAppleScript
  • 有害ではなくなっていたSpaces
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行
  • Pixelmator Proがv3.6.8でHDR画像をサポート
  • (確認中)AppleScript Dropletのバグっぽい動作が解消?
  • AVSpeechSynthesizerで読み上げテスト
  • AppleScript Dropletのバグっぽい動作が「復活」(macOS 15.5β)
  • 指定フォルダ以下の画像のMD5チェックサムを求めて、重複しているものをピックアップ

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (201) 14.0savvy (156) 15.0savvy (145) CotEditor (66) Finder (52) Keynote (119) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (76) Pages (55) Pixelmator Pro (20) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • date
  • dialog
  • diff
  • drive
  • Droplet
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • Scripting Additions
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2025年9月
  • 2025年8月
  • 2025年7月
  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC