[futurebasic] Find file in App's folder

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : February 2008 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Fri, 29 Feb 2008 03:38:02 -0500
Joe wrote:

> I am trying to adapt Brian's demo for finding a file in the documents
> folder to find a file in the program's folder. I am having trouble
> getting it to work. The demo below looks for a file called
> FindThisFile.txt first in the documents folder and then in the
> program's folder. The demo successfully only for the documents folder.
> It fails on the call to FSOpenIterator in the Fn IterateFolder
> function when using the fsref for the application's bundle. I suspect
> that the problem is that the function fn GetApplicationBundleFSRef
> returns an fsref for the application, not the folder where the
> application lives. If that is true how can I get the fsref for the
> applications folder?

Joe,

Here are a few bundle-related helper functions wrapped in a little  
demo. Copy this code into FB and save it as "Bundle." This works best  
if first compiled in FBtoC and then run. You can use your generic  
iterator to run through the FSRefs, or even better if you are trying  
to access files inside your application's internal Resources folder,  
simply follow the directions here:

http://tinyurl.com/yu5yld

Ken



/*

    Bundle Helper Functions

    For best results, compile
    in FBtoC and run to see
    how functions return FSRefs
    to various bundle-related
    directories in a well-formed
    bundle app.

    Ken Shmidheiser
    Feb. 29, 2008

*/

include "Tlbx MoreFilesX.incl"

begin enum
_kCFURLPOSIXPathStyle   = 0
_kCFURLHFSPathStyle     = 1
_kCFURLWindowsPathStyle = 2
end enum

#if ndef _DEFINEDINCARBON
#define CFURLPathStyle as long
#endif

toolbox fn CFBundleGetMainBundle = CFBundleRef
toolbox fn CFBundleCopyBundleURL ( CFBundleRef bundle ) = CFURLRef
toolbox fn CFURLCopyPath ( CFURLRef anURL ) = CFStringRef
toolbox fn CFURLCopyFileSystemPath ( CFURLRef anURL, CFURLPathStyle )  
= CFStringRef
toolbox fn CFURLGetFSRef ( CFURLRef url, FSRef *fsRef ) = Boolean
toolbox fn CFURLGetString ( CFURLRef anURL ) = CFStringRef
toolbox fn CFBundleCopyResourcesDirectoryURL ( CFBundleRef bundle ) =  
CFURLRef


'~Helper Function to Print File Paths

local fn FSRefPathAsString$( fsRef as ^FSRef ) as Str255
'~'1
dim as CFURLRef        url
dim as CFStringRef     cfStr
dim as Str255        path
dim as CFURLPathStyle  pathStyle
dim as Boolean       result

pathStyle = _kCFURLPOSIXPathStyle
cfStr = 0

url = fn CFURLCreateFromFSRef( 0, #fsRef )
long if ( url )
cfStr = fn CFURLCopyFileSystemPath ( url, pathStyle )
long if ( cfStr )
result = fn CFStringGetPascalString( cfStr, @path, sizeof( path ),  
_kCFStringEncodingMacRoman )
end if
end if

CFRelease( url )
CFRelease( cfStr )

end fn = path


'~Get FSRef to the bundle's .app directory

local fn GetBundleDirectory( bundleCFRef as CFBundleRef ) as FSRef
'~'1
dim as CFURLRef     bundleCFURL
dim as FSRef      @ bundleFSRef
dim as Boolean    result

bundleCFURL = fn CFBundleCopyBundleURL( bundleCFRef )
long if ( bundleCFURL )
result = fn CFURLGetFSRef( bundleCFURL, bundleFSRef )
end if
CFRelease( bundleCFURL )

end fn = bundleFSRef


'~Get FSRef to the directory in which the bundle app resides

local fn GetBundleParentDirectory( bundleCFRef as CFBundleRef ) as FSRef
'~'1
dim as CFURLRef    bundleCFURL
dim as FSRef     @ parentFSRef, @ bundleFSRef
dim as OSErr       err
dim as Boolean    result

bundleCFURL = fn CFBundleCopyBundleURL( bundleCFRef )
long if ( bundleCFURL )
result = fn CFURLGetFSRef( bundleCFURL, bundleFSRef )
long if ( result )
err = fn FSGetCatalogInfo( bundleFSRef, _kFSCatInfoNone, #0, #0, #0,  
@parentFSRef )
end if
end if
CFRelease( bundleCFURL )

end fn = parentFSRef


'~Get FSRef to the Resources folder inside the bundle app

local fn GetBundleResourcesDirectory( bundleCFRef as CFBundleRef ) as  
FSRef
'~'1
dim as CFURLRef    resourcesCFURL
dim as FSRef     @ resourcesFSRef
dim as Boolean   result

resourcesCFURL = fn CFBundleCopyResourcesDirectoryURL( bundleCFRef )
long if ( resourcesCFURL )
result = fn CFURLGetFSRef( resourcesCFURL, resourcesFSRef )
end if
CFRelease( resourcesCFURL )

end fn = resourcesFSRef


'~Test for Demo

dim as FSRef @ bundleRef, @ parentRef, @ resourcesRef

window 1
text _monaco, 12

// Get FSRefs...
bundleRef    = fn GetBundleDirectory( fn CFBundleGetMainBundle )
parentRef    = fn GetBundleParentDirectory( fn CFBundleGetMainBundle )
resourcesRef = fn GetBundleResourcesDirectory( fn  
CFBundleGetMainBundle )

// ...and do something with them
print:print "Path to bundle is:"
print fn FSRefPathAsString$( bundleRef )

print:print "Path to folder in which bundle app resides is:"
print fn FSRefPathAsString$( parentRef )

print:print "Path to bundle's internal Resources folder is:"
print fn FSRefPathAsString$( resourcesRef )

do
HandleEvents
until gFBQuit