[fpc-pascal] specifying the relative path to a file that is located in the same folder as the application bundle?

Ken G. Brown kbrown at mac.com
Fri Feb 6 16:17:43 CET 2009


Basically from point of sale system running on Macintosh, I needed to pick up a file with a known name located in the same folder as the application bundle, and process it for printing in a custom format. This way it does not need the file path hard coded in case it is different on the different computers. All that is required os for the Prin.app to be located in the same folder as the file. There are certainly other options.
Here is what I ended up with that is working, I'm open to suggestions for improvement:
----------------------
var
	indexOfBundleNameStart : SInt32 = 0;
	success : boolean = false;
	mainBundle : CFBundleRef;
	bundlePath : CFURLRef;
	bundlePathStr : CFStringRef;
	myBundlePathStr : pchar;
	pathToPrintApp : Str255 = '';
const
	printAppName : Str255 = 'Print.app';

begin
 
// Get the main bundle for the app
	mainBundle := CFBundleGetMainBundle();
	bundlePath := CFBundleCopyBundleURL(mainBundle);
	
	bundlePathStr := CFURLCopyFileSystemPath (bundlePath, kCFURLPOSIXPathStyle);
	
	myBundlePathStr := getmem(CFStringGetMaximumSizeOfFileSystemRepresentation(bundlePathStr));
	
	success := CFStringGetFileSystemRepresentation (bundlePathStr, myBundlePathStr,
											 CFStringGetMaximumSizeOfFileSystemRepresentation(bundlePathStr));
	
	if (success = true) then {get rid of 'Print.app' at the end of the path}
	   begin
	   pathToPrintApp := copy(myBundlePathStr, 1, Length(myBundlePathStr));
	
	   indexOfBundleNameStart := Pos(printAppName, pathToPrintApp);
	   delete(pathToPrintApp, indexOFBundleNameStart, Length(printAppName));
	 end;

//	Writeln(stdout, myBundlePathStr);
//	Writeln(stdout, 'Path to the app folder = ' + pathToPrintApp);
----------------------	
You would do something similar to pick up the paths to the other files of interest within the app bundle package using whatever other routines necessary as Jonas indicated.

Ken G. Brown


At 7:25 AM -0600 2/6/09, Travis Siegel apparently wrote:
>So, as someone who only uses terminal apps written in fpc ( since ibuilder isn't really that usable and writing interface generating code is still beyond me despit trying for 4 years) Could you please post a copy of a code segment that shows how you performed this little marvel of finding files inside the app bundle? I've been trying (unsuccessfully) to imitate compiled programs by running interpreters on files located inside an app bundle.  Thus far, I've had little success, but if you've gotten this working, then it's exactly what I was trying to do, and I sure could use your hard work. :-).
>
>
>On Feb 5, 2009, at 9:05 PM, Ken G. Brown wrote:
>
>>Thanks a bunch for all the help!
>>My legacy OS 9 app is now upgraded to OS X and working!
>>It put up a hell of a fight but with all your help, the obstacles have been overcome.
>>Awesome!
>>
>>Ken G. Brown
>>
>>At 9:51 PM +0100 2/5/09, Jonas Maebe apparently wrote:
>>><>
>>>You have to allocate memory for it. Something like
>>>
>>>myBundlePathStr:=getmem(CFStringGetMaximumSizeOfFileSystemRepresentation(bundlePathStr));
>>>success := CFStringGetFileSystemRepresentation (bundlePathStr, myBundlePathStr, CFStringGetMaximumSizeOfFileSystemRepresentation(bundlePathStr));
>>>
>>>When interfacing with C routines, you have to do the same things as if you were using C.
>>>
>>>>And I notice that CFStringGetFileSystemRepresentation is only available for 10.4 and newer. What do i need to do instead if wanting to deploy to 10.3.9?
>>>
>>>Call CFStringGetCStringPtr and/or CFStringGetCStringPtr and tell it to convert to utf-8 (kCFStringEncodingUTF8).
>>>
>>>
>>>Jonas
>>>_______________________________________________
>>>fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
>>>http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>>
>>_______________________________________________
>>fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
>>http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
>_______________________________________________
>fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
>http://lists.freepascal.org/mailman/listinfo/fpc-pascal




More information about the fpc-pascal mailing list