-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex2illustrator.js
91 lines (75 loc) · 3.68 KB
/
latex2illustrator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var pdflatexexe = "pdflatex.exe"; // Add full path if necessary
// determining the local temporary directory
var tempPath = Folder.temp.fsName; // path already in Windows syntax: c:\...
var i = tempPath.indexOf("Temporary Internet Files");
if (i >= 0) tempPath = tempPath.substr(0, i + 4);
//tempPath should now contain something like C:\Documents and Settings\<user>\Local Settings\Temp
function replaceAll(str, searchStr, replaceStr) {
return str.split(searchStr).join(replaceStr);
}
// prompt for user input
var thisFile = new File($.fileName);
var basePath = thisFile.path;
var basePathW = basePath;
basePathW = basePathW.charAt(1) + ":/" + basePathW.substring(3, basePathW.length);
basePathW = replaceAll(basePathW, "/", "\\");
var lastcodeFile = new File (basePath + "\\latex.tex");
lastcodeFile.open("r");
var latexcode = lastcodeFile.read();
lastcodeFile.close();
var panel = new Window ("dialog", "latex2illustrator", undefined, { resizeable:true });
var textDlg = panel.add('edittext', [25, 25, 500, 300], latexcode, { multiline: true });
var renderDlg = panel.add('button', undefined, "render");
var cancelDlg = panel.add('button', undefined, "cancel");
renderDlg.onClick = function () {
latexcode = textDlg.text;
panel.close();
if (latexcode != null) {
lastcodeFile.open("w");
lastcodeFile.write(latexcode);
lastcodeFile.close();
// add latex header etc. to create a complete latex document
var latexfile = new File(tempPath + '\\latex2illustrator.tex');
latexfile.open("w");
var headerfile = new File(basePath + "\\header.tex");
headerfile.open("r");
latexfile.writeln(headerfile.read());
headerfile.close();
latexfile.writeln(latexcode);
latexfile.writeln("\\end{document}");
latexfile.close();
var pdffile = File(basePath + "\\latex2illustrator.pdf");
if (pdffile.exists)
pdffile.remove();
// create a batch file calling latex
var batchfile = new File(basePath + '\\latex2illustrator.bat');
batchfile.open("w");
//batchfile.writeIn('del "' + basePath + '\\latex2illustrator.pdf"');
batchfile.writeln(pdflatexexe + ' -aux-directory="' + tempPath + '" -include-directory="' + tempPath + '" -output-directory="' + basePathW + '" "' + tempPath + '\\latex2illustrator.tex"');
batchfile.writeln('del "' + basePathW + '\\latex2illustrator.bat"');
batchfile.close();
batchfile.execute();
for (; batchfile.exists;)
// wait until the batch file has removed itself
var pdffile = File(basePathW + "\\latex2illustrator.pdf");
if (pdffile.exists) {
// import pdf file into the current document
var grp = app.activeDocument.activeLayer.groupItems.createFromFile(pdffile);
// The imported objects are grouped twice. Now move the subgroup
// items to the main group and skip the last item which is the page frame
for (var i = grp.pageItems[0].pageItems.length; --i >= 0;)
grp.pageItems[0].pageItems[i].move(grp, ElementPlacement.PLACEATEND);
var last = grp.pageItems.length - 1;
if (last >= 0 && grp.pageItems[last].typename == 'PathItem')
grp.pageItems[last].remove();
// Move the imported objects to the center of the current view.
grp.translate(app.activeDocument.activeView.centerPoint[0] - grp.left, app.activeDocument.activeView.centerPoint[1] - grp.top);
}
else
alert("File " + basePathW + "\\" + pdffile.name + " could not be created. LaTeX error?");
}
}
cancelDlg.onClick = function () {
panel.close();
}
panel.show();