Skip to content

Instantly share code, notes, and snippets.

@mhulse
Last active June 11, 2016 03:43
Show Gist options
  • Save mhulse/ce3d8f623ce36b3988a59d30370769ba to your computer and use it in GitHub Desktop.
Save mhulse/ce3d8f623ce36b3988a59d30370769ba to your computer and use it in GitHub Desktop.
Illustrator convert images into strings for embedding in scripts (copied from ScriptUI for dummies: http://www.kahrel.plus.com/indesign/scriptui.html), page 122.
function graphic_to_text(infiles) { // Array of file objects.
var outfile,
s,
re1 = /^\(new String\(/,
re2 = /\)\)$/;
for (var i = 0; i < infiles.length; i++) {
if (infiles[i].exists) {
outfile = File (infiles[i].fullName.replace (/\.(png|idrc)$/, '.txt'));
outfile.open ('w');
infiles[i].encoding = 'BINARY';
infiles[i].open('r');
s = infiles[i].read();
outfile.write('var ' + outfile.name.replace ('.txt', '') + ' = ');
outfile.write(s.toSource().replace(re1, '').replace(re2, ''));
outfile.write(';');
infiles[i].close();
outfile.close();
}
}
}
graphic_to_text (Folder ("/d/test/").getFiles ("*.png"));
graphic_to_text ([File ("/d/test/iconA.png"), File ("/d/test/iconB.jpg")]);
graphic_to_text ([File ("/d/test/iconC.idrc")]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment