-
Notifications
You must be signed in to change notification settings - Fork 2
/
RenameStyles.jsx
159 lines (133 loc) · 3.57 KB
/
RenameStyles.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
In character-style names, replace spaces with underscores, remove diacritics,
remove non-word characters, and lower-case what remains.
Duplicate names are avoided by suffixing _1, _2, etc.
Peter Kahrel at PepCon 2014
Version: 1.0, June 23, 2014
*/
// Single undo of script added by Jim Birkenseer.
if (parseInt(app.version) >= 6)
{
// ID CS4 or newer. This script creates a single undo in the Edit menu.
app.doScript(main, ScriptLanguage.javascript, undefined, UndoModes.entireScript, "Undo Rename Styles");
}
else
{
// ID CS3 or older. This version of InDesign cannot create a single undo for a script.
main();
}
function main ()
{
// Check for open documents and prompt to start code added by Jim Birkenseer.
if (app.documents.length == 0)
{
alert("Rename styles requires that an InDesign document is open.");
return;
}
var retVal = confirm("Remove special characters from all style names in your InDesign document?");
if (!retVal)
{
alert("The script has been canceled. Your document has not been modified.");
return;
}
// Pre-compile all regexes
var reNonWordChar = /\W/g;
var reSpace = / /g;
var reA =/[áàâäåāąăæ]/g;
var reC =/[çćčċ]/g;
var reD =/[ďđ]/g;
var reE =/[éèêëęēĕėě]/g;
var reG =/[ģĝğġ]/g;
var reH =/[ĥħ]/g;
var reI =/[íìîïīĩĭįi̇]/g;
var reJ =/[ĵ]/g;
var reK =/[ķ]/g;
var reL =/[łĺļľ]/g;
var reN =/[ñńňņŋ]/g;
var reO =/[óòôöōŏőøœ]/g;
var reR =/[ŕřŗ]/g;
var reS =/[śšŝşș]/g;
var reSS =/ß/g;
var reT =/[ţțťŧ]/g;
var reU =/[úùûüůūųũŭűų]/g;
var reW =/[ŵ]/g;
var reY =/[ÿýŷ]/g;
var reZ =/[źżž]/g;
// The following regex tests if a style name is already ok. If we don't do this,
// ok style names get a unique-making suffix even if they are already unique.
var isOK = /^[_a-z0-9]+$/;
var name = "";
renameStyles ('allCharacterStyles');
renameStyles ('allParagraphStyles');
renameStyles ('allObjectStyles');
renameStyles ('allTableStyles');
renameStyles ('allCellStyles');
function renameStyles (category)
{
var styles = app.documents[0][category];
var known = getNames (styles);
for (var i = styles.length-1; i > 0; i--)
{
if (styles[i].name.indexOf ('[') < 0)
{
styles[i].name = renamed (styles[i].name, known);
}
}
} // renameStyles
function getNames (styles)
// Collect the stylenames of only those style names
// that do not conform to the required format
{
var obj = [];
for (var i = styles.length-1; i > 0; i--)
{
if (!isOK.test(styles[i].name))
{
obj[styles[i].name] = true;
}
}
return obj;
}
function renamed (str, known)
{
str = stripAccents (str.toLowerCase());
str = str.replace (reSpace, "_").replace (reNonWordChar, "");
return uniqueName (str, known);
}
function uniqueName (base, known)
{
function stripBase (s) {return s.replace(/_\d+$/,"");}
var n = 0;
while (known[base])
{
base = stripBase (base) + "_" + String(++n);
}
known[base] = true;
return base;
}
function stripAccents (s)
{
return s.replace (reA, "a").
replace (reC, "c").
replace (reD, "d").
replace (reE, "e").
replace (reG, "g").
replace (reH, "h").
replace (reI, "i").
replace (reJ, "j").
replace (reK, "k").
replace (reL, "l").
replace (reN, "n").
replace (reO, "o").
replace (reR, "r").
replace (reS, "s").
replace (reSS, "ss").
replace (reT, "t").
replace (reU, "u").
replace (reW, "w").
replace (reY, "y").
replace (reZ, "z");
} // stripAccents
alert("Style renaming is complete."); // Added by Jim Birkenseer
} // main