Greetings! I am currently working on a script to export JSON files from Adobe Illustrator.
// Custom function for exporting prefixed objects
function exportPrefixedObjects(doc) {
// User input for prefixes and reference points
var prefixInput = prompt("Enter the Prefixes separated by commas","btn_,dlg_");
var referencePointInput = prompt("Enter the reference point for each prefix separated by commas","center,top_left");
var prefixes = {}
var prefixArr = prefixInput.split(',')
var referenceArr = referencePointInput.split(',')
for(var i=0; i<prefixArr.length; i++){
prefixes[prefixArr[i]] = referenceArr[i]
}
const prefixedObjects = [];
for (var i = 0; i < doc.layers.length; i++) {
const layer = doc.layers[i];
const name = layer.name;
// Checking if the layer name starts with a prefix
for (var prefix in prefixes) {
if (name.startsWith(prefix)) {
// Obtaining the reference point
const referencePoint = prefixes[prefix];
// Getting the position of the layer
const pos = layer.position;
// Retrieving the width and height of the layer
const width = layer.width;
const height = layer.height;
// Creating an object with the layer's information
const obj = {
name: name,
x: pos[0],
y: pos[1],
referencePoint: referencePoint,
width: width,
height: height
};
prefixedObjects.push(obj);
break;
}
}
}
return prefixedObjects;
}
// Custom function to obtain artboard information
function getArtboardInfo(artboard) {
return {
name: artboard.name,
origin: {
x: artboard.rulerOrigin[0],
y: artboard.rulerOrigin[1]
}
};
}
// Accessing the active document and selected artboard
const doc = app.activeDocument;
const selectedArtboard = doc.artboards[doc.artboards.getActiveArtboardIndex()];
// Fetching the array of prefixed objects and their respective details
const prefixedObjects = exportPrefixedObjects(doc);
// Retrieving the artboard information
const artboardInfo = getArtboardInfo(selectedArtboard);
// Adding the artboard info to each prefixed object
prefixedObjects.forEach(obj => {
obj.artboardName = artboardInfo.name;
obj.artboardOrigin = artboardInfo.origin;
});
// Converting array of objects into a JSON string
const jsonString = JSON.stringify(prefixedObjects);
// Saving the JSON file
const file = new File(doc.path + '/prefixed-objects.json');
file.open('w');
file.write(jsonString);
file.close();
The issue at hand is that the prompt()
function is not appearing, resulting in an error. Upon investigation, it was discovered that
the prefixedObjects
array does not contain any values (which makes sense as there is no input provided).
I'm unsure how to proceed from this point.