AppleScript Studio でツールバーを使ってみる (2)

さて、何はともあれツールバーを使ってみましょう。AppleScript Studio では、Objective-C でデリゲートメソッドとして用意されているメソッドが toolbar クラスの属性として用意されています。AppleScript Studio ではこれらの属性を使ってツールバーを作成します。

ツールバーを利用するには、

  1. ウィンドウにつけるツールバーを作成
  2. ツールバーで利用出来るツールバーアイテムを登録
  3. デフォルトのツールバーアイテムを設定する
  4. ウィンドウに接続する

というステップを踏まなくてはいけません。これらのステップは、ツールバーを利用する上での最低限必要なものです。

それぞれのステップを具体的に見ていきます。

ツールバーは、ウィンドウに付加するものなのでウィンドウの awake from nib イベントハンドラを利用するのが手軽だと思います。まずは、プロジェクトを作成しウィンドウに awake from nib イベントハンドラを接続します。awake from nib ハンドラ内で上記のステップを行います。最初にツールバーを作成します。

set theToolbar to make new toolbar at end with properties {name:"new toolbar", identifier:"new toolbar", allows customization:true, display mode:default display mode, size mode:default size mode, auto sizes cells:true}

ツールバーの作成は make 命令で行います。ここで属性をまとめて設定しています。

name -- AppleScript がオブジェクトを判別するための名前
identifier -- ツールバーの一意の識別子
allows customization -- ツールバーのカスタマイズが行えるかどうか
display mode -- ツールバーアイテムの表示形式。アイコンのみか、アイコンとラベルか等
size mode -- ツールバーアイテムの表示サイズ
auto sizes cells -- ツールバーアイテムを自動で保存するか

これら以外にも属性はありますが、それらは後ほど。ところで、これらの属性の中の auto sizes cells ですが、これは auto saves configuration 属性のことです。この属性は、ユーザーがカスタマイズしたツールバーアイテム(ツールバーに表示される個々のオブジェクト)を自動で保存するかどうかの属性ですが、構文確認を行うとなぜか auto sizes cells になってしまいます。しかし、動作自体は正しく行われているようです。auto sizes cells は、matrix クラスの属性です。

ツールバーの作成が出来たら次にツールバーで利用出来るツールバーアイテムの登録を行います。Objective-C では、toolbarAllowedItemIdentifiers: というデリゲートメソッドで行いますが、AppleScript Studio では、toolbar クラスの allowed identifiers 属性を利用します。ツールバーは、ここに登録したツールバーアイテムのみが利用出来るようになります。ツールバーは、ツールバーアイテムの識別子を利用して個々のオブジェクトを判別します。AppleScript Studio は多くの場合、name 属性を利用して個々のオブジェクトを判別しますが、ツールバーでは name 属性ではなく identifier(識別子)を多用します。

ツールバーアイテムには、フレームワーク側が標準で用意しているツールバーアイテムがあります。

print item identifier -- 印刷
show colors item identifier -- カラーパネル表示
show fonts item identifier -- フォントパネル表示
customize toolbar item identifer -- ツールバーカスタマイズ
space item identifier -- スペース
flexible space item identifier -- 伸張可能なスペース
separator item identifier -- 区切り

いろんなアプリケーションがツールバーを利用していますが、どのアプリケーションでも共通しているツールバーアイテムがあります。それらはこういった標準で用意されているツールバーアイテムを利用しているのです。これら標準で用意されているものを使ってみます。

set allowed identifiers of theToolbar to {"print item identifier", "show colors item identifier", "show fonts item identifier", "customize toolbar item identifer", "space item identifier", "flexible space item identifier", "separator item identifier"}

ツールバーで利用出来るツールバーアイテムをここで設定します。繰り返しますが、ここでは name 属性ではなく identifier 属性を利用します。

次にデフォルトのツールバーアイテムを設定します。これまた、Objective-C では toolbarDefaultItemIdentifiers: として用意されているデリゲートメソッドが toolbar クラスの属性として用意されています。default identifiers 属性は、最初にツールバーに表示されるツールバーアイテムを設定する属性です。

set default identifiers of theToolbar to {"print item identifier", "customize toolbar item identifer"}

ここでもツールバーアイテムの identifier 属性をまとめたリストを指定します。最後にウィンドウにツールバーを設定します。

set toolbar of theObject to theToolbar

スクリプトの全体は以下のようになります。

on awake from nib theObject
    if name of theObject is "main" then
        set theToolbar to make new toolbar at end with properties {name:"new toolbar", identifier:"new toolbar", allows customization:true, display mode:default display mode, size mode:default size mode, auto sizes cells:true}

        set allowed identifiers of theToolbar to {"print item identifier", "show colors item identifier", "show fonts item identifier", "customize toolbar item identifer", "space item identifier", "flexible space item identifier", "separator item identifier"}
        set default identifiers of theToolbar to {"print item identifier", "customize toolbar item identifer"}

        set toolbar of theObject to theToolbar
    end if
end awake from nib

これでビルドして実行するとツールバーがついたウィンドウが表示されます。ツールバーをカスタマイズしてからアプリケーションを終了し、再度起動させるとカスタマイズされたツールバーが表示されます。この自動保存が先ほどの auto saves configuration 属性になります。

ざっと駆け足でしたが、ツールバーの詳しいことは次回。

0 件のコメント :

コメントを投稿