Skip to content

Instantly share code, notes, and snippets.

@mhulse
Last active December 5, 2016 14:49
Show Gist options
  • Save mhulse/5692304983517c37cbc9 to your computer and use it in GitHub Desktop.
Save mhulse/5692304983517c37cbc9 to your computer and use it in GitHub Desktop.
Illustrator script that uses the command line to cURL a JSON file on remote server. This is just an example and experiment, not meant for production purposes. UNIX required. Not Windows-friendly, sorry. :(
{
"key": "value"
}
#target illustrator
#targetengine main
var NS = 'TEST';
this[NS] = (function(_$this, _$application, _$window, undefined) {
var _private = {};
_private.term = function($directory, $name) {
var script = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
'<plist version="1.0">',
'<dict>',
'<key>WindowSettings</key>',
'<array>',
'<dict>',
'<key>ExecutionString</key>',
'<string>TMP=' + $directory + '/' + $name + '.sh; chmod u+x $TMP; source $TMP;</string>',
'</dict>',
'</array>',
'</dict>',
'</plist>'
].join('\n');
return _private.file($directory + '/' + $name + '.term', script);
};
_private.shell = function($directory, $name, $result) {
var script = [
'#!/usr/bin/env bash\n',
'cd ' + $directory,
'curl https://dl.dropboxusercontent.com/u/1277106/data.json > ' + $result,
'exit;'
].join('\n');
return _private.file($directory + '/' + $name + '.sh', script);
};
_private.file = function($file, $string) {
var f = new File($file);
f.encoding = 'UTF-8';
f.lineFeed = 'Unix'; // Convert to UNIX lineFeed
// term.lineFeed = 'Windows';
// term.lineFeed = 'Macintosh';
f.open('w');
f.writeln($string);
f.close();
return f;
};
_private.directory = function($directory, $remove) {
if ( ! $directory.exists) {
$directory.create();
} else if ( !! $remove) {
_private.remove($directory);
$directory.remove();
}
};
_private.script = function($term) {
var result = $term.execute(); // Now execute the term file.
};
_private.read = function($directory, $result) {
var f;
var result = '';
f = new File($directory + '/' + $result);
if (f.open('r')) {
result = f.read();
f.close();
}
return result;
}
_private.parse = function($json) {
var data;
eval('data = ' + $json);
$.writeln(data.key); // Should return "value".
// Done!!!
}
_$this.init = function($title) {
var json;
var result;
var directory;
var json;
var name;
var term;
name = 'tmp'; // Name to use for auto-generated files.
result = 'result.json'; // File name of copy of remote data.
// Determine location of "temp" folder:
directory = new Folder(File($.fileName).path + '/' + name); // Where do these scripts live?
// Create our temporary directory:
_private.directory(directory);
term = _private.term(directory, name);
_private.shell(directory, name, result);
// This is just a hack to make sure the files above have been created before calling them ...
$.sleep(1000);
// ... so, this would be called after every BridgeTalk Message:
_private.script(term);
// Another hacky way of waiting for files to get created (doing this for demo purposes):
$.sleep(2000);
// Read the newly-created data file:
json = _private.read(directory, result);
// Parse the JSON:
_private.parse(json);
// Done!!!!!
};
return _$this;
}((this[NS] || {}), app, Window));
this[NS].init();
@mhulse
Copy link
Author

mhulse commented Jan 28, 2016

Long story short, this script creates .term and .sh files; the .term file sets permissions on the shell script and the shell script requests a json file hosted on my Dropbox public folder (see json file above). This script then writes the contents of the response to a file, reads that file and parses the json.

@mhulse
Copy link
Author

mhulse commented Jan 29, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment