• 検索結果がありません。

- 129 -110. 爆発アニメーションを表示する

● NSShowAnimationEffect を使う

Mac OS X 10.3.0〜

ド ッ ク や ツ ー ル バ ー か ら ア イ テ ム を 取 り 除 い た と き な ど に 表 示 さ れ る 爆 発 ア ニ メ ー シ ョ ン は 、 NSShowAnimationEffect でアニメーションの種類に NSAnimationEffectPoof を指定することで表示するこ とが出来ます。

様々なアニメーションを表示する

書式

void NSShowAnimationEffect(

     NSAnimationEffect animationEffect, // このアニメーションを       NSPoint centerLocation, // この場所に

      NSSize size, // このサイズで表示する       id delegate, // 終わったらこのオブジェクトの       SEL didEndSelector, // このメソッドを呼ぶ

      void* contextInfo ) // そのメソッドにはこれを渡す 入力

 animationEffect : アニメーションの種類

 centerLocation : アニメーションの中心の位置 (画面の座標)  size : アニメーションを表示するサイズ  delegate : デリゲートオブジェクト (なければnil)

 didEndSelector : アニメーション終了時に呼ぶメソッド (なければnil)  contextInfo : デリゲートメソッドに渡すデータ (なければnil) 備考

 アニメーション終了後にデリゲートオブジェクトのdidEndSelectorが呼ばれる。

アニメーションの種類を NSAnimationEffectPoof に指定しているとき、size に NSZeroSize を渡すとデフ ォルトサイズが指定されます。アニメーション終了後によばれるメソッドは以下のような形式になります。

(void) animationEffectDidEnd : (void*) contextInfo;

メソッド名は自由に決めて構いません。引数には NSShowAnimationEffect で指定した contextInfo の値が

そのまま届きます。アニメーション終了後に特にすることがなければ、最後の 3 つの引数は全て nil にします。

(SAMPLE) マウスカーソルの位置に爆発アニメーションを表示する

NSShowAnimationEffect( NSAnimationEffectPoof, [ NSEvent mouseLocation ], NSZeroSize, nil, nil, nil );

検索用キーワード:削除時のアニメーション

- 131 -111. AppleScript ファイルを実行する

● - [ NSAppleScript initWithContentsOfURL : error : ] を使う

AppleScript のスクリプトファイルを実行するには、 - [ NSAppleScript  initWithContentsOfURL : error : ] を使います。

SAMPLE : Resources内のTEST.applescriptを実行する

// Resourcesの中のFileのパスをNSURLにする

NSString *asPath = [ [ [ NSBundle mainBundle ] resourcePath ]

stringByAppendingPathComponent : @"TEST.applescript" ];

NSURL *url = [ NSURL fileURLWithPath : asPath ];

// 初期化

NSDictionary *asErrDic = nil; // エラー情報 NSAppleScript *as = [ [ NSAppleScript alloc ] initWithContentsOfURL : url

error : &asErrDic ];

// 実行

NSAppleEventDescriptor *aed;

aed = [ as executeAndReturnError : &asErrDic ];

このサンプルでは、アプリケーションの中の Resources フォルダーの中に「  TEST.appleScript  」という ファイルを入れておいてそれを読み込んでいます。こうすることで、アプリケーションの中にスクリプトのフ ァイルが隠されることになりますので、アプリケーションを利用しているユーザーには、普通のアプリケーシ ョンにしか見えません。

NSAppleScript : ファイルで与えられたスクリプトで初期化する

書式

 - (id) initWithContentsOfURL : (NSURL *) url   error : (NSDictionary **) err 入力

 url : 実行したいスクリプトファイルの場所

 err : エラー情報を受け取る辞書のポインタのアドレス 出力

 err : エラー情報

 返り値 : 初期化されたインスタンス

NSAppleScript : ファイルで与えられたスクリプトで初期化する

書式

 - (NSAppleEventDescriptor *) executeAndReturnError : (NSDictionary **) err 入力

 err : エラー情報を受け取る辞書のポインタのアドレス 出力

 err : エラー情報

 返り値 : AppleScriptから戻された情報

- 133 -112. 文字列の AppleScript を実行する

● - [ NSAppleScript initWithSource : ] を使う

文字列で与えられた AppleScipt を実行するには、- [ NSAppleScript initWithSource : ] を使います。

SAMPLE : デフォルトブラウザでAppleのサイトを表示する

NSDictionary *asErrDic = nil; // エラー情報

NSAppleScript *as = [ [ NSAppleScript alloc ]

initWithSource : @"open location ¥"http://www.apple.com/¥"" ]; // 初期化 [ as executeAndReturnError : &asErrDic ]; // 実行

NSAppleScript : 文字列で与えられたスクリプトで初期化する

書式

 - (id) initWithSource : (NSString *) source 入力

 source : 実行したいスクリプトの文字列 出力

 返り値 : 初期化されたインスタンス

113. AppleScript から文字列データを取得する

● - [ NSAppleEventDescriptor stringValue : ] を使う

実行した AppleScript から文字列データを取得した場合は、- [ NSAppleEventDescriptor stringValue ] を 使います。

SAMPLE : AppleScriptから文字列受け取って表示する

 : 略

NSAppleEventDescriptor* aed = [ as executeAndReturnError : &asErrDic ];

NSLog( @"%@", [ aed stringValue ] ); // 結果を表示

114. AppleScript から配列データを取得する

● - [ NSAppleEventDescriptor descriptorAtIndex : ] を使う

実 行 し た AppleScript か ら 配 列 デ ー タ を 取 得 し た 場 合 は 、 -  [  NSAppleEventDescriptor descriptorAtIndex  :  ]  を 使 っ て 配 列 の 各 要 素 に ア ク セ ス し ま す 。 そ の 各 要 素 に 対 し て   -[ NSAppleEventDescriptor stringValue ] 等のメソッドを実行して中身を取り出します。

SAMPLE : AppleScriptから文字列の配列を受け取って表示する

 : 略

NSAppleEventDescriptor* aed = [ as executeAndReturnError : &asErrDic ];

// 結果を表示 int i;

int iNum = [ aed numberOfItems ]; // データの個数を取得 for ( i = 1 ; i <= iNum ; i++ ) {

NSLog( @"%d = %@", i, [ [ aed descriptorAtIndex : i ] stringValue ] );

- 135 -115. Mutable でないオブジェクトから Mutable なオブジェクトを得る

● - [ <NSObject> mutableCopy ] を使う

NSString や NSArray などのオブジェクトは変更したくても変更できません。その場合、そのオブジェクト をもとに NSMutableString や NSMutableArray を  -  [  <NSObject>  mutableCopy  ]  でコピーを生成するこ とが出来ます。

SAMPLE : NSStringからNSMutableStringを生成する

NSString* str1 = @"HELLO";

NSMutableString* str2 = [ str1 mutableCopy ];

SAMPLE : NSArrayからNSMutableArrayを生成する

NSArray* array1 = [ NSArray arrayWithObjects : @"HELLO", @"BYE", nil ];

NSMutableArray* array2 = [ array1 mutableCopy ];

関連したドキュメント