Skip to content

Instantly share code, notes, and snippets.

@iconifyit
Last active December 10, 2023 00:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iconifyit/3c0491e9b7963f5957ba5ecba9c93e42 to your computer and use it in GitHub Desktop.
Save iconifyit/3c0491e9b7963f5957ba5ecba9c93e42 to your computer and use it in GitHub Desktop.
Creates an Adobe Illustrator action to export to SVG on-the-fly.
/*
* This script creates an Adobe Illustrator action, on-the-fly, to export to SVG. The main thing to
* understand is that the `name` values in the action code are hexadecimal-encoded strings. The number
* that immediately preceeds the encoded name are the length of the hex string divided by 2.
*
* Usage:
*
* Change the `basePath` variable to match your file system. This can be set to any folder you like.
*
* Credits:
*
* Ascii-to-hex JS : https://www.w3resource.com/javascript-exercises/javascript-string-exercise-27.php
* Overall concept : https://github.com/Silly-V
*
* @see https://community.adobe.com/t5/illustrator/creating-a-dynamic-action-to-use-with-app-doscript-method/td-p/8918373
*/
try {
var actionScript = getActionScript();
var basePath = '/Users/username/Downloads/';
var ts = new Date().getTime();
var setName = 'Custom--' + ts,
setNameHex = asciiToHex(setName),
setNameLen = setNameHex.length / 2;
var actionName = 'TestAction--' + ts,
actionNameHex = asciiToHex(actionName),
actionNameLen = actionNameHex.length / 2;
var outputPath = basePath + actionName + '.svg',
outputPathHex = asciiToHex( outputPath ),
outputPathLen = outputPathHex.length / 2;
actionScript = actionScript.replace( '{actionName}', actionNameHex);
actionScript = actionScript.replace( '{actionNameLen}', actionNameLen );
actionScript = actionScript.replace( '{outputPath}', outputPathHex );
actionScript = actionScript.replace( '{outputPathLen}', outputPathLen );
actionScript = actionScript.replace( '{setName}', setNameHex );
actionScript = actionScript.replace( '{setNameLen}', setNameLen );
createAction( basePath, actionScript, setName );
app.doScript( actionName, setName );
app.unloadAction( setName, '' );
}
catch(e) {alert(e)}
// =======================================================================
// FUNCTIONS
// =======================================================================
function getActionScript() {
return "/version 3" +
"/name [ {setNameLen}" +
" {setName}" +
"]" +
"/isOpen 1" +
"/actionCount 1" +
"/action-1 {" +
" /name [ {actionNameLen}" +
" {actionName}" +
" ]" +
" /keyIndex 0" +
" /colorIndex 0" +
" /isOpen 0" +
" /eventCount 1" +
" /event-1 {" +
" /useRulersIn1stQuadrant 0" +
" /internalName (adobe_exportDocument)" +
" /localizedName [ 10" +
" 54657374416374696f6e" +
" ]" +
" /isOpen 1" +
" /isOn 1" +
" /hasDialog 1" +
" /showDialog 0" +
" /parameterCount 13" +
" /parameter-1 {" +
" /key 1349673843" +
" /showInPalette 4294967295" +
" /type (enumerated)" +
" /name [ 24" +
" 4149535647436f6f7264696e617465507265636973696f6e" +
" ]" +
" /value 3" +
" }" +
" /parameter-2 {" +
" /key 1400132720" +
" /showInPalette 4294967295" +
" /type (enumerated)" +
" /name [ 10" +
" 41495356475374796c65" +
" ]" +
" /value 1" +
" }" +
" /parameter-3 {" +
" /key 1380740963" +
" /showInPalette 4294967295" +
" /type (enumerated)" +
" /name [ 19" +
" 41495356475261737465724c6f636174696f6e" +
" ]" +
" /value 1" +
" }" +
" /parameter-4 {" +
" /key 1179929460" +
" /showInPalette 4294967295" +
" /type (enumerated)" +
" /name [ 13" +
" 4149535647466f6e7454797065" +
" ]" +
" /value 2" +
" }" +
" /parameter-5 {" +
" /key 1231311947" +
" /showInPalette 4294967295" +
" /type (enumerated)" +
" /name [ 11" +
" 4149535647496454797065" +
" ]" +
" /value 1" +
" }" +
" /parameter-6 {" +
" /key 1835951737" +
" /showInPalette 4294967295" +
" /type (enumerated)" +
" /name [ 11" +
" 41495356474d696e696679" +
" ]" +
" /value 1" +
" }" +
" /parameter-7 {" +
" /key 1919972214" +
" /showInPalette 4294967295" +
" /type (enumerated)" +
" /name [ 15" +
" 4149535647526573706f6e73697665" +
" ]" +
" /value 0" +
" }" +
" /parameter-8 {" +
" /key 1851878757" +
" /showInPalette 4294967295" +
" /type (ustring)" +
" /value [ {outputPathLen}" +
" {outputPath}" +
" ]" +
" }" +
" /parameter-9 {" +
" /key 1718775156" +
" /showInPalette 4294967295" +
" /type (ustring)" +
" /value [ 15" +
" 7376672066696c6520666f726d6174" +
" ]" +
" }" +
" /parameter-10 {" +
" /key 1702392942" +
" /showInPalette 4294967295" +
" /type (ustring)" +
" /value [ 3" +
" 737667" +
" ]" +
" }" +
" /parameter-11 {" +
" /key 1936548194" +
" /showInPalette 4294967295" +
" /type (boolean)" +
" /value 0" +
" }" +
" /parameter-12 {" +
" /key 1935764588" +
" /showInPalette 4294967295" +
" /type (boolean)" +
" /value 0" +
" }" +
" /parameter-13 {" +
" /key 1936875886" +
" /showInPalette 4294967295" +
" /type (ustring)" +
" /value [ 0" +
"" +
" ]" +
" }" +
" }" +
"}";
}
function asciiToHex(str) {
var arr1 = [];
for (var n = 0, l = str.length; n < l; n ++) {
var hex = Number(str.charCodeAt(n)).toString(16);
arr1.push(hex);
}
return arr1.join('');
}
function createAction(basePath, actionString, setName) {
try {
var actionsFolder = basePath + 'actions';
if (! new Folder(actionsFolder).exists) {
new Folder(actionsFolder).create();
}
var f = File(actionsFolder + '/' + setName + '.aia');
f.open('w');
f.write(actionString);
f.close();
app.loadAction(f);
}
catch(e) { alert('createAction : ' + e.message) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment