0

I'm developing my own Thunderbird extension. The extension adds an .xml-file as an attachment to a Thunderbird mail (it works very well). My only problem is that I don’t know how to use a relative path.

It looks something like that:

var file= 'C:\\...[… \\…]...\\chrome\\VHitG2.xml';
var attachments = [];
    attachments.push(FileToAttachment(file));
    AddAttachments(attachments); 

If the extension is installed in a different path, the extension can’t work.
Doe’s anyone know how to use relative paths ?

MSKV
  • 38
  • 7

2 Answers2

0

The FileToAttachment() function doesn't do magic, it is actually very simple. I assume that you are talking about a static file that is part of your extension - it should be accessible under a URL like chrome://myextension/content/VHitG2.xml. Then you can simply create an nsIMsgAttachment instance yourself using that URL:

var attachment = Components.classes["@mozilla.org/messengercompose/attachment;1"]
                           .createInstance(Components.interfaces.nsIMsgAttachment);
attachment.url = "chrome://myextension/content/VHitG2.xml";
AddAttachments([attachment]);

Note that your extension doesn't need to be installed unpacked for that, you don't need an actual file on disk.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Thank you. I tried it with `attachment.url = "chrome://myextension@example.com/chrome/VHitG2.xml";`. The attachmentbucked is filled with a file called “chrome://myextension@example.com/chrome/VHitG2.xml" I’m not able to open this attachment or to send the mail with the attachment (lack of authorization). – MSKV Mar 06 '14 at 07:05
  • @MSKV: `chrome://` URLs don't work like that, please read https://developer.mozilla.org/en/docs/Chrome_Registration. – Wladimir Palant Mar 06 '14 at 07:52
  • @ Wladimir Palant: I added `content attach chrome://myextension/content/VHitG.xml` to my `chrome.manifest` and it works! My problem now is that i can’t change the content of the .xml file with `NetUtil` and `FileUtils` (worked before). – MSKV Mar 06 '14 at 10:47
  • I have no idea how to modify your code to get a `nsIFile` instead of an `nsiMsgAttachmet`. – MSKV Mar 12 '14 at 14:40
  • @MSKV: You cannot get an `nsIFile` instance for something that isn't a physical file (it's inside a compressed archive). However, if you want content that you can change then maybe using [`data:` URLs](http://en.wikipedia.org/wiki/Data_URI_scheme) will work. – Wladimir Palant Mar 12 '14 at 14:54
0

I used a very circuitous way to get the URL of the extension’s files:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
var test1 = FileUtils.getFile("CurProcD", ["VHitG2.xml"]);
var test2 = FileUtils.getFile("CurProcD", ["VHitG.xml"]);
var file1 = test1.path.replace(/VHitG2.xml/i, "extensions\\custom-toolbar-button@example.com\\chrome\\VHitG2.xml");
var file2 = test2.path.replace(/VHitG.xml/i, "extensions\\custom-toolbar-button@example.com\\chrome\\VHitG.xml");
var attachment1 = file1.replace(/\\/g, "\\\\");
var attachment2 = file2.replace(/\\/g, "\\\\");
MSKV
  • 38
  • 7