-
Notifications
You must be signed in to change notification settings - Fork 6
/
Rename Items.jsx
49 lines (36 loc) · 1.45 KB
/
Rename Items.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var ItemRenamer, itemRenamer;
ItemRenamer = (function() {
function ItemRenamer() {
if ( app.activeDocument.selection.length > 0 ) {
this.replacements = [
prompt( 'What would you like to replace?\nEmpty if you are renaming instead of replacing.', 'Eg: source.' ),
prompt( 'What would you like to replace it with?\nUse %i placeholder to auto-increment naming. "name-%i" will name items: "name-1", "name-2", etc.', 'Eg: replacement, or replacement-%i' )
];
this.renameItems( app.activeDocument.selection );
} else {
alert( 'Select the items you would like to be renamed. You can select a Layer to rename all the items inside it.' );
}
}
ItemRenamer.prototype.renameItems = function( items ) {
var _i, _results, counting, regxp;
_results = [];
regxp = new RegExp( '%i', 'i' );
counting = this.replacements[1].match( regxp ) ? prompt( 'Start counting at:', '1' ) : 0;
for (_i = 0; _i < items.length; _i++) {
nb = parseInt( _i ) + parseInt( counting );
origin = this.replacements[0];
replacement = this.replacements[1].match( regxp ) ? this.replacements[1].replace( regxp, ( '' + nb ) ) : this.replacements[1];
// Renaming.
if ( origin === '' ) {
_results.push( items[_i].name = replacement );
}
// Replacing.
else {
_results.push( items[_i].name = items[_i].name.replace( origin, replacement ) );
}
}
return _results;
};
return ItemRenamer;
})();
itemRenamer = new ItemRenamer();