How do I find out about files dropped on my app's icon?

Tuesday, September 02 2003 @ 03:44 PM BST

Contributed by: witness

Want to know how to detect when a user wants to open a file with your application, by double-clicking it in the Finder, or by dragging and dropping it on your application's Finder or dock icon? The solution is the "open document" Apple Event.

Whenever the Finder needs to notify your application of a user request for opening a document with your application, it sends you an { kCoreEventClass, kAEOpenDocuments } Apple Event. This Apple Event contains a list of files to open as its direct argument. To unpack them, use code like:


	FSRef				vFile;
	AEDescList			docList;
	long				x, numDocs;
	AEKeyword			vKeyword;
	DescType			vOutType;
	Size				vOutActualSize;
	OSErr				vErr;
	
	// Get list of files from Apple Event and count its items:
	vErr = AEGetParamDesc( sEvent, keyDirectObject, typeAEList, &docList );	
	vErr = AECountItems( &docList, &numDocs );
	
	// Go through list and open all  items:
	for( x = 1; x <= numDocs; x++ )
	{
		// Get an FSSpec to our file:
		vErr = AEGetNthPtr( &docList,
				x, 
				typeFSRef, 
				&vKeyword,
				&vOutType, 
				&vFile, 
				sizeof(FSRef), 
				&vOutActualSize );
		
		MyOpenOneFile( &vFile );
	}
	vErr = AEDisposeDesc( &docList );
Other useful Apple Events include the "open application" event, which is sent instead of an "open documents" event whenever your application is launched directly, without any documents, and the "reopen applications" event, which is sent whenever the user directly opens your application (which includes clicking its dock icon) while it is already running.

0 comments



http://www.macosxguru.net/article.php?story=2003090215443366