Skip to main content
Participant
April 1, 2021
Question

Does Illustrator support template literals?

  • April 1, 2021
  • 2 replies
  • 641 views

Hi,

 

I have an illustrator file with all of my icons, and a script that generates all the png assets for me. I would like it to also generate Contents.json files for iOS integration. In order to do that, I would like to utilize a feature like template literals, but I can't get it to work. I have tried with both 'let' and 'const' but the values don't update in the template. Any help would be greatly appreciated.

 

function mkContents(txt)
{
    let name = 'back';
    let size = '24';

    var Path = app.activeDocument.path;
    var saveFile = File(Path + "/" + "Contents" + ".json");

    if(saveFile.exists)
        saveFile.remove();

    saveFile.encoding = "UTF8";
    saveFile.open('w');
    saveFile.writeln(txt);
    saveFile.close();
}

let template = '''{
  "images" : [
    {
      "filename" : "ic_${ name }-${ size }@1x.png",
      "idiom" : "universal",
      "scale" : "1x"
    },
    {
      "filename" : "ic_${ name }-${ size }@2x.png",
      "idiom" : "universal",
      "scale" : "2x"
    },
    {
      "filename" : "ic_${ name }-${ size }@3x.png",
      "idiom" : "universal",
      "scale" : "3x"
    }
  ],
  "info" : {
    "author" : "xcode",
    "version" : 1
  }
}''';

mkContents(template);

  

This topic has been closed for replies.

2 replies

CarlosCanto
Community Expert
Community Expert
April 2, 2021

I've also seen developers use TypeScript which supports modern stuff, then transpile to ES3.

m1b
Community Expert
Community Expert
April 1, 2021

Hi, so you are running this as an ExtendScript targeting Illustrator? If so, it needs to be ES3, so write it like it was 1995. Which means only use 'var' not 'let' or 'const' and no template literals, just old-fashioned string concatenation. At a quick glance, it looks like those two changes should work in your code.

Silly-V
Legend
August 15, 2022

With TS we can have the template literals!
https://github.com/Silly-V/Adobe-TS-ExtendScript/blob/master/SampleTSScript/src/1_functions.ts

 

/**
 * > An example of template interpolation.
 */
function test (inStr?: string): string {
	let resultStr;
	if (inStr) {
		resultStr = `Your string is: "${inStr}".`;
	} else {
		resultStr = "Hello World!";
	}
	return resultStr;
}

/**
 * > An example of destructuring an array.
 * 
 * > An example of an Arrow function.
 */
function makeShapes (doc: Document): string[] {
	const completedItems = [];
	let [x, y] = [0, 0];
	new Array(10).join(",").split("").forEach(() => {
		const newShape = doc.pathItems.rectangle(y, x, 50, 50);
		x += 10;
		y += 10;
		completedItems.push(newShape.uuid);
	});
	return completedItems;
}

function makeOtherShapes (doc: Document) {
	let [x, y] = [200, 0];
	// Make the shapes with a one-line statement.
	CircleNames.forEach(m => doc.pathItems.ellipse(y += 10, x += 10, 50, 50));
}

/**
 * > An example showing powerful typescript typing: a known-typed object can be used
 * to add several properties to another object expecting those known properties.
 */
function initializeDocument () {
	const doc = app.activeDocument;
	const defaultColorization: Partial<Document> = {
		defaultFilled : false,
		defaultStroked : true,
	};
	Object.assign(doc, defaultColorization);
}