PDA

View Full Version : launch html file from web browser in carbon




printf
Nov 30, 2008, 04:32 AM
the title says it all. i'm trying to launch html-based help files in the same directory as my app.

i've tried both LSOpenCFURLRef, as well as LSOpenFSRef, but can only get them to work with www urls but NOT local files like "file:///Developer/help.html". all i get is an OSStatus of -43, which doesn't seem to be in the range of the documented Launch Services result codes.

anyone ever get this to work?



hhas
Nov 30, 2008, 05:38 AM
the title says it all. i'm trying to launch html-based help files in the same directory as my app.

Assuming these help files belong to your application, the simplest solution would be to use the standard Carbon/Cocoa help system:

http://developer.apple.com/documentation/Carbon/Conceptual/ProvidingUserAssitAppleHelp/index.html

printf
Nov 30, 2008, 06:17 AM
hhas, thanks for your response and that link.

i'd love to adopt that system, and probably will somehow in the future, but for the sake of time, and because this is the same system used in my win32 app, i need to just pop open the html files using the aforementioned technique.

any idea how?

hhas
Nov 30, 2008, 11:51 AM
i need to just pop open the html files using the aforementioned technique.

-43 = file not found. Probably your URL is wrong. Post the relevant code if you want specifics.

Sayer
Nov 30, 2008, 12:15 PM
You need to get a CFURL reference to the html file to open, most likely in the app bundle. So first you get a reference to the main app's bundle, which you can then use to get the embedded HTML file.

so do:

CFBundle bundle = CFGetMainBundle();
CFURL = CFBundleCopyResourceURL( bundle, "Help", "html", "HelpContentFolder" );

So "Help" is the file name minus extension, "html" is the file type extension (hurray for Unix/Windows!) and "HelpContentFolder" is the name of the folder in the Resources folder inside the app bundle that contains the Help files.

App Bundle > Contents > Resources > HelpContentFolder

Then its just a matter of: Pass that CFURL to LSOpenCFURLRef.

printf
Nov 30, 2008, 04:16 PM
-43 = file not found. Probably your URL is wrong. Post the relevant code if you want specifics.

here's both my attempts at this. i hard-coded the path in place of CFBundleCopyBundleURL(CFBundleGetMainBundle()), where my help files are stored just to eliminate any further points of failure. and i can assure you, that html file is in fact at that location.


//attempt 1
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("file:///Developer/Projects/help.html"), kCFURLPOSIXPathStyle, false);
OSStatus result = LSOpenCFURLRef(url,NULL);

//attempt 2
FSRef ref = {0};
Boolean isDir = false;
OSStatus result = FSPathMakeRef((UInt8 *)"file:///Developer/Projects/help.html", &ref, &isDir);
LSOpenFSRef(&ref,NULL);


and sayer, if i'm understanding you correctly that would require the help files to be 'inside' the app. i'd like the help to be external from the app so updates don't require a full download of the main app, and so the user can get to help without the app being loaded.

kainjow
Nov 30, 2008, 04:33 PM
In your examples you are passing the absolute string of a file URL instead of the absolute path.

printf
Nov 30, 2008, 04:38 PM
In your examples you are passing the absolute string of a file URL instead of the absolute path.

haha kainjow, i had JUST figured that out and came back to post my result!!! haha, thanks! (and i think you meant relative in the second part)

note to self: read EVERY line of the api documentation!

printf
Nov 30, 2008, 04:44 PM
for posterity:


CFURLRef fURL = CFURLCreateWithFileSystemPath(NULL, CFSTR("Help/help.html"), kCFURLPOSIXPathStyle, false); //Help resides in folder relative to app
LSOpenCFURLRef(fURL, NULL);

kainjow
Nov 30, 2008, 04:49 PM
Hm I could be wrong, but I don't think that code will work all the time. Did you test it by running your app outside of Xcode (e.g. via the Finder)? Xcode sets the current working directory usually to the Debug/Release directory, but the Finder doesn't.

printf
Nov 30, 2008, 04:57 PM
Hm I could be wrong, but I don't think that code will work all the time. Did you test it by running your app outside of Xcode (e.g. via the Finder)? Xcode sets the current working directory usually to the Debug/Release directory, but the Finder doesn't.

awww crap, you're right - it doesn't work when launched from finder! how do i get around that then?

kainjow
Nov 30, 2008, 05:15 PM
Well if the Help folder is in the same folder as your .app you can use CFBundleCopyBundleURL() and manipulate it from there (as a URL, or convert to C-string, etc). But since we're dealing with a Mac app, the Help folder *should* be located inside the .app's Resources folder, and if that's the case you can use Sayer's recommendation of CFBundleCopyResourceURL.

printf
Nov 30, 2008, 06:11 PM
kainjow, does this theory apply to dylib files as well? i just noticed dlopen is failing if i pass in a relative path and run my app from finder - but it works when i run it in xcode..

if so, that would seem contradictory to the point of a 'dynamically' linked lib, methinks

printf
Nov 30, 2008, 07:24 PM
i was able to fix the dlopen issue using CFBundleCopyBundleURL(CFBundleGetMainBundle()), however my question still stands.

is the mac standard to package dylib's with the .app's bundle? if this is the case, i would greatly appreciate a link to documentation on this, so i can follow these best practices!

kainjow
Nov 30, 2008, 07:27 PM
Yes, you can link to a dylib inside your application bundle. You have to copy it to a certain folder (probably Contents/Frameworks), and then write a run script to use install_name_tool to set the path to relative based on the directory. I used to do this with a project but I don't have the script anymore.