How to know if file already exists with Firefox iMacros - javascript

I'm using iMacros firefox addon to automatize redundant tasks like downloading bills (pdf files) from a website to my local disk.
What I want is, if bill has been already downloaded, to not ask for download next time. Is there a way to know if a file already exists locally ?
My macros are integrated in a js script.
Thanks for your help.

Here is a simple workaround for that:
var ret = iimPlayCode("SET !FOLDER_DATASOURCE D:\\iMacros\\Downloads\\blablabla.pdf");
if (ret == 1) {
// file exists
} else {
// file doesn't exist
}

var ret = iimPlayCode("SET !FOLDER_DATASOURCE D:\\iMacros\\Downloads\\blablabla.pdf");
if (ret == 1) {
// file exists
} else {
// file doesn't exist
}
This code no work for me.

You can make use of SET !DATASOURCE command.
var retCode = iimPlayCode("SET !DATASOURCE C:\\my<SP>folder\\my<SP>file.csv");
if (retCode == 1)
{
alert("WARNING! - File already exists.");
//action you want to take
}
Replace all backslash ('\') with '\' and spaces with < SP > (without spaces) in the file path.
You can use FILEDELETE command to delete exisiting file if you want.
Note: Return code 1 means file exists. Not equal to 1 does not mean that file does not exis, there are many return codes possible like -910. See documentation for more detail. However in most cases, you just need to handle file exist case.

Related

Is there a way to get the page count of a word doc?

Preferably I would like to do this in the browser with javascript. I am already able to unzip the doc file and read the xml files but can't seem to find a way to get a page count. I am hoping the property exist in the xml files I just need to find it.
edit: I wouldn't say it is a duplicate of Is there a way to count doc, docx, pdf pages with only js (without Node.js)? My question is specific to word doc/docx files and that question was never resolved.
Found a way to do this with docx4js
Here is a small sample parsing file from input elem
import docx4js from 'docx4js';
docx4js.load(file).then(doc => {
const propsAppRaw = doc.parts['docProps/app.xml']._data.getContent();
const propsApp = new TextDecoder('utf-8').decode(propsAppRaw);
const match = propsApp.match(/<Pages>(\d+)<\/Pages>/);
if (match && match[1]) {
const count = Number(match[1]);
console.log(count);
}
});
In theory, the following property can return that information from the Word Open XML file, using the Open XML SDK:
int pageCount = (int) document.ExtendedFilePropertiesPart.Properties.Pages.Text;
In practice, however, this isn't reliable. It might work, but then again, it might not - it all depends on 1) What Word managed to save in the file before it was closed and 2) what kind of editing may have been done on the closed file.
The only sure way to get a page number or a page count is to open a document in the Word application interface. Page count and number of pages is calculated dynamically, during editing, by Word. When a document is closed, this information is static and not necessarily what it will be when the document is open or printed.
See also https://github.com/OfficeDev/Open-XML-SDK/issues/22 for confirmation.
When you say "do this in the browser" I assume that you have a running webserver with LAMP or the equivalent. In PHP, there is a pretty useful option for .docx files. An example php function would be:
function number_pages_docx($filename)
{
$docx = new docxArchive();
if($docx->open($filename) === true)
{
if(($index = $docx->locateName('docProps/app.xml')) !== false)
{
$data = $docx->getFromIndex($index);
$docx->close();
$xml = new SimpleXMLElement($data);
return $xml->Pages;
}
$docx->close();
}
return false;
}

How do I pass parameters to an UltraEdit script from the command line?

Now an UltraEdit script is executed from the command line with:
uedit64.exe /s="J:\SkyDrive\work\ue-script\newFile.js"
Is it possible to pass parameters to UltraEdit script from command line? And how do I get them in the script?
Maybe like this:
uedit64.exe /s="J:\SkyDrive\work\ue-script\newFile.js" /pars="parameter1=value1,parameter2=value2"
And then get parameter1=value1 and parameter2=value2 in newFile.js.
UltraEdit scripts are executed usually from the command line to reformat one or more text files completely without user interaction and without depending on parameters. Or an UltraEdit script is started manually by a user from within UltraEdit without or with some minimal user interaction using getString and/or getValue. There are many scripting languages and script interpreters for doing something depending on parameters like VBScript, PowerShell, Perl, Python, etc.
It is not possible to specify additional custom parameters for an UltraEdit macro/script on the command line of UltraEdit. The command line arguments are interpreted by uedit64.exe or uedit32.exe and UltraEdit macros and scripts don't have access to arguments list of the executable.
I'm aware of three possibilities to pass strings (parameters) to an UltraEdit script from another process before starting UltraEdit and executing the script:
Via the clipboard, or
via a text file, or
by modifying the script before execution.
1. Pass parameters to an UltraEdit/UEStudio script via the clipboard
The first solution is easy to implement. But it has the big disadvantage that Windows clipboard content is modified on starting and no other process should copy something to the clipboard before the parameters and their values are read by the script. These disadvantages are very problematic if UltraEdit should be executed in the background for executing the script.
The following two commands must be executed on the command line or in a batch file:
echo parameter1=value1,parameter2=value2 | %SystemRoot%\System32\clip.exe
uedit64.exe /fni /s="J:\SkyDrive\work\ue-script\newFile.js"
clip.exe has been available since Windows Vista and Windows Server 2003. But there is no clip.exe on Windows XP. However, clip.exe from Windows Server 2003 can be also used on Windows XP.
It is highly recommended to use /fni (force new instance) on starting UltraEdit for executing a script from the command line when the configuration setting Allow multiple instances is not checked as by default. For an explanation why /fni should be used as the first parameter on the command line on running an UltraEdit macro/script from the command line, read topic Always get error message when running a macro/script via command line parameter (solved) in the UltraEdit forum.
An example script code for reading the parameters from clipboard is:
// Copy content of system (Windows/Mac/Linux) clipboard to a variable.
UltraEdit.selectClipboard(0);
var sParameterList = UltraEdit.clipboardContent;
// For safety check if the first parameter string begins with "parameter1".
if (sParameterList.indexOf("parameter1") == 0)
{
// Remove trailing withspaces from parameter list
var sParameterList = sParameterList.replace(/\s+$/,"");
// Split up the parameters list using comma as delimiter.
var asParameterList = sParameterList.split(',');
// For demonstration just open a message box listing the read
// parameters with their values without splitting them up further.
var sMessageText = "The parameter";
if (asParameterList.length > 1)
{
sMessageText += "s are:\n";
}
else
{
sMessageText += " is:\n";
}
for (var nParameter = 0; nParameter < asParameterList.length; nParameter++)
{
sMessageText += '\n "' + asParameterList[nParameter] + '"';
}
UltraEdit.messageBox(sMessageText,"List of parameters");
}
2. Pass parameters to UltraEdit/UEStudio script via a text file
In comparison to the first solution the clipboard is not modified by using this solution. However, it must be made sure that the following command lines are not executed at the same time by two processes.
> C:\Temp\ParameterList.tmp echo parameter1=value1,parameter2=value2
start "" /wait uedit64.exe /fni /s="J:\SkyDrive\work\ue-script\newFile.js"
del C:\Temp\ParameterList.tmp
The line output by command ECHO is redirected into text file C:\Temp\ParameterList.tmp, and then UltraEdit is started for running the script in a separate process and batch processing is halted until UltraEdit is exited. Finally, the temporary text file is deleted from the command line.
Example script code for reading the parameters from file with fixed name and path:
// Define the name of the file with the parameters with full path.
// The usage of environment variables in file name is not possible.
var sParameterListFile = "C:\\Temp\\ParameterList.tmp";
// Remember document index of active document which requires UltraEdit for
// Windows v16.00 or UEStudio v10.00. It would be possible to use code to
// get document index of active document on using an even older version of
// UltraEdit or UEStudio.
var nActiveDocIndex = UltraEdit.activeDocumentIdx;
// Open the parameter list file. This file should not be opened already
// before running this script. Otherwise additional code would be needed
// to search first in list of opened files for this file and read the
// parameters from already opened file and keep the file open instead
// of opening it and closing it after reading the first line.
UltraEdit.open(sParameterListFile);
// Test with a case-sensitive string comparison if the file could be really
// opened successfully in which case the parameter list file is the active
// file whose path property is the full name of the file with path.
if (UltraEdit.activeDocument.path == sParameterListFile)
{
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Read from the parameter list file just the first line without
// the line terminating character(s) and split up the parameters
// list using comma as delimiter before closing the file.
UltraEdit.activeDocument.startSelect();
UltraEdit.activeDocument.key("END");
UltraEdit.activeDocument.endSelect();
var asParameterList = UltraEdit.activeDocument.selection.split(',');
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
// For safety check if the first parameter string begins with "parameter1".
if (asParameterList[0].indexOf("parameter1") == 0)
{
// For demonstration just open a message box listing the read
// parameters with their values without splitting them up further.
var sMessageText = "The parameter";
if (asParameterList.length > 1)
{
sMessageText += "s are:\n";
}
else
{
sMessageText += " is:\n";
}
for (var nParameter = 0; nParameter < asParameterList.length; nParameter++)
{
sMessageText += '\n"' + asParameterList[nParameter] + '"';
}
UltraEdit.messageBox(sMessageText,"List of parameters");
}
// Make the previously active document again the active
// document if there was any document opened before at all.
if (nActiveDocIndex >= 0)
{
UltraEdit.document[nActiveDocIndex].setActive();
}
}
For usage of a dynamic file name, the file name must be specified as the second parameter to open this file by UltraEdit before running the script and the script reads the parameters from the first opened file.
> "%TEMP%\Any File Name.txt" echo parameter1=value1,parameter2=value2
start "" /wait uedit64.exe /fni "%TEMP%\Any File Name.txt" /s="J:\SkyDrive\work\ue-script\newFile.js"
del "%TEMP%\Any File Name.txt"
The script code for this solution could be something like:
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Move caret to top of first opened file which should
// be the file with the parameters for the script.
UltraEdit.document[0].top();
// Read from the parameter list file just the first line without the
// line terminating character(s) and split up the parameters list
// using comma as delimiter. The parameter list file remains opened.
UltraEdit.document[0].startSelect();
UltraEdit.document[0].key("END");
UltraEdit.document[0].endSelect();
var asParameterList = UltraEdit.document[0].selection.split(',');
// For safety check if the first parameter string begins with "parameter1".
if (asParameterList[0].indexOf("parameter1") == 0)
{
// For demonstration just open a message box listing the read
// parameters with their values without splitting them up further.
var sMessageText = "The parameter";
if (asParameterList.length > 1)
{
sMessageText += "s are:\n";
}
else
{
sMessageText += " is:\n";
}
for (var nParameter = 0; nParameter < asParameterList.length; nParameter++)
{
sMessageText += '\n"' + asParameterList[nParameter] + '"';
}
UltraEdit.messageBox(sMessageText,"List of parameters");
}
}
3. Modifying the script before execution
An UltraEdit script file is an ANSI text file and therefore it is possible to modify the script before execution.
echo var asParameterList = [ "value1", "value2" ];>"%TEMP%\ParameterList.tmp"
copy /B "%TEMP%\ParameterList.tmp" + "J:\SkyDrive\work\ue-script\newFile.js" "%TEMP%\TempScript.js" >nul
start "" /wait uedit64.exe /fni /s="%TEMP%\TempScript.js"
del "%TEMP%\ParameterList.tmp" "%TEMP%\TempScript.js"
The JavaScript code line which defines an initialized array of strings is first written into a temporary file. This temporary file is copied together with the script file to a new script in folder for temporary files. UltraEdit is executed using the temporary script file with the added parameter list array. Finally both temporary files are deleted from the command line.
An example script code which is extended dynamically with an additional line at the top to define the parameter strings:
// For demonstration just open a message box listing the parameter
// values as defined at top of the script from outside UltraEdit.
var sMessageText = "The parameter value";
if (asParameterList.length > 1)
{
sMessageText += "s are:\n";
}
else
{
sMessageText += " is:\n";
}
for (var nParameter = 0; nParameter < asParameterList.length; nParameter++)
{
sMessageText += '\n"' + asParameterList[nParameter] + '"';
}
UltraEdit.messageBox(sMessageText,"List of parameter values");
The advantage of this approach is that instead of an array of parameter strings it is also possible to define an array of integer numbers or floating point numbers. Or on the command line, multiple variables of different types are defined which are added to the temporary script file.
A last variant would be using // include parameters.js at top of the script file and the file parameters.js (with complete path or without path) is created dynamically on command line containing the lines to define the parameters in JavaScript language as JavaScript variables.

javascript prompt select a file (no upload, just get file name)

I'm mixing javascript and imacros to automate some tasks.
The main JS file will do some stuff and go to my website in firefox. Then, the script check if the word "confirmation" is found
Here's what i have so far
var macro = "CODE:";
(...)
var big_count=parseFloat(prompt("how many loops ?",100));
for(var x=0;x<big_count;x++)
{
iimSet("rnd",Math.floor(Math.random()*2000 + 1));
iimPlay(macro);
if(window.content.document.body.textContent.contains('confirmation'))
{
//if confirmation word found
iimPlay("CODE:WAIT SECONDS=60");
}
else
{
//if confirmation word isn't found
}
}
If the confirmation word is found, i want to launch another .iim script. I know i can do this using the following:
iimPlay("myFile.iim")
But the user running the .JS file needs to be able to chose which .IIM file will be run at the end of the script. How can i do this ?
I need some sort of prompt dialog that will list all files inside the following folder:
C:\Users\Libertad\Documents\iMacros\Macros
Then when a file is selected, pass the file name to the iimPlay() function

Set PDF page layout to "TwoPageLeft" using JavaScript (Acrobat Pro)

I would like to change (or add if it doesn't exist) to a PDF file with multiple pages the setting that will force the PDF to be opened in two page mode (PageLayout : TwoPageLeft for example).
I tried with that kind of JavaScript (given with Enfocus FullSwitch as example) :
if(($error == null) && ($doc != null))
{
try
{
$outfile = $outfolder + '/' + $filename + ".pdf";
$doc.layout = "TwoPageLeft";
$doc.saveAs( {cPath : $outfile, bCopy : true});
$outfiles.push($outfile);
}
catch(theError)
{
$error = theError;
$doc.closeDoc( {bNoSave : true} );
}
}
But it doesn't work as I would like (it will be opened with Acrobat Pro and saved as a new file without including the setting about the layout).
Does anyone can help me to correct that code to let JS open the PDF file, set the layout inside the PDF datas and save it out?
The readable information inside the PDF file should looks like this:
PageLayout/TwoPageLeft/Type/Catalog/ViewerPreferences
For information, I'm using FullSwitch (Enfocus) to handle files in a workflow, with Acrobat Pro, and at this time, it's only saving the file without adding the setting.
I can't find myself the answer over all the Web I searched recently, so I askā€¦
Thanks in advance!
I think you copied the "this.layout = ..." line out of the Acrobat JavaScript reference documentation, correct?
When you write a JavaScript for Switch to execute (or rather for Switch to instruct Acrobat to execute for you), you should use the "$doc" variable to refer to the document Switch is processing.
So try changing the line:
$this.layout = "TwoColumnLeft";
to
$doc.layout = "TwoColumnLeft";
As you say the rest of the code works and the document is saved without errors I assume the rest of your code is correct. The change proposed here will make the adjustment in the document you're looking for.

What are some methods to link to and run a local .exe file in a local browser

I am trying to use html/javascript to run a local .exe file in a local browser. The .exe file will generate asci text and I have it programed to encapsulate the text in html legible to the browser. But I want to have it load the new output from the .exe in the current browser, replacing whats there now.
There are two solutions I can think of.
1) In IE - Use WScript.Shell and do whatever you need in Windows.
In IE - Here is a sample to open notepad.
You place your executable there and then have it write it's file and then read the file.
<script>
function go() {
w = new ActiveXObject("WScript.Shell");
w.run('notepad.exe');
return true;
}
</script>
<form>
Run Notepad (Window with explorer only)
<input type="button" value="Go"
onClick="return go()">
</FORM>
Here is a sample for reading from a file
// define constants
// Note: if a file exists, using forWriting will set
// the contents of the file to zero before writing to
// it.
var forReading = 1, forWriting = 2, forAppending = 8;
// define array to store lines.
rline = new Array();
// Create the object
fs = new ActiveXObject("Scripting.FileSystemObject");
f = fs.GetFile("test.txt");
// Open the file
is = f.OpenAsTextStream( forReading, 0 );
// start and continue to read until we hit
// the end of the file.
var count = 0;
while( !is.AtEndOfStream ){
rline[count] = is.ReadLine();
count++;
}
// Close the stream
is.Close();
// Place the contents of the array into
// a variable.
var msg = "";
for(i = 0; i < rline.length; i++){
msg += rline[i] + "\n";
}
// Give the users something to talk about.
WScript.Echo( msg );
2) Create a signed Java Applet and talk to it through JavaScript
Maybe there is a way to talk to a Java Applet from the Javascript and then have the Applet do the work - It would probably need to be signed.
The short answer: you can't do it without writing a browser plug-in of some sort. Probably there's a simpler way of doing what you want.
Would it make it easier if, rather than running the browser and application separately, maybe you had an application you run which contains a webbrowser - then talking between them is easier because you can get access to the inner workings of the browser ... Just a thought.

Categories