forked from alexander-ladygin/illustrator-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inlineSVGToAI.jsx
104 lines (86 loc) · 2.67 KB
/
inlineSVGToAI.jsx
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
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Program version: Adobe Illustrator CS5+
Name: inlineSVGToAI.jsx;
Author: Alexander Ladygin, email: [email protected]
Thanks for refactoring and testing - Sergey Osokin, email: [email protected]
Copyright (c) 2018
***
Instruction for use:
1. Run script
2. Paste your svg code in textarea
3. Press button "Paste"
*/
#target illustrator
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
function main() {
if (app.documents.length == 0) {
alert("Error: \nOpen a document and try again.");
return;
}
uiDialog().show();
}
// Create dialog window
function uiDialog() {
var win = new Window("dialog", "Inline SVG To AI \u00A9 www.ladygin.pro", undefined),
winPanel = win.add("panel");
winPanel.alignChildren = ['fill', 'fill'];
// Field for pasting the SVG code
var winSVGCodeTitle = winPanel.add("statictext", [0, 0, 200, 15], "Please paste your svg code:"),
SVGCode = winPanel.add("edittext", [0, 0, 200, 50], "", { multiline: true, scrolling: true }),
insertOpen = winPanel.add("checkbox", undefined, 'Insert through "Open" (without crash AI)');
insertOpen.value = true;
SVGCode.active = true; // Set state
// Buttons
var winButtonsGroup = win.add("group"),
closeButton = winButtonsGroup.add("button", [0, 0, 100, 30], "Cancel"),
pasteButton = winButtonsGroup.add("button", [0, 0, 100, 30], "Paste");
// Close window
closeButton.onClick = function () {
win.close();
};
// Paste button action
pasteButton.onClick = function () {
var code = SVGCode.text;
if (code) {
importSVG(code);
win.close();
} else {
alert("You didn't insert the SVG code.");
}
};
function importSVG(string) {
var svgFileName = "inlineSVGtoAI.svg",
svgFile = new File("" + Folder.temp + "/" + svgFileName),
backDoc = activeDocument;
svgFile.open("w");
svgFile.write(string);
svgFile.close();
if (!insertOpen.value && (activeDocument.importFile instanceof Function)) {
activeDocument.importFile(svgFile, false, false, false);
}
else {
app.open(svgFile);
var l = activeDocument.layers,
i = l.length;
while (i--) { l[i].hasSelectedArtwork = true; }
app.copy();
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
backDoc.activate();
app.paste();
}
$.sleep(500);
svgFile.remove();
}
return win;
}
function showError(err) {
if (confirm(scriptName + ": an unknown error has occurred.\n" +
"Would you like to see more information?", true, "Unknown Error")) {
alert(err + ": on line " + err.line, "Script Error", true);
}
}
try {
main();
} catch (e) {
showError(e);
}