Skip to content

Instantly share code, notes, and snippets.

@omz
Created June 29, 2014 17:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save omz/1089ee924c25358c3671 to your computer and use it in GitHub Desktop.
Save omz/1089ee924c25358c3671 to your computer and use it in GitHub Desktop.
/*
Batch-Rename Layers -- plugin for Sketch 2 (http://www.bohemiancoding.com/sketch/)
INSTALLATION
------------
Select "Reveal Plugins Folder..." in the "Plugins" menu,
then put this file in the folder that shows up in the Finder.
USAGE
-----
The plugin has three modes, all of which operate on the current selection.
1. Find/Replace: Replaces text in the names of all selected layers, e.g. if you have
"Rectangle 1" and "Rectangle 2" selected, you can rename them to "Button 1" and
"Button 2" in one step.
2. RegEx: Same as find/replace, but uses a regular expression instead of basic text search.
3. Sequence: Changes the names of all selected layers to consist of a common prefix and a
number. The order of the numbers depends on the layers' positions (left-to-right and
top-to-bottom).
For example, with "Layer_" as the prefix, a layout like this:
[ Foo ] [ Bar ]
[ Baz ]
would be renamed to:
[ Layer_1 ] [ Layer_2 ]
[ Layer_3 ]
*/
function toJSArray (arr) {
var len = [arr count]
var res = [];
while(len--){
res.push(arr[len]);
}
return res;
}
function sort_by_pos (a, b) {
var topA = [[a frame] top];
var topB = [[b frame] top];
var bottomA = [[a frame] top] + [[a frame] height];
var bottomB = [[b frame] top] + [[a frame] height];
if ((topA >= topB && topA <= bottomB) || (topB >= topA && topB <= bottomA)) {
return [[a frame] left] - [[b frame] left];
}
return topA - topB;
}
function showModeDialog () {
var alert = [[NSAlert alloc] init];
[alert setMessageText:"Select a renaming mode"];
[alert addButtonWithTitle:'Sequence'];
[alert addButtonWithTitle:'Find/Replace'];
[alert addButtonWithTitle:'RegEx'];
[alert addButtonWithTitle:'Cancel'];
var responseCode = [alert runModal]
return responseCode;
}
function renameSequentially () {
var prefix = [doc askForUserInput:"Enter a prefix to rename the selected layers." initialValue:"Layer_"];
if (prefix == null) {
return;
}
var startStr = [doc askForUserInput:"Start at number..." initialValue:"1"];
if (startStr == null) {
return;
}
var start = parseInt(startStr);
var sorted_selection = toJSArray(selection).sort(sort_by_pos);
for (var i=0; i < sorted_selection.length; i++) {
var item = sorted_selection[i];
item.setName(prefix + (i+start));
}
[doc showMessage:sorted_selection.length + " layers renamed"];
}
function regexReplace () {
var findStr = [doc askForUserInput:"Enter regular expression to replace." initialValue:""];
if (findStr == null) {
return;
}
var regex = new RegExp(findStr, "g");
var replaceStr = [doc askForUserInput:"Enter replacement" initialValue:""];
if (replaceStr == null) {
return;
}
var count = 0;
var selection_array = toJSArray(selection)
for (var i=0; i < selection_array.length; i++) {
var item = selection_array[i];
if (item.name().search(regex) >= 0) {
item.setName(item.name().replace(regex, replaceStr));
count++;
}
}
[doc showMessage:count + " layers renamed"];
}
function findReplace () {
var findStr = [doc askForUserInput:"Enter text to replace in selected layer names." initialValue:""];
if (findStr == null) {
return;
}
var replaceStr = [doc askForUserInput:"Enter replacement" initialValue:""];
if (replaceStr == null) {
return;
}
var count = 0;
var selection_array = toJSArray(selection)
for (var i=0; i < selection_array.length; i++) {
var item = selection_array[i];
if (item.name().search(findStr) >= 0) {
item.setName(item.name().replace(findStr, replaceStr));
count++;
}
}
[doc showMessage:count + " layers renamed"];
}
function main () {
if ([selection count] == 0) {
[doc showMessage:"Nothing selected"];
} else {
var mode = showModeDialog();
print(mode);
if (mode == NSAlertFirstButtonReturn) {
renameSequentially();
} else if (mode == NSAlertSecondButtonReturn) {
findReplace();
} else if (mode == NSAlertThirdButtonReturn) {
regexReplace();
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment