LoginSignup
3
4

More than 3 years have passed since last update.

[macOS] Cocoa Scripting Support

Last updated at Posted at 2020-07-24

Cocoa Scripting

Cocoa Scriptingとは

  • Cocoa Scriptingとは、スクリプト制御可能なmacOSアプリケーションを実装するための、Cocoaフレームワークの機能を指します。これをサポートしたアプリケーションは、AppleScriptで制御可能になります。
  • 当初は、Apple Event Managerが使用されていましたが、Tiger以降はCocoa Scriptingがこれに取って変わりました。

Cocoa Scriptのサポート準備

macOSアプリにてCocoa Scriptingをサポートする際には、実装以前に複数項目の準備が必要です。

ターゲットプロパティ (.plistファイル)

plistファイルに以下の要素を追加し、CocoaScriptingサポートを有効化します。設定中のScripting.sdefは後述するスクリプト定義ファイルの名前です。
下記の例では、.sdefファイルは、アプリケーションのResourceフォルダの直下におかれる事を想定しています。

<key>NSAppleScriptEnabled</key>
<string>YES</string>
<key>OSAScriptingDefinition</key>
<string>Scripting.sdef</string>

スクリプト定義 (.sdefファイル)

アプリケーションがサポートするスクリプトの種類を定義します。 xmlフォーマットに基づいて記述します。
詳細については、Preparing a Scripting Definition Fileを参照して下さい。

以下はsdefファイルの一例です。最上位は、`dictionaryノードです。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="CoconutScript">
... スクリプト定義 ...
</dictionary>
`

dictionaryノードは、1つ以上のsuiteノードを含みます。関連のある定義が1つのsuiteノードに格納されます。

<dictionary title="CoconutScript">
<suite name="Preference Suite" code="CcPS" description=" Suites by CocoaScript">
<class name="application" code="capp" description="The application's top-level scripting object.">
... Application suite 定義 ...
</class>
</suite>
</dictionary>

suiteの1つです。アプリケーションクラスを定義します。アプリケーションクラスは、NSApplicationDelegateにマップされます。のインターフェースを定義します。

<class name="application" code="capp" description="The application's top-level scripting object.">
<cocoa class="NSApplication"/>
... Application プロパティ定義 ...
</class>

以下の例では、アプリケーションクラスに、foreground colorbackground colorプロパティを定義します。

<property name="foreground color" code="fgcl" description="Foreground color of the terminal" type="Color" access="rw"/>
<property name="background color" code="bgcl" description="Background color of the terminal" type="Color" access="rw"/>

機能実装

AppleScriptからアプリケーションへのアクセスは、KVOのプロトコルで通知されます。.sdefファイルのプロパティのname要素で指定した名前が、キーとして使用されます。

open class CNScriptableAppicationDelegate: CNApplicationDelegate
{
  public override func value(forKey key: String) -> Any? {
  }
  open override func setValue(_ value: Any?, forKey key: String) {
  }
}

参考文献

  1. Cocoa Scripting Guide: Cocoa Scriptingについて、おそらく最も詳細なドキュメント。ただ、かなり昔(2008年)に書かれている。PDF版はこちら
  2. Getting Started With Cocoa Scripting: Cocoa Scriptingについて、簡単な例を持って説明。[1]よりかなりシンプル。
3
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4