Skip to content

Instantly share code, notes, and snippets.

@uhunkler
uhunkler / clipboard-test.jstalk
Last active August 23, 2017 12:14
clipboard handling in JSTalk for Sketch
// @requires https://gist.github.com/uhunkler/5465857
var text = 'to clipboard ' + new Date(),
returnedtext = '';
clipboard.set( text );
returnedtext = clipboard.get();
test.assert( text === returnedtext, 'Text correctly copied and read to/from the clipboard' );
test.show();
@uhunkler
uhunkler / readTextFromFile.jstalk
Last active October 4, 2016 02:48
read and write TextToFile - JSTalk Sketch
/**
* Read text from a file at the given path
*
* @param {string} path given file path
* @return {mixed} string with text if successful, false if error
*/
function readTextFromFile( path )
{
var result = false;
@uhunkler
uhunkler / SketchLayers.jstalk
Last active September 12, 2016 02:59
SketchLayers - JSTalk Sketch
/**
* SketchLayers - a collection of layer related functions
*
* @type {object}
*/
var SketchLayers =
{
collection : [],
selection : function()
@uhunkler
uhunkler / browser.jstalk
Created April 27, 2013 16:45
browser communication object - JSTalk
// handle the browser communication
var browser =
{
name : "",
app : null,
win : null,
tab : null,
/**
* Initialise the browser object
@uhunkler
uhunkler / doJavaScript.jstalk
Last active December 16, 2015 17:50
doJavaScript - JSTalk browser
/**
* Execute JavaScript in the browser
*
* @param {string} js JavaScript string
* @required {object} browsername the browser name
* @required {object} browser the reference to the browser
* @required {object} tab the reference to the active tab
* @return {mixed} return value from JavaScript
*/
function doJavaScript( js )
@uhunkler
uhunkler / browserConnection.jstalk
Created April 27, 2013 15:07
browser connection - JSTalk
// create the connection to the browser
var browsername = "Safari",
// var browsername = "Google Chrome",
browser = SBApplication.application( browsername ),
win = browser.windows()[0],
tab = null;
// get the active tab
if( browsername === "Safari" || browsername === "WebKit" )
{
@uhunkler
uhunkler / simple-test-object.jstalk
Created April 26, 2013 09:00
test object - JSTalk Sketch
/**
* Test object for simple asserts
* test.assert( typeof 1 === 'number', '1 is a number' ); // add a test
* test.print( 'text' ); // add some text
* test.show(); // output all results
*/
var test = {
heap: [],
assert: function (value, desc) {
var result = value ? "--- pass: " : "xxx fail: ";
@uhunkler
uhunkler / sketch-header.jstalk
Created April 13, 2013 10:39
header - JSTalk Sketch
var doc = doc || null,
app = null,
extern = false;
if( doc === null )
{
extern = true;
app = JSTalk.application("Sketch");
// app.activate();
doc = app.orderedDocuments()[0];
var selection = doc.selectedLayers();
@uhunkler
uhunkler / sketch-loop-selection.jstalk
Created April 13, 2013 10:43
loop selection - JSTalk Sketch
for( var i = 0; i < selection.length(); i++ )
{
var layer = selection[ i ];
// do something with the selected layer
}
@uhunkler
uhunkler / sketch-create-table-01.jstalk
Created May 21, 2014 15:15
A basic table creation plugin.
/*global JSTalk:false, NSApplication:false, NSAlert:false, print:true */
// Set the basic variables for an externally run script
// which are set by default when the script is run
// as a plugin from within Sketch. "doc" is defined when the script
// is called from within Sketch.
if (doc === undefined) {
var doc, selection;
}
var sketch = null;