Question

I've got a problem with reading and writing files in mozilla xul. At first I want simply to read path to file(to check whether I/O works) So I wrote this code

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="window" title="title">
<script>
Components.utils.import("resource://gre/modules/FileUtils.jsm");

var file = FileUtils.getFile("Desk", ["temp.xml"]);
alert(file.path);
</script>
</window>

It should show alert window with path to temp.xml(this file exists on desktop). But it shows nothing in mozilla firefox. what's the problem?

Was it helpful?

Solution

My Firefox shows two problems:

  1. it chokes on alert() with error:

    Error: Cannot call openModalWindow on a hidden window = NS_ERROR_NOT_AVAILABLE Source file: resource://gre/components/nsPrompter.js Line: 382

  2. Your window has no style, so it's "invisible"

Here is a work around with displaying the path in a textbox instead and in browser console

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet href="chrome://global/skin/global.css"  type="text/css"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="window" title="title">
    <textbox id="text" value="N/A"/>
<script type="application/javascript">
Components.utils.import("resource://gre/modules/FileUtils.jsm");

var file = FileUtils.getFile("Desk", ["temp.xml"]);
document.getElementById("text").value = file.path;
console.log(file.path);
//alert(file.path);
</script>
</window>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top