Objective-C メモ

Last-modified: 2006-12-04 (月) 18:46:28
 

アップルスクリプトの実行(外部ファイル)

NSAppleEventDescriptor *aed;
NSDictionary  *asErrDic = nil;
NSString *asPath = [ [ [ NSBundle mainBundle ]
					resourcePath ]
					stringByAppendingPathComponent :
	@"fileName.applescript"
];
NSURL *url = [ NSURL fileURLWithPath : asPath ];
NSAppleScript *as = [ NSAppleScript initWithContentsOfURL : url error : &asErrDic ];
[ as executeAndReturnError : &asErrDic ];
aed = [ as executeAndReturnError : &asErrDic ];
if ( asErrDic ) {
    …エラー処理…
}
if ( [ [ aed stringValue ] intValue ] == 0 )
{
	…
}

※スクリプトファイルの拡張子が、「.scrt」ではなく「.applescript」なのに注意
※パッケージ > Contents > Resources の中に置く

 
環境:IntelCPU+MacOS10.4.8+Xcode2.4
2006-10-XX

アップルスクリプトの実行(文字列)

NSAppleEventDescriptor *aed;
NSDictionary  *asErrDic = nil;
NSAppleScript *as = [ [ [ NSAppleScript alloc ] autorelease ] initWithSource :
	@"tell application \"iTunes\" \n\
	… \n\
	end tell"
];
[ as executeAndReturnError : &asErrDic ];
aed = [ as executeAndReturnError : &asErrDic ];
if ( asErrDic ) {
    …エラー処理…
}
if ( [ @"false" isEqualToString : [ aed stringValue ] ] )
{
	…
}
 
環境:IntelCPU+MacOS10.4.8+Xcode2.4
2006-10-XX

アプリケーション起動確認

// 起動中のアプリケーションを取得
NSArray*	applications;
applications = [ NSArray arrayWithArray :
				[ [ NSWorkspace sharedWorkspace ]
				launchedApplications ] ];
// iTunes起動確認
int i;
runiTunes = NO;
for ( i = 0; i < [ applications count ]; i++ )
{
	if ( [ @"iTunes" isEqualToString :
		[ [ applications objectAtIndex : i ]
		objectForKey : @"NSApplicationName" ] ] )
	{
		runiTunes = YES;
		break;
	}
}
 
環境:IntelCPU+MacOS10.4.8+Xcode2.4
2006-10-XX

ホットキーを設定

// Command + Sキー
[ 該当アイテム setKeyEquivalent : @"s" ];
[ 該当アイテム setKeyEquivalentModifierMask : NSCommandKeyMask ];
// Control + Shift  + Sキー
[ 該当アイテム setKeyEquivalent : @"S" ];
[ 該当アイテム setKeyEquivalentModifierMask : NSControlKeyMask ];
// Command + Alt + Sキー
[ 該当アイテム setKeyEquivalent : @"s" ];
[ 該当アイテム setKeyEquivalentModifierMask : NSCommandKeyMask | NSAlternateKeyMask ];

setKeyEquivalentModifierMask は NSControlKeyMask、NSCommandKeyMask、NSAlternateKeyMask の3種を or でつなぐ。
Shift は setKeyEquivalent の指定文字が大文字の場合にホットキーになる。

 
環境:IntelCPU+MacOS10.4.8+Xcode2.4.1
2006-12-02

初期化処理

File's Owner の delegate にインスタンスを設定しておく。

/**** アプリケーションの起動処理後に呼ばれる処理 ****/
- ( void )applicationDidFinishLaunching : ( NSNotification* )aNote
{
    // 初期化処理
    …
}
 
環境:IntelCPU+MacOS10.4.8+Xcode2.4.1
2006-12-02

最後のウィンドウが閉じられたらアプリを終了する

File's Owner の delegate にインスタンスを設定しておく。

/**** 最後のウィンドウが閉じられた時の処理 ****/
- ( BOOL )applicationShouldTerminateAfterLastWindowClosed : ( NSApplication* )theApplication
{
    // アプリを終了する
    return YES;
}
 
環境:IntelCPU+MacOS10.4.8+Xcode2.4.1
2006-12-02

Localizable.strings から値を取得する

Localizable.strings(English)

PlayString = "Play";

Localizable.strings(Japanese)

PlayString = "再生";

値を取得したい場所

NSBundle* bundle;
NSString* playString;
bundle = [ NSBundle mainBundle ];
// 英語環境では「Play」、日本語環境では「再生」を取得できる
playString = [ bundle localizedStringForKey : @"PlayString" value : nil table : nil ];
 
環境:IntelCPU+MacOS10.4.8+Xcode2.4.1
2006-12-04

文字を数値に変換

int intTime = [ @"20" intValue ];
 
環境:IntelCPU+MacOS10.4.8+Xcode2.4.1
2006-12-04

数値を文字に変換

NSString* strTime;
strTime = [ NSString stringWithFormat : @"%d", 20 ];
 
環境:IntelCPU+MacOS10.4.8+Xcode2.4.1
2006-12-04