factordog
factordog

Reputation: 597

Illustrator Script: Generate a JSON file

I have been working on creating a system where I can export my swatches from Illustrator as a JSON object in order to allow for my simplicity when trying to update my App that I have created.

Using the illustrator scripting API I have managed to loop through all my swatches and generate an object. What I am attempting to do now is take this data and generate a JSON file with it. This is so that whenever I make colour updates to my App in illustrator it will immediately change everything when I run that script.

I have been making use of Adobe Documentation as well as a helpful site with it simplified and easier to navigate Jongware.

The code overall looks like this: JSFiddle

The code in question is the following. I am not sure if there is way to generate the file without making use of the API. They seem to be using the same JS engine as a browser would but I am not 100% sure. Any advice would be great!

var file = new File('filename.txt');
file.saveAs('txt');

So the main question is how would I generate a new file locally that is able to store this object I have created? As the API isnt that clear on how to create a basic text file from the data I have created.

Upvotes: 1

Views: 2756

Answers (1)

factordog
factordog

Reputation: 597

Based off of the suggestion @enhzflep and this question.

I came to the final output of:

var file;
file = File.saveDialog('Export');
file.open('w');
file.write(JSON.stringify(colourObject));
file.close();

Making use of Douglas Crockfords JSON2 Pollyfill (As Illustrator scripts dont support .stringify) to create a stringify method I was able to create a JSON exported file.

Upvotes: 1

Related Questions