Re: real noob question about CF functions with Cocoa
Re: real noob question about CF functions with Cocoa
- Subject: Re: real noob question about CF functions with Cocoa
- From: "Finlay Dobbie" <email@hidden>
- Date: Thu, 15 Jun 2006 12:53:08 +0100
On 15/06/06, Ron Fleckner <email@hidden> wrote:
Hi,
I hope this doesn't annoy anyone, but I'd really like to know.
Please stop reading now if you don't like dumb questions.
I wanted the user to be able to choose an item from a list and send
it to the Trash. I found a great little example function in the docs
at: Reference Library > Guides > Cocoa > File Management > Low-Level
File Management Programming Topics > Locating Directories on the
System. It's at the bottom of that page. I use it and it works,
though I couldn't tell you how or why. As the docs state just above
the example which I shamelessly copied and pasted into my own code,
That function does not actually have correct functionality in all
cases. If the file resides outside of the user's home directory, it
should not go to their user trash but instead to the correct trash
folder in the root of the volume (e.g.
/Volumes/MyRemovableVolume/.Trashes/501 rather than /Users/Me/.Trash).
The only reliable way to move files to the trash in all cases, AFAIK,
is to send an AppleEvent to the Finder asking it to do it for you.
Here is some sample code which does that:
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h> // for FinderRegistry.h - don't need to
link against the framework, though.
OSType gFinderSignature = 'MACS';
OSStatus MoveFileToTrash(CFURLRef fileURL) {
AppleEvent event, reply;
OSStatus err;
FSRef fileRef;
AliasHandle fileAlias;
if (CFURLGetFSRef(fileURL, &fileRef) == false)
return coreFoundationUnknownErr;
err = FSNewAliasMinimal(&fileRef, &fileAlias);
if (err == noErr) {
err = AEBuildAppleEvent(kAECoreSuite, kAEDelete,
typeApplSignature,
&gFinderSignature, sizeof(OSType),
kAutoGenerateReturnID,
kAnyTransactionID, &event,
NULL,"'----':alis(@@)", fileAlias);
if (err == noErr) {
err = AESendMessage(&event, &reply,
kAEWaitReply, kAEDefaultTimeout);
if (err == noErr)
AEDisposeDesc(&reply);
AEDisposeDesc(&event);
}
DisposeHandle((Handle)fileAlias);
}
return err;
}
So, what IS Core Foundation, really? Do people write software using
only Core Foundation? What relation does it have to Cocoa?
CoreFoundation is a C API, and is more lightweight than Foundation. It
is also available to Carbon applications, going back to Mac OS 8 or
so. Many of the types in Foundation are implemented using
CoreFoundation, and you can just cast between them (see the conceptual
doc on "toll-free bridging").
Occasionally CF provides more flexibility than Foundation does.
Some of CF is open-source and has been ported to other operating
systems, such as Linux.
-- Finlay
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden