Re: Absolute path to the Application
Re: Absolute path to the Application
- Subject: Re: Absolute path to the Application
- From: Mark Dawson <email@hidden>
- Date: Fri, 27 Jul 2007 09:33:24 -0700
>Hello everybody,
>
>just started to find my way through the File Management Programming
>in Cocoa.
>
>can someone point me in the direction on how to find the absolute
>path to the Application (including the Application itself). My
>problem is that i need to use a file inside the Resources directory.
>and at the moment, i am assuming to much about the Application name etc.
I typically use a combination of Carbon and Cocoa (as sometimes I need to access particular Carbon APIs, so I store FSRefs AND CFURLs). The below code finds the Resources and Apps directories:
OSErr SetupOSLocations()
{
OSErr err = noErr;
// remember our locations
CFBundleRef myAppsBundle = CFBundleGetMainBundle();
if (myAppsBundle != NULL)
{
Boolean ok1 = false, ok2 = false;
gAppLoc->appLocMainBundleURL = CFBundleCopyBundleURL(myAppsBundle);
if (gAppLoc->appLocMainBundleURL != NULL)
{
ok1 = CFURLGetFSRef(gAppLoc->appLocMainBundleURL, &gAppLoc->appLocMainBundleRef); // convert main bundle to FSRef
// get the directory that the app is in
err = FSGetCatalogInfo(&gAppLoc->appLocMainBundleRef, kFSCatInfoNone, NULL, NULL, NULL, &gAppLoc->appLocMainBundleDirRef);
if (err == noErr)
{
err = GetFilePathFromFSRef(&gAppLoc->appLocMainBundleDirRef, gAppLoc->posixMainBundlePath, &gAppLoc->posixMainBundlePathLen);
gAppLoc->appLocMainBundleDirURL = CFURLCreateFromFSRef(NULL, &gAppLoc->appLocMainBundleDirRef); // convert main bundle to FSRef
}
gAppLoc->appLocResourceBundleURL = CFBundleCopyResourcesDirectoryURL(myAppsBundle); // find the Resources folder
if (gAppLoc->appLocResourceBundleURL)
ok2 = CFURLGetFSRef(gAppLoc->appLocResourceBundleURL, &gAppLoc->appLocResourceFSRef); // convert Resource bundle to FSRef
if (ok1 && ok2)
{
gAppLoc->isValid = true;
}
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden