Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
3

Export SVG as a String

Participant ,
Nov 26, 2023 Nov 26, 2023

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!

TOPICS
Scripting
798
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Nov 26, 2023 Nov 26, 2023

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);

 

Translate
Adobe
Participant ,
Nov 26, 2023 Nov 26, 2023

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);

 

____________________
Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 26, 2023 Nov 26, 2023

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();

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 26, 2023 Nov 26, 2023
LATEST

No. Both ways lead flawlessly to Rome. 😉

____________________
Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 26, 2023 Nov 26, 2023

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();

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 26, 2023 Nov 26, 2023

That's a very good example, thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines