How can i save Javascript text string to a TXT file?

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
wemersonrv
Posts: 35
Joined: Thu Oct 16, 2014 10:25 pm

How can i save Javascript text string to a TXT file?

Post by wemersonrv » Tue Jul 26, 2016 6:01 pm

Hello.

I am using iMacros for firefox with javascript engine. And need to save a couple of informations that i have in text format...

This information is not data extracted. This is a simple text generated by manipulating strings.

There are a way to save a text file with iMacros ?

I try to add the text content to !EXTRACT and then use SAVEAS TYPE=EXTRACT but, my text has special chars like TABs ( \t ) and LINEFEEDs ( \n ) then not works...

The code below saves content without error:

Code: Select all

var file_content="content";
var mm="CODE:";
    mm+="\nADD !EXTRACT "+file_content;
    mm+="\nSAVEAS TYPE=EXTRACT FOLDER=* FILE=file.txt";
iimPlay(mm);
but if i change the content for a text with spaces or any special char, not works. And even working, do not ovewrite file, just append a new line with content.


My environment: Windows 7 64 Bits
Browser: Mozilla Firefox 47.0
iMacros: 8.9.7
Wemerson Guimaraes
Rio Verde - GO, Brazil
Just an iMacros curious
iimfun
Posts: 239
Joined: Tue Jul 19, 2016 1:06 pm

Re: How can i save Javascript text string to a TXT file?

Post by iimfun » Wed Jul 27, 2016 11:22 am

If your text has special chars, just use the iimSet() function

Code: Select all

var file_content="content";
iimSet("file_content", file_content);
var mm="CODE:";
    mm+="\nADD !EXTRACT {{file_content}}";
    mm+="\nSAVEAS TYPE=EXTRACT FOLDER=* FILE=file.txt";
iimPlay(mm);
wemersonrv
Posts: 35
Joined: Thu Oct 16, 2014 10:25 pm

Re: How can i save Javascript text string to a TXT file?

Post by wemersonrv » Wed Jul 27, 2016 12:40 pm

iimfun wrote:If your text has special chars, just use the iimSet() function

Code: Select all

var file_content="content";
iimSet("file_content", file_content);
var mm="CODE:";
    mm+="\nADD !EXTRACT {{file_content}}";
    mm+="\nSAVEAS TYPE=EXTRACT FOLDER=* FILE=file.txt";
iimPlay(mm);
Thanks for your help. Works... but still has an issue... Does not respect special chars...

See my code updated to save the my log to a file.

Code: Select all

var log="Name: Wemerson\nCountry: Brazil";
iimSet("LOG", log);	
var mm="CODE:";
    mm+="\nADD !EXTRACT {{LOG}}";
    mm+="\nSAVEAS TYPE=EXTRACT FOLDER=* FILE=file.txt";
iimPlay(mm);
It Produces the result:
"Name: Wemerson\nCountry: Brazil"
But my expect result is:
Name: Wemerson
Country: Brazil
Wemerson Guimaraes
Rio Verde - GO, Brazil
Just an iMacros curious
serbeer
Posts: 44
Joined: Fri Sep 11, 2015 5:36 am

Re: How can i save Javascript text string to a TXT file?

Post by serbeer » Wed Jul 27, 2016 3:15 pm

Try your previous version without iimSet but use
<BR>
instead of
\n
in the string.

That alternative way per
http://wiki.imacros.net/Form_Filling#Variables

If that does not work, your only recourse is to write your log file from Javascript, thus using .js macro instead of .iim, using HTML5 features.
wemersonrv
Posts: 35
Joined: Thu Oct 16, 2014 10:25 pm

Re: How can i save Javascript text string to a TXT file?

Post by wemersonrv » Wed Jul 27, 2016 11:42 pm

serbeer wrote:Try your previous version without iimSet but use
<BR>
instead of
\n
in the string.

That alternative way per
http://wiki.imacros.net/Form_Filling#Variables

If that does not work, your only recourse is to write your log file from Javascript, thus using .js macro instead of .iim, using HTML5 features.
Not works too... then i make a change here and work 99%:

Code: Select all

var line1="Name: Wemerson\t\tCountry: Brazil" ;
var line2="Name: Joseph\t\tCountry: Australia";

iimSet("LINE1", line1);	
iimSet("LINE2", line2);	

var mm="CODE:";
    mm+="\nADD !EXTRACT {{LINE1}}";
    mm+="\nSAVEAS TYPE=EXTRACT FOLDER=* FILE=file.txt";
    mm+="\nADD !EXTRACT {{LINE2}}";
    mm+="\nSAVEAS TYPE=EXTRACT FOLDER=* FILE=file.txt";

iimPlay(mm);
The result:

Code: Select all

"Name: Wemerson		Country: Brazil"
"Name: Joseph		Country: Australia"
This way works for me... but if i run the script again, will append new lines at the end of file. But i need to replace the file with new content.
Wemerson Guimaraes
Rio Verde - GO, Brazil
Just an iMacros curious
serbeer
Posts: 44
Joined: Fri Sep 11, 2015 5:36 am

Re: How can i save Javascript text string to a TXT file?

Post by serbeer » Thu Jul 28, 2016 6:00 pm

wemersonrv
Posts: 35
Joined: Thu Oct 16, 2014 10:25 pm

Re: How can i save Javascript text string to a TXT file?

Post by wemersonrv » Thu Jul 28, 2016 8:04 pm

serbeer wrote:Use
http://wiki.imacros.net/FILEDELETE
in the beginning
Yes... thanks... now works fine!

Well.. i need to put the ERRORIGNORE because if file not exists, causes error... here the final code:

Code: Select all

	
var line1="Name: Wemerson\t\tCountry: Brazil" ;
var line2="Name: Joseph\t\tCountry: Australia";

iimSet("LINE1", line1);	
iimSet("LINE2", line2);	
var mm="CODE:";
    mm+="\nSET !ERRORIGNORE YES";
    mm+="\nFILEDELETE NAME=file.txt";
    mm+="\nSET !ERRORIGNORE NO";
    mm+="\nADD !EXTRACT {{LINE1}}";
    mm+="\nSAVEAS TYPE=EXTRACT FOLDER=* FILE=file.txt";
    mm+="\nADD !EXTRACT {{LINE2}}";
    mm+="\nSAVEAS TYPE=EXTRACT FOLDER=* FILE=file.txt";
iimPlay(mm);
Wemerson Guimaraes
Rio Verde - GO, Brazil
Just an iMacros curious
Post Reply