views:

1487

answers:

3

in short: is there any way to find the current directory full path of a xul application?

long explanation:

I would like to open some html files in a xul browser application. The path to the html files should be set programmatically from the xul application. The html files reside outside the folder of my xul application, but at the same level. (users will checkout both folders from SVN, no installation available for the xul app)

It opened the files just fine if I set a full path like "file:///c:\temp\processing-sample\index.html"

what i want to do is to open the file relative to my xul application.

I found i can open the user's profile path:

var DIR_SERVICE = new Components.Constructor("@mozilla.org/file/directory_service;1", "nsIProperties");
var path = (new DIR_SERVICE()).get("UChrm", Components.interfaces.nsIFile).path;
var appletPath;

// find directory separator type
if (path.search(/\\/) != -1)
{
appletPath = path + "\\myApp\\content\\applet.html"
}
else
{
appletPath = path + "/myApp/content/applet.html"
}

// Cargar el applet en el iframe
var appletFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
appletFile.initWithPath(appletPath);
var appletURL = Components.classes["@mozilla.org/network/protocol;1?name=file"].createInstance(Components.interfaces.nsIFileProtocolHandler).getURLSpecFromFile(appletFile);
var appletFrame = document.getElementById("appletFrame");
appletFrame.setAttribute("src", appletURL);

is there any way to find the current directory full path of a xul application?

+2  A: 

I found a workaround: http://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO i cannot exactly open a file using a relative path "../../index.html" but i can get the app directory and work with that.

var DIR_SERVICE = new Components.Constructor("@mozilla.org/file/directory_service;1", "nsIProperties");
var path = (new DIR_SERVICE()).get(resource:app, Components.interfaces.nsIFile).path;
var appletPath;
rec
A: 

In a xul application, you can access the chrome folder of the application using a chrome url.

I have relatively little experience with the element , so I'm not sure if this will work exactly for you. It is the way you include javascript source files in your xul file:

<script src="chrome://includes/content/XSLTemplate.js" type="application/x-javascript"/>

So, perhaps if the file resides at c:\applicationFolder\chrome\content\index.html , you can access it at:

chrome://content/index.html

in some fashion.

You may also look at jslib, a library that simplifies a lot of things, including file i/o. IIRC, I had trouble getting to a relative path using '../', though.

pc1oad1etter
+1  A: 

Yes, html files within your extension can be addressed using chrome URIs. An example from one of my extensions:

content.document.location.href = "chrome://{appname}/content/logManager/index.html"
bizzy