Thunderbird extension: open EML file as draft - javascript

After my first question here, I am now looking for a way do the the same, but instead of opening an EML as a message, I want to open it as a draft.
Basically, I want to load a generated EML file into the compose window, so I can directly send it.
I already found some code, but I can't find the correct documentation on how to use it
var filePath = new FileUtils.File(getPath(params));
var uri = io.newFileURI(filePath);
var msgComposeService = Components.classes["#mozilla.org/messengercompose;1"].getService(Components.interfaces.nsIMsgComposeService);
var messenger = Components.classes["#mozilla.org/messenger;1"].createInstance(Components.interfaces.nsIMessenger);
var hdr = messenger.msgHdrFromURI(uri.spec);
var identity = getIdentityForHeader(hdr, Components.interfaces.nsIMsgCompType.Draft);
var msgWindow = Components.classes["#mozilla.org/messenger/msgwindow;1"].createInstance(Components.interfaces.nsIMsgWindow);
msgComposeService.OpenComposeWindow(null,null,uri,Components.interfaces.nsIMsgCompType.Draft,Components.interfaces.nsIMsgCompFormat.Default,identity,msgWindow);

I would suggest injecting the eml file into a local Drafts folder so as to get an nsIMsgDBHdr, and then calling the ComposeMessage function with Ci.nsIMsgCompType.Draft, Ci.nsIMsgCompFormat.Default, yourMsgHdr.folder, yourMsgHdr'sURI.
I think there are several StackOverflow answers on how to inject a given message into a folder.

Related

NEWBIE: Impossible to get my attached pdf in a mail with google script and form

I'm new to google script and I'm going crazy trying to do a simple tool.
I created a simple google form with just an email and a file uploader and I need just to insert an email and a pdf, and this should go to the recipient well HTML formatted and whit the pdf attached.
I'm new to this and I tried litterally every syntax using mailApp and Gmail app and the mail comes smooth but the pdf wont.
I know it's just a newbie stupid thing but I can't figure it out at my 74th attempt.
here's the code, I will be grateful forever with someone who can help me!
function sendPDF (e) {
var html = HtmlService.createTemplateFromFile("emailbolladireso.html");
var htmlText = html.evaluate().getContent();
var emailTo = e.response.getRespondentEmail();
var Subject = "---."
var textbody = "---."
var attach = e.response.getItemResponses();
var options = { htmlBody: htmlText};
if(emailTo !== undefined){
GmailApp.sendEmail(emailTo, Subject, textbody, options, )
attachments: attach[1];
}
}
First you have to get the id of your file wich is uploaded by your form to google drive.
You tried with:
var attach = e.response.getItemResponses();
But this will return you an array of objects with all your answers of your form.
From this object you have to extract the id of the uploaded PDF.
If you know the position, for example the first question in your form is for the pdf you can access it with attach[0] if it is e.g. in second position with attach[1], cause arrays index start with 0.
(You could look it also up with a for loop check for the name of the object.
attach[0].getItem().getTitle()
)
With attach[0] you get the next object and from this you get with attach[0].getResponse() finaly the id of your PDF.
attach[0].getResponse()
Now you "load" the file with the given id from google Drive (Make sure you have the permissions for access GoogleDrive)
var file = DriveApp.getFileById(attach[0].getResponse())
You can now attach your file (blob) to your attachments with the right MimeType
attachments: [file.getAs(MimeType.PDF)]
You should also place the attachments in the options of your email.
function sendPDF (e) {
var html = HtmlService.createTemplateFromFile("emailbolladireso.html");
var htmlText = html.evaluate().getContent();
var emailTo = e.response.getRespondentEmail();
var Subject = "Form with PDF"
var textbody = "Your PDF is attached."
var attach = e.response.getItemResponses();
var file = DriveApp.getFileById(attach[0].getResponse())
var options = { htmlBody: htmlText, attachments: [file.getAs(MimeType.PDF)]};
if(emailTo !== undefined){
GmailApp.sendEmail(emailTo, Subject, textbody, options);
}
}
Finaly it should work...
I tested it like this and it works for me.
DON'T PANIC!
Read also the docs -> https://developers.google.com/apps-script/reference/forms/item-response

How can I modify a javascript event in a PDF file programmatically?

My PDF file has an event attached to a button. I need to be able to modify that event programmatically. I tried this way using iTextSharp, but it didn't change the javascript in the new file:
var pdfReader = new PdfReader(originalPdfDocumentPath);
pdfReader.RemoveUsageRights();
var pdfStamper = new PdfStamper(pdfReader, new FileStream(
newPdfDocumentPath, FileMode.Create, FileAccess.Write, FileShare.None),
'\0', true);
var originalXml = pdfReader.AcroFields.Xfa.DomDocument.InnerXml;
var newXml = originalXml.Replace(
"Table2.Row1.instanceManager.removeInstance(1)",
"Table2._Row1.removeInstance(this.parent.parent.index)");
// Unfortunately, this line does nothing.
pdfStamper.AcroFields.Xfa.DomDocument.InnerXml = newXml;
pdfStamper.Close();
pdfReader.Close();
Any help would be greatly appreciated.
I found that it works if, instead of changing the XML directly, I change the DomDocument and mark the XFA as changed. Below is the corrected code:
var pdfReader = new PdfReader(originalPdfDocumentPath);
pdfReader.RemoveUsageRights();
var pdfStamper = new PdfStamper(pdfReader, new FileStream(newPdfDocumentPath, FileMode.Create, FileAccess.Write, FileShare.None), '\0', true);
var originalXml = pdfReader.AcroFields.Xfa.DomDocument.InnerXml;
var newXml = originalXml.Replace("Table2.Row1.instanceManager.removeInstance(1)", "Table2._Row1.removeInstance(this.parent.parent.index)");
/* New Code */
var doc = new XmlDocument();
doc.LoadXml(newXml);
pdfStamper.AcroFields.Xfa.DomDocument = doc;
pdfStamper.AcroFields.Xfa.Changed = true;
/* End of New Code */
pdfStamper.Close();
pdfReader.Close();
I should note that, even though this code changes the javascript in the PDF file, it also disables the extended features in Adobe Acrobat Reader. You can find more information regarding this here:
http://developers.itextpdf.com/question/why-do-i-get-error-saying-use-extended-features-no-longer-available
"The problem is related to whether or not your document is Reader Enabled. Reader-enabling can only be done using Adobe software. It is a process that requires a digital signature using a private key from Adobe. When a valid signature is present, specific functionality (as defined in the usage rights when signing) is unlocked in Adobe Reader.
You change the content of such a PDF, hence you break the signature."

Cannot write to file in Titanium

I am having trouble writing to a file in Titanium Studio.
specifically .json file. Code is compiled through and no exception was thrown.
Here is my relevant section of code, I parse the file to var first before adding element and stringify it to be written back.
Reading works perfectly, so is adding element, it's the writing process that has issues
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'data.json');
var jsontext = file.read().toString();
var jsondoc = JSON.parse(jsontext);
jsondoc['feedlist'].push({
"picloc":imagename,
"title":titlef.value,
"desc1":descf1.value,
"desc2":descf2.value,
"desc3":descf3.value
});
jsontext = JSON.stringify(jsondoc);
file.write(jsontext); // write(data,[append])
Note: I have consulted Documentation and done some of my own search, some are suggesting that "Filestream" should be used in place of normal file along with .close(), I have yet got them working but it could be pointers the solution, if anyone knows how to get it working
Thanks in advance.
EDIT: This question is flagged for duplication, initially I deemed that was 2 separate issues, one was about merely writing text to a file. Another is parsing event.media (picture) into a file.
I got it working now, The issue was that I was trying to write to file in read-only directory
Ti.Filesystem.resourcesDirectory: A read-only directory where your application resources are located
Ti.Filesystem.applicationDataDirectory: A read/write directory accessible by your app. Place your application-specific files in this directory.
The contents of this directory persist
until you remove the files or until the user uninstalls the application
Here is my code, directory is modified
var sesfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'data2.json');
var jsontext = sesfile.read().toString();
var jsondoc = JSON.parse(jsontext);
jsondoc['feedlist'].push({
"picloc":imagename,
"title":titlef.value,
"desc1":descf1.value,
"desc2":descf2.value,
"desc3":descf3.value
});
jsontext = JSON.stringify(jsondoc);
sesfile.write(jsontext,false);
If you are unable to locate data directory and simply want to load the file from there.
(In my case it does not exist in project nor will be created with Webpreview compilings)
You can do bootstrap-ish type instruction like this first
var rdfile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'data.json');
var sesfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'data2.json');
var jsontext = rdfile.read().toString();
var jsondoc = JSON.parse(jsontext);
sesfile.write(jsontext);
hope it helps whomever makes amateur mistake like I did.

Thunderbird extension - How to use relative paths with Javascript?

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 ?
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.
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, "\\\\");

How to upload image file into the SharePoint 2010 picture library using ECMA Script

I need to upload image file into the sharepoint 2010 picture library using java script...
requirement is --
1.we have File Upload control
2.And, we have to upload image file from that file upload control
Please see code...But this code is not working (showing "Undefined object" exception for 'File' or 'FileInfo')
If any body have better solution that would be nice.
Thanks in advance.
<script>
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Test');
//var fStream = (new FileInfo(uploadimagepath)).OpenRead();
var fStream = File.OpenRead(uploadimagepath);
//var fStream = FileUpload.PostedFile.InputStream;
//var contents = new byte[fStream.Length];
var newPic = oList.RootFolder.Files.Add(phototitle, fStream);
var oItem = newPic.Item;
oItem.set_item('Title', phototitle);
oItem.update();
oList.Rootfolder.Update();
clientContext.load(oItem);
</script>
you cannot use the uploadimagepath, it gives you the path from the client.
instead use the file stream which is sent from client to server.
check this url for understanding the file upload control better
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx

Categories