Last active
September 12, 2016 02:59
-
-
Save uhunkler/5481898 to your computer and use it in GitHub Desktop.
SketchLayers - JSTalk Sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get a layer by name | |
* | |
* @param {object} startLayer the layergroup to start the search | |
* @param {string} name the name of the searched layer | |
* @param {bool} sublayers include subleayers | |
* @required {object} SketchLayers the SketchLayers object with the getLayers function | |
* @return {mixed} null or a MSLayer|Group | |
*/ | |
function getLayerByName( startLayer, name, sublayers ) | |
{ | |
sublayers = sublayers || false; | |
// declare the filter/search itarator | |
var iterator = function( layer ) | |
{ | |
return layer.name().toString() === name; | |
}; | |
// call the filter/search function | |
var foundlayer = SketchLayers.getLayers( startLayer, iterator, sublayers ); | |
// return only the found layer or null | |
if( foundlayer ) | |
return foundlayer; | |
return null; | |
} | |
// get the layer with the name "thelayername" | |
var page = doc.currentPage(); | |
var layer = getLayerByName( page, "thelayername" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get the layers with the search string in their name | |
* | |
* @param {object} startLayer the layergroup to start the search | |
* @param {string} inname the string as part of the name of the searched layer | |
* @param {bool} sublayers include subleayers | |
* @required {object} SketchLayers the SketchLayers object with the getLayers function | |
* @return {array} found layers or empty array | |
*/ | |
function getLayersByNameContaining( startLayer, inname, sublayers ) | |
{ | |
sublayers = sublayers || false; | |
// declare the filter/search itarator | |
var iterator = function( layer ) | |
{ | |
return ( layer.name().toString().indexOf( inname ) !== -1 ); | |
}; | |
// call the filter/search function | |
var foundlayers = SketchLayers.getLayers( startLayer, iterator, sublayers, true ); | |
return foundlayers; | |
} | |
// get all layers with "h1" in the layer name, exclude sublayers | |
var page = doc.currentPage(); | |
var layers = getLayersByNameContaining( page, "h1" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Select all layers | |
* | |
* @param {object} startLayer the layergroup to start the search | |
* @param {bool} sublayers include subleayers | |
* @required {object} SketchLayers the SketchLayers object with the getLayers function | |
* @return {array} found layers or empty array | |
*/ | |
function selectAllLayers( startLayer, sublayers ) | |
{ | |
sublayers = sublayers || false; | |
// declare the filter/search itarator | |
var iterator = function( layer ) | |
{ | |
return true; | |
}; | |
// call the filter/search function | |
var foundlayers = SketchLayers.getLayers( startLayer, iterator, sublayers, true ); | |
// do something with the layers | |
if( foundlayers.length ) | |
{ | |
SketchLayers.selectLayers( foundlayers ); | |
} | |
return foundlayers; | |
} | |
// select all layers, include sublayers | |
var page = doc.currentPage(); | |
var layers = selectAllLayers( page, true ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Select the text layers with a given font size | |
* | |
* @param {object} startLayer the layergroup to start the search | |
* @param {number} fontsize the fontsize | |
* @param {bool} sublayers include subleayers | |
* @required {object} SketchLayers the SketchLayers object with the getLayers function | |
* @return {array} found layers or empty array | |
*/ | |
function selectLayersByFontSize( startLayer, fontsize, sublayers ) | |
{ | |
sublayers = sublayers || false; | |
// declare the filter/search itarator | |
var iterator = function( layer ) | |
{ | |
return ( ( layer.className().toString() === 'MSTextLayer' ) && ( layer.fontSize() === fontsize ) ); | |
}; | |
// call the filter/search function | |
var foundlayers = SketchLayers.getLayers( startLayer, iterator, sublayers, true ); | |
// do something with the layers | |
if( foundlayers.length ) | |
{ | |
SketchLayers.selectLayers( foundlayers ); | |
} | |
return foundlayers; | |
} | |
// get all layers with 18 px font size, include sublayers | |
var page = doc.currentPage(); | |
var layers = selectLayersByFontSize( page, 18, true ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Select the layers where the regex matches the layer name | |
* | |
* @param {object} startLayer the layergroup to start the search | |
* @param {regex} theRegex the given regex | |
* @param {bool} sublayers include subleayers | |
* @required {object} SketchLayers the SketchLayers object with the getLayers function | |
* @return {array} found layers or empty array | |
*/ | |
function selectLayersByNameContainingRegex( startLayer, theRegex, sublayers ) | |
{ | |
sublayers = sublayers || false; | |
// declare the filter/search itarator | |
var iterator = function( layer ) | |
{ | |
return ( layer.name().toString().match( theRegex ) ); | |
}; | |
// call the filter/search function | |
var foundlayers = SketchLayers.getLayers( startLayer, iterator, sublayers, true ); | |
// do something with the layers | |
if( foundlayers.length ) | |
{ | |
SketchLayers.selectLayers( foundlayers ); | |
} | |
return foundlayers; | |
} | |
// find all layers with "rect" (case independent) in the layer name, include sublayers | |
var page = doc.currentPage(); | |
var pattern = /rect/gi; | |
var layers = selectLayersByNameContainingRegex( page, pattern, true ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Select the layers with a given tag in the name | |
* | |
* @param {object} startLayer the layergroup to start the search | |
* @param {string} tag the tag in the layer name: [tag] | |
* @param {bool} sublayers include subleayers | |
* @required {object} SketchLayers the SketchLayers object with the getLayers function | |
* @return {array} found layers or empty array | |
*/ | |
function selectLayersByTag( startLayer, tag, sublayers ) | |
{ | |
sublayers = sublayers || false; | |
// declare the filter/search itarator | |
var iterator = function( layer ) | |
{ | |
return ( layer.name().toString().indexOf( '[' + tag + ']' ) !== -1 ); | |
}; | |
// call the filter/search function | |
var foundlayers = SketchLayers.getLayers( startLayer, iterator, sublayers, true ); | |
// do something with the layers | |
if( foundlayers.length ) | |
{ | |
SketchLayers.selectLayers( foundlayers ); | |
} | |
return foundlayers; | |
} | |
// layers are tagged with tags like [one] [two] etc. | |
// get all layers with the tag [two] in the layer name, include sublayers | |
var page = doc.currentPage(); | |
var layers = selectLayersByTag( page, 'two', true ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Select all text layers | |
* | |
* @param {object} startLayer the layergroup to start the search | |
* @param {bool} sublayers include subleayers | |
* @required {object} SketchLayers the SketchLayers object with the getLayers function | |
* @return {array} found layers or empty array | |
*/ | |
function selectTextLayers( startLayer, sublayers ) | |
{ | |
sublayers = sublayers || false; | |
// declare the filter/search itarator | |
var iterator = function( layer ) | |
{ | |
return ( layer.className().toString() === 'MSTextLayer' ); | |
}; | |
// call the filter/search function | |
var foundlayers = SketchLayers.getLayers( startLayer, iterator, sublayers, true ); | |
// do something with the layers | |
if( foundlayers.length ) | |
{ | |
SketchLayers.selectLayers( foundlayers ); | |
} | |
return foundlayers; | |
} | |
// select all text layers, include sublayers | |
var page = doc.currentPage(); | |
var layers = selectTextLayers( page, true ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* SketchLayers - a collection of layer related functions | |
* | |
* @type {object} | |
*/ | |
var SketchLayers = | |
{ | |
collection : [], | |
selection : function() | |
{ | |
return doc.selectedLayers(); | |
}, | |
/** | |
* Select one layer | |
* | |
* @param {object} layer The given layer | |
* @param {bool} state true: select, false deselect | |
*/ | |
selectLayer : function( layer, state ) | |
{ | |
if( typeof state === 'undefined' ) | |
state = true; | |
layer.setIsSelected( state ); | |
}, | |
/** | |
* Select the given layers | |
* | |
* @param {array} layers Array with layers | |
* @param {bool} state true: select, false deselect | |
*/ | |
selectLayers : function( layers, state ) | |
{ | |
if( typeof state === 'undefined' ) | |
state = true; | |
for( var _l in layers ) | |
{ | |
if( layers.hasOwnProperty( _l ) ) | |
{ | |
this.selectLayer( layers[ _l ], state ); | |
} | |
} | |
}, | |
/** | |
* getLayers is the main function to find/filter layers | |
* | |
* @param {object} startLayer the layer group to start the search | |
* @param {function} iterator the search function | |
* @param {bool} sublayers include subleayers | |
* @param {bool} getAll false: find first, true: find all | |
* @param {object} context the iterater call context | |
* @return {mixed} if getAll {array}, else found layer or null | |
*/ | |
getLayers : function( startLayer, iterator, sublayers, getAll, context ) | |
{ | |
sublayers = sublayers || false; | |
getAll = getAll || false; | |
context = context || null; | |
this.collection = []; | |
this._getLayers( startLayer, iterator, sublayers, getAll, context ); | |
if( getAll ) | |
{ | |
return this.collection; | |
} | |
else | |
{ | |
if( this.collection.length ) | |
{ | |
return this.collection[ 0 ]; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
}, | |
/** | |
* Internal recursive function to walk the layers | |
* | |
* @param {object} startLayer the layer group to start the search | |
* @param {function} iterator the search function | |
* @param {bool} sublayers include subleayers | |
* @param {bool} getAll false: find first, true: find all | |
* @param {object} context the iterater call context | |
* @return {bool} found: true, else false | |
*/ | |
_getLayers : function( startLayer, iterator, sublayers, getAll, context ) | |
{ | |
var _layers = startLayer.layers(); | |
for( var i = 0; i < _layers.length(); i++ ) | |
{ | |
if( this._checkLayer( iterator, context, _layers[i], i, _layers ) ) | |
{ | |
if( !getAll ) | |
return true; | |
} | |
if( sublayers && _layers[i].className().toString() === 'MSLayerGroup' ) | |
{ | |
if( this._getLayers( _layers[i], iterator, sublayers, getAll, context ) ) | |
{ | |
if( !getAll ) | |
return true; | |
} | |
} | |
} | |
return false; | |
}, | |
/** | |
* Internal function which checks the layer for the given condition | |
* | |
* @param {function} iterator the iterator function | |
* @param {object} context the iterater call context | |
* @param {object} obj the layer | |
* @param {integer} index the index in the for loop | |
* @param {array} collection all layers in the actual layer group | |
* @return {bool} true if found | |
*/ | |
_checkLayer : function( iterator, context, obj, index, collection ) | |
{ | |
if( iterator.call( context, obj, index, collection ) ) | |
{ | |
this.collection.push( obj ); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry, but why don't you bundle this Gists into a plugin. At the moment, I don't know how to use them.