Latexdiff Script for Texstudio – How to Use and Configure

javascriptmacrostexstudio

I'm trying to use latexdiff script in the texstudio.
What is the problem with the following script.

%SCRIPT

var cf = app.getCurrentFileName();

var ld = new String(cf);
ld=ld.substring(0,ld.lastIndexOf("/"));

information("Select original file");
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec();
var fold=new String();
fold=fileChooser.fileName();
fold=fold.substring(fold.lastIndexOf("/")+1);

information("Select revised file");
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec();
var fnew=new String();
fnew=fileChooser.fileName();
fnew=fnew.substring(fnew.lastIndexOf("/")+1);

information("Select changes tracking file");
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec(fout);
var fout=new String();
fout=fileChooser.fileName();
fout=fout.substring(fout.lastIndexOf("/")+1);

var cmd=new String();
cmd="cd "+ld+" ; latexdiff-so "+fold+" "+fnew+" > "+fout+"\"";
dialog=new UniversalInputDialog();
dialog.add(cmd,"run command?");
var rpl=dialog.exec();
if(rpl!=null){
    system(cmd);
}

delete(cmd);
delete(dialog);
delete(fold);
delete(fnew);
delete(fout);
delete(ld);

When I'm trying to run this script I get this error:

"The specified stdout redirection is not supported: "> CHANGE.tex"". Please see the manual for details."

Best Answer

I struggled a lot dealing with output redirect, as it seems to have been broken since the previous answer. system() now seems to discard anything after and including the '>' sign, even when it is in quotation in the cmd string (TeXstudio 2.12.0 on Windows 10). My solution was to redirect to /dev/null (this is supported), which makes it possible to retrieve the output of latexdiff by the readAllStandardOutputStr() on the process object returned from system() and then write it to file and compile and show the result (guesswork with buildManager).

I hope my version of the TeXstudio latexdiff script can be of use for others. Does not use 'cmd', so it might work on non-windows as well.

%SCRIPT

// Execute latexdiff-so with current file as new file
// optional magic comments in .tex file as input parameters:
//   !TeX latexdiff:original = <original file>
//   !TeX latexdiff:output = <output file>
// Requires perl and latexdiff to be installed

var cf = app.getCurrentFileName();
var ld = new String(cf);
ld=ld.substring(0,ld.lastIndexOf("/")); //Local directory
fnew = cf;

var fold = editor.document().getMagicComment("latexdiff:original");
if(fold == "")
{
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec();
fold=fileChooser.fileName();
}

var fout = String(editor.document().getMagicComment("latexdiff:output"));
if(fout == "") // if path not given in magic comment
{
fileChooser.setDir(ld);
fileChooser.setFilter("TeX files (*.tex)");
fileChooser.exec();
fout=fileChooser.fileName();
}
else if(!((fout.indexOf(":") != -1) || (fout.indexOf("/")==0)))
fout = ld + '/' + fout; // if relative path, make absolute

var proc = system("latexdiff-so \"" + fold + "\" \"" + fnew + "\" > /dev/null", ld)
proc.waitForFinished();
writeFile(fout, proc.readAllStandardOutputStr());
//app.load(fout); // load diff file
buildManager.runCommand("txs:///quick", fout);
//app.load(fnew); // change back to original file

delete(proc);
delete(fold);
delete(fout);
delete(fnew);
delete(cf);
delete(ld);
Related Question