Project Builderで、新たなプロジェクトを、Cocoa Applicationのテンプレートを使って作成する。そして、作成されたプロジェクトにあるmain.cの内容を以下のプログラムにそっくりそのまま入れ替えれば、利用することができる。
/* main.c */
#include
OSStatus LaunchItem(FSRef * app); OSStatus LaunchDocumentWithApp(FSRef * doc, UInt32 nDocs, FSRef * app); OSStatus OpenURL(CFStringRef urlStr);
/* 3種類のプログラムを用意したが、以下の3行のdefineのうち、1つだけ コメントをはずすようにしてどのプログラムを動かすかを決める。 */ //#define Try1 #define Try2 //#define Try3
int main(int argc, char *argv[]) { OSStatus er; FSRef editTextRef; char path[256]; #ifdef Try2 NavReplyRecord reply; UInt32 itemsInList; FSRef* docArray; AEKeyword keywd; DescType returnedType; Size actualSize; int i; #endif LSInit(kLSInitializeDefaults); //Launch Servicesの初期化、引数はこれを指定しろとなっている
strcpy(path, "/Applications/TextEdit.app"); //TextEditへのパスを記述 er = FSPathMakeRef(path, & editTextRef, NULL); //TextEditへのFSRefを取得 #ifdef Try1 er = LaunchItem(&editTextRef); #endif #ifdef Try2 er = NavChooseFile(NULL, &reply, NULL, NULL, NULL, NULL, NULL, NULL); //ファイルを選択するダイアログボックスを表示する if(reply.validRecord) { //Cancelが押されていないなら er = AECountItems(&reply.selection, &itemsInList); //ファイル選択した個数 docArray = (FSRef*)NewPtr(sizeof(FSRef)*itemsInList); //各ファイルへのFSRefを納める領域を確保 for ( i=1 ; i<= itemsInList ; i++) { er = AEGetNthPtr(&reply.selection, i, typeFSRef, &keywd, &returnedType, &docArray[i-1], sizeof(FSRef), &actualSize); //ファイルの選択結果からFSRefとして取り出して、メモリに保持する } LaunchDocumentWithApp(docArray, itemsInList, &editTextRef); DisposePtr((Ptr)docArray); } #endif #ifdef Try3 OpenURL(CFSTR("http://cserver.locus.co.jp/mdo.html")); #endif LSTerm(); return 0; }
OSStatus LaunchItem(FSRef * app) { OSStatus er; er = LSOpenFSRef(app, NULL); //引数のファイルを開く return er; }
OSStatus LaunchDocumentWithApp(FSRef * doc, UInt32 nDocs, FSRef * app) { OSStatus er; LSLaunchFSRefSpec inLaunchSpec;
inLaunchSpec.appRef = app; //起動するアプリケーション inLaunchSpec.numDocs = nDocs; //開く文書の数 inLaunchSpec.itemRefs = doc; //文書ファイルのFSRefの配列へのポインタ inLaunchSpec.passThruParams = NULL; //追加のパラメータはななし inLaunchSpec.launchFlags = kLSLaunchDefaults; //起動の動作はデフォルト inLaunchSpec.asyncRefCon = NULL; //この引数も使わない er = LSOpenFromRefSpec(&inLaunchSpec, NULL); //文書をアプリケーションで開く return er; }
OSStatus OpenURL(CFStringRef urlStr) { OSStatus er; CFURLRef thisURL; thisURL = CFURLCreateWithString(kCFAllocatorDefault, urlStr, NULL); //CFStringからCFURLに変換 er = LSOpenCFURLRef(thisURL, NULL); //URLを開く return er; }
|