Copy link to clipboard
Copied
I'd like to manipulare the current document as a SVG. I could export it to the harddrive and then open/read it in from the .jsx to store it as a string. But maybe there's a shortcut to store it directly as a string without saving it to the harddrive first? Probably not, but you guys sometimes har creative solutions to virtually anything =).
Thanks!
Unfortunately, there is no way around passing a valid file path to the File() constructor.
This would be the quickest method that I'm aware of:
var svgContent = "";
var f = new File("/path/to/your.svg");
app.activeDocument.exportFile(f, ExportType.SVG, new ExportOptionsSVG());
f.open("r");
while (!f.eof) {
svgContent += f.readln();
}
f.close();
alert(svgContent);
Copy link to clipboard
Copied
Unfortunately, there is no way around passing a valid file path to the File() constructor.
This would be the quickest method that I'm aware of:
var svgContent = "";
var f = new File("/path/to/your.svg");
app.activeDocument.exportFile(f, ExportType.SVG, new ExportOptionsSVG());
f.open("r");
while (!f.eof) {
svgContent += f.readln();
}
f.close();
alert(svgContent);
Copy link to clipboard
Copied
Yes, that seem to be the best/shortest way of doing it, thanks.
Just curious, is there any upsides using the while loop to read lines rather than just:
svgContent = f.read();
Copy link to clipboard
Copied
No. Both ways lead flawlessly to Rome. 😉
Copy link to clipboard
Copied
Hi @iLLMonkey, just for some extra information, you can parse the SVG string into XML, which provides some conveniences. Here's a random example of setting the "font-family" attribute on every element:
var xml = new XML(svgContent),
elements = xml.descendants(),
len = elements.length();
for (var i = 0; i < len; i++) {
var found = elements[i]['@font-family'].toString();
if (found)
elements[i]['@font-family'] = 'Veneer';
}
- Mark
P.S. @GNDGN, you probably already know this, but you read a file's contents in one go like this:
// read the whole file contents
var svgContent = f.read();
Copy link to clipboard
Copied
That's a very good example, thanks!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now