/
apply_Style_With_RegExp.jsx
executable file
·149 lines (115 loc) · 4.68 KB
/
apply_Style_With_RegExp.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
#target illustrator
// For the contents of the text object within the selection,
// applies the selected style to the part that matches the input regular expression
(function(){
const SCRIPTNAME = "apply Style With RegExp";
var _opt = {
ignore_case : true,
clearing_overrides : true,
styles : [],
input_field_width : 16,
rex : "",
character_style : "",
text_frames : []
}
function showDialog(){
getCharacterStyleNames();
_opt.text_frames = [];
getTextFramesFromSelection(_opt.text_frames);
if(_opt.text_frames.length < 1){
showError("no text object selected");
return;
}
if(app.documents.length < 1) return;
var adoc = app.activeDocument;
var win = new Window("dialog", SCRIPTNAME);
win.alignChildren = "left";
win.add("statictext", undefined, "regular expression to search");
win.etext1 = win.add("edittext", undefined, "");
win.etext1.characters = _opt.input_field_width;
win.add("statictext", undefined, "character style to apply");
win.list1 = win.add("dropdownlist", undefined, _opt.styles);
win.list1.selection = 0;
win.check0 = win.add("checkbox", undefined, "ignore case");
win.check0.value = _opt.ignore_case;
win.check1 = win.add("checkbox", undefined, "clearing overrides");
win.check1.value = _opt.clearing_overrides;
win.buttons = win.add("group");
win.buttons.alignment = "center";
win.buttons.ok = win.buttons.add("button", undefined, "OK");
win.buttons.cancel = win.buttons.add("button", undefined, "Cancel");
var isValuesOK = function(){
_opt.rex = win.etext1.text;
if(_opt.rex == ""){
showError("please input a regular expression to search");
return false;
}
var style_name = win.list1.selection == null ? "" : win.list1.selection.text;
if(style_name == ""){
showError("please select a character style to apply");
return false;
}
_opt.character_style = app.activeDocument.characterStyles.getByName(style_name);
_opt.ignore_case = win.check0.value;
_opt.clearing_overrides = win.check1.value;
return true;
}
win.buttons.ok.onClick = function(){
try{
win.enabled = false;
if(isValuesOK() == false) return;
if(main() == false) return;
} catch(e) {
alert(e);
} finally {
win.enabled = true;
}
win.close();
}
win.buttons.cancel.onClick = function(){
win.close();
}
win.show();
}
function getCharacterStyleNames(){
var styles = app.activeDocument.characterStyles;
_opt.styles = [];
for(var i = 0; i < styles.length; i++){
_opt.styles.push(styles[i].name);
}
}
function main(){
// regex_changeContentsOfWordOrString_RemainFormatting.jsx
// regards pixxxel schubser
var result, aCon;
var regex_option = _opt.ignore_case ? "gmi" : "gm";
for(var i = _opt.text_frames.length - 1; i >= 0; i--){
var atf = _opt.text_frames[i];
var s = new RegExp(_opt.rex, regex_option);
while ( result = s.exec(atf.contents)) {
try {
aCon = atf.characters[result.index];
aCon.length = result[0].length;
_opt.character_style.applyTo(aCon, _opt.clearing_overrides);
} catch (e) {};
}
}
}
function getTextFramesFromSelection(textFrames, items){
if(!items) items = app.activeDocument.selection;
for(var i = 0; i < items.length; i++){
if(items[i].locked || items[i].hidden){
continue;
} else if(items[i].typename == "TextFrame"){
textFrames.push(items[i]);
} else if(items[i].typename == "GroupItem"){
getTextFramesFromSelection(textFrames, items[i].pageItems);
}
}
}
function showError(msg){
alert(SCRIPTNAME + "\rERROR :\r"
+ msg);
}
showDialog();
})();