Does Illustrator support template literals?

New Here ,
Mar 31, 2021 Mar 31, 2021

Copy link to clipboard

Copied

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

  

TOPICS
Bug , Import and export , Scripting

Views

165

Likes

Translate

Translate

Report

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 ,
Mar 31, 2021 Mar 31, 2021

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

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 ,
Apr 02, 2021 Apr 02, 2021

Copy link to clipboard

Copied

i use const regularly. wish i could use let, though. =(

Likes

Translate

Translate

Report

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 ,
Aug 15, 2022 Aug 15, 2022

Copy link to clipboard

Copied

LATEST

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

Likes

Translate

Translate

Report

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 ,
Apr 02, 2021 Apr 02, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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