嘿,我正在制作一个从Adobe Illustrator导出JSON文件的脚本
// Helper function to export prefixed objects
function exportPrefixedObjects(doc) {
// get prefixes and reference point as input from user
var prefixInput = prompt("Enter the Prefixes seperated by commas","btn_,dlg_");
var referencePointInput = prompt("Enter the reference point for each prefix seperated 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;
// Check if the layer name starts with a prefix
for (var prefix in prefixes) {
if (name.startsWith(prefix)) {
// Get the reference point
const referencePoint = prefixes[prefix];
// Get the position of the layer
const pos = layer.position;
// Get the width and height of the layer
const width = layer.width;
const height = layer.height;
// Create an object with the layer's info
const obj = {
name: name,
x: pos[0],
y: pos[1],
referencePoint: referencePoint,
width: width,
height: height
};
prefixedObjects.push(obj);
break;
}
}
}
return prefixedObjects;
}
// Helper function to get artboard info
function getArtboardInfo(artboard) {
return {
name: artboard.name,
origin: {
x: artboard.rulerOrigin[0],
y: artboard.rulerOrigin[1]
}
};
}
// Get the active document and the selected artboard
const doc = app.activeDocument;
const selectedArtboard = doc.artboards[doc.artboards.getActiveArtboardIndex()];
// Get the array of prefixed objects and their info
const prefixedObjects = exportPrefixedObjects(doc);
// Get the artboard info
const artboardInfo = getArtboardInfo(selectedArtboard);
// Append the artboard info to each prefixed object
prefixedObjects.forEach(obj => {
obj.artboardName = artboardInfo.name;
obj.artboardOrigin = artboardInfo.origin;
});
// Convert the array of objects to a JSON string
const jsonString = JSON.stringify(prefixedObjects);
// Save the JSON file
const file = new File(doc.path + '/prefixed-objects.json');
file.open('w');
file.write(jsonString);
file.close();
这里的问题是提示()函数没有显示,所以他给出了一个错误,当我检查这是因为
prefixedObjects
没有任何值(肯定是因为没有提示输入值)
我不知道该怎么办
Illustrator的Extendscript是过时的旧版本的Javascript。ES3或ES4代。没有forEach
、map
、filter
等数组方法。所以你可以改变行:
prefixedObjects.forEach(obj => {
obj.artboardName = artboardInfo.name;
obj.artboardOrigin = artboardInfo.origin;
});
和一个经典的循环。例如循环' For ':
for (var i=0; i<prefixedObjects.length; i++) {
var obj = prefixedObjects[i];
obj.artboardName = artboardInfo.name;
obj.artboardOrigin = artboardInfo.origin;
}