Skip to content

Instantly share code, notes, and snippets.

@dustinsenos
Last active February 1, 2019 08:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinsenos/df14bc33139721fb738d0c0a765bb3cf to your computer and use it in GitHub Desktop.
Save dustinsenos/df14bc33139721fb738d0c0a765bb3cf to your computer and use it in GitHub Desktop.
A simple sketch plugin to rename the selected layers to match the contents of their text.
var sketch = context.api()
var MAX_CHARS = 60
var document = sketch.selectedDocument
var selection = document.selectedLayers
var renamedTextLayers = false
selection.iterate(function(item) {
if (!item.isText) {
return
}
if (item.text.length() > MAX_CHARS) {
item.name = item.text.substring(0, MAX_CHARS) + "…"
} else {
item.name = item.text
}
renamedTextLayers = true
})
if (renamedTextLayers) {
sketch.message("Layer names now equal the layer’s text content.")
} else {
sketch.message("Please select a text layer.")
}
@dustinsenos
Copy link
Author

dustinsenos commented May 18, 2017

To create and run this plugin in Sketch:

  • Click the Plugin menu item in Sketch
  • Click Custom Plugin…
  • Paste the above script
  • Click Save…
  • Name it Rename text layers to match contents
  • Select a layer you want the plugin to rename
  • Open the Plugin menu, click Rename text layers to match contents

Tested in Sketch 43.2

Feedback or suggestions welcome @dustin

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